silentzdw @ 2023-06-25 17:44:32
#include <bits/stdc++.h>
using namespace std;
char a[110][110];
int n, m;
int dir[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
bool st[110][110];
bool dfs(int x, int y) {
if (x == n && y == m) {
return true;
}
for (int k = 0; k < 4; k++) {
int tx = x + dir[k][0];
int ty = y + dir[k][1];
if (tx < 1 || tx > n || ty < 1 || ty > m || a[tx][ty] == '#') {
continue;
}
if (!st[tx][ty]) {
st[tx][ty] = true;
if (dfs(tx, ty)) return true;
st[tx][ty] = false;
}
}
return false;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
if (dfs(1, 1)) {
cout << "Yes" << "\n";
} else {
cout << "No" << "\n";
}
return 0;
}
by _Steve_ @ 2023-06-25 18:01:05
@silentzdw 把 st[tx][ty] = false;
删掉即可,因为dfs遍历过这个点就不能再遍历了,否则会出现重复走点的情况
by silentzdw @ 2023-06-25 19:57:24
@yutiti8001 好的好的,谢谢啦
by _Steve_ @ 2023-06-25 20:03:50
@silentzdw 不用谢