WA 40分求助
查看原帖
WA 40分求助
1812436
cchu666楼主2025/8/31 16:37
#include <bits/stdc++.h>
using namespace std;
int n, m, v[100][100];
char a[100][100];
bool dfs(int x, int y) {
    if (x == n && y == m) return true;
    if (x - 1 >= 0 && a[x - 1][y] == '.' && v[x - 1][y] == 0) {
        v[x - 1][y] = 1;
        dfs(x - 1, y);
    }
    if (y - 1 >= 0 && a[x][y - 1] == '.' && v[x][y - 1] == 0) {
        v[x][y - 1] = 1;
        dfs(x, y - 1);
    }
    if (x + 1 < n && a[x + 1][y] == '.' && v[x + 1][y] == 0) {
        v[x + 1][y] = 1;
        dfs(x + 1, y);
    }
    if (y + 1 < m && a[x][y + 1] == '.' && v[x][y + 1] == 0) {
        v[x][y + 1] = 1;
        dfs(x, y + 1);
    }
    return false;
}
int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) cin >> a[i][j];
    }    
    cout << (dfs(0, 0) ? "Yes" : "No");
    return 0;
}
2025/8/31 16:37
加载中...