中国象棋

版权声明:小简原创 https://blog.csdn.net/qq_43469554/article/details/87900692

输入表示这个棋盘,我们用’. ‘表示空位置,用’#"表示该位置有棋子,用’S’表示初始的马的位置,用’T’表示马需要跳到的位置。

输入保证一定只存在一个’S’和一个’T’。
输出格式
如果在不移动其他棋子的情况下,马能
从’S’跳到’T’,那么输出一行"Yes", 否则输出一行"No"。

样例输入:
.#…#S#
…#.#.#…
…##.#…#
…##.
…T…
…#.#…
…#…
…###…

.##…

样例输出:
Yes

#include<iostream>
#include<string>
using namespace std;
int n , m;
char s[10][10];
int dir[8][2] = {{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-1, -2}, {-2, -1}, {2, -1}, {1, -2}};//8个方向 
bool vis[10][10];
bool f;
bool in(int x, int y)
{
	return 0 <= x && x < 10 && 0 <= y && y < 9; 
}
void dfs(int x, int y)
{
	
	vis[x][y] = true;
	if (s[x][y] == 'T')
	{
		f = true;
		return;
	}
	for (int i = 0; i < 8; i++)
	{
		int tx = x + dir[i][0];
		int ty = y + dir[i][1];
		if (in(tx, ty) && s[tx][ty] != '#' && !vis[tx][ty])
		{
			dfs(tx, ty);
		}
	}
}

int main()
{
	int x, y;
	for (int i = 0; i < 10; i++)
	{
		cin.getline(s[i], 10, '\n');
	}
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			if (s[i][j] == 'S')
			{
				x = i;
				y = j;
			}
		}
	}
	dfs(x, y);
	if (f)
	{
		cout << "Yes" << endl;
	}
	else
	{
		cout << "No" << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43469554/article/details/87900692
今日推荐