bfs样例过了全wa

B3625 迷宫寻路

chenyanhui @ 2024-11-03 16:41:25

#include<bits/stdc++.h>
using namespace std;
const int N = 1145;
int n,m;
char a[N][N];
int xx[4] = { 0,0,1,-1 };
int yy[4] = { 1,-1,0,0 };
struct inf {
    int x, y;
};
void bfs() {
    queue<inf>q;
    q.push({ 1,1 });
    a[1][1] = '#';
    while (!q.empty()) {
        inf t = q.front();
        q.pop();
        for (int i = 0; i < 4; i++) {
            int nx = t.x + xx[i];
            int ny = t.y + yy[i];
            if (nx > 0 && nx <= n && ny > 0 && ny <= m && a[nx][ny] == '.') {
                a[nx][ny] = '#';
                if (t.x == n && t.y == m) {
                    return;
                }
                q.push({ nx,ny });
            }
        }

    }
}
int main() {
    cin >> n>>m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }
    bfs();
    if (a[n][m] == '#') {
        cout << "YES";
    }
    else {
        cout << "NO";
    }
    return 0;
}

by chzhh_111 @ 2024-11-03 16:49:08

@chenyanhui 是输出 YesNo


|