60分求助
查看原帖
60分求助
915253
xingwuKling楼主2023/5/19 21:21

这是代码

#include<iostream>

using namespace std;
int n, m;
const int N = 101;
char a[N][N];
bool judge[N][N] = { false }, flag = false;
void dfs(int x, int y)
{
	judge[x][y] = true;
	if (!judge[x + 1][y]&&x<n)//向下走
	{
		judge[x + 1][y] = true;
		dfs(x + 1, y);
		judge[x + 1][y] = false;
	}
	if (!judge[x - 1][y] && x>0 )向上走
	{
		judge[x - 1][y] = true;
		dfs(x - 1, y);
		judge[x + 1][y] = false;
	}
	if (!judge[x][y - 1] && y>0)向左走
	{
		judge[x][y - 1] = true;
		dfs(x, y - 1);
		judge[x][y - 1] = false;
	}
	if (!judge[x][y + 1] && y < m)向右走
	{
		judge[x][y + 1] = true;
		dfs(x, y + 1);
		judge[x][y + 1] = false;
	}
	if (x == n && y == m)
	{
		flag = true;
		return;
	}
}
int main()
{

	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			cin >> a[i][j];
			if (a[i][j] == '#')a[i][j] = true;
		}
	}
	dfs(1, 1);
	if (flag)cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}
2023/5/19 21:21
加载中...