bfs全WA求调

P1141 01迷宫

511017802yrb @ 2024-04-01 21:59:34

#define _CRT_SECURE_NO_WARNINGS 2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
#define PI 3.1415926
struct p {
    int x;
    int y;
};
int n;
int a[1001][1001] = { 0 };
bool is[1001][1001] = { 0 };
int ans = 0;
queue<p> Q;
p pp;
p tmkp;
int posi[4][2] = { 0,1,0,-1,1,0,-1,0 };
void bfs() {
    while (!Q.empty()) {
        p tmp = Q.front();
        is[tmp.x][tmp.y] = 1;
        Q.pop();
        int re = a[tmp.x][tmp.y];
        for (int i = 0; i < 4; i++) {
            int ntx = tmp.x + posi[i][0];
            int nty = tmp.y + posi[i][1];
            if (!is[ntx][nty] && ((re + a[ntx][nty]) == 1) && ntx >= 1 && ntx <= n && nty >= 1 && nty <= n) {
                tmkp.x = ntx;
                tmkp.y = nty;
                Q.push(tmkp);
                ans++;
            }
        }
    }
}
int main() {
    ios::sync_with_stdio(0);
    int n, m;
    void bfs();
    cin >> n >> m;
    getchar();
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            char t1;
            scanf("%c", &t1);
            a[i][j] = t1 - '0';
        }
        getchar();
    }

    while (m--) {
        ans = 0;
        cin >> pp.x >> pp.y;
        Q.push(pp);
        ans++;
        bfs();
        cout << ans << '\n';
    }
}

by elpsconr @ 2024-04-03 01:52:20

is[ntx][nty]用完没有标记,然后底下为啥你还要Q.push(qq)


|