离谱!DFS 90pts,1*WA
查看原帖
离谱!DFS 90pts,1*WA
1345055
Better_Tomorrow楼主2025/2/3 21:59
#include<bits/stdc++.h>

using namespace std;
char a[105][105];
bool c;
int n, m;
bool vis[105][105] = {false};
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,-1,1};
void dfs(int x, int y) {
	if (x == n && y == m) {
		c = true;
		return;
	}
	else {
		for (int i = 0; i < 4; i++) {
			int xx = x + dx[i];
			int yy = y + dy[i];
			if (a[xx][yy] != '#' && xx >= 0 && xx <= n && yy >= 0 && yy <= m && !vis[xx][yy]) {
				vis[xx][yy] = 1;
				dfs(xx,yy);
			}
		}
	}
}
int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			cin >> a[i][j];
	vis[1][1] = 1;
	dfs(1,1);
	if (c == true) cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}
2025/2/3 21:59
加载中...