40分求调WA

B3625 迷宫寻路

wawatime1 @ 2024-09-25 17:49:09


#include <bits/stdc++.h>
using namespace std;
char a [100][100];
bool v [100][100];
int n, m, f = 0, dx [4] = {0, 0, 1, -1}, dy [4] = {-1, 1, 0, 0};

void dfs (int d, int x, int y) {
    if (x == n && y == m) {
        cout << "Yes"; 
        f = 1;
        return;
    } else {
        for (int i = 0; i < 4; i++){
            int nx = x + dx [i];
            int ny = y + dy [i];
            if (v [nx][ny] == 0 && a [nx][ny] == 0 && nx > 0 && nx <= n && ny > 0 && ny <= m && a [nx][ny] == '.') {
                v [nx][ny] = 1;
                dfs (d + 1, nx, ny);
                v [nx][ny] = 0;
            }
        }
    }
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++)
            cin >> a [i][j];
    }
    v [0][0] = 1;
    dfs (0, 1, 1);
    if (f == 0)
        cout << "No";

    return 0;
}

|