萌新60分WA求助,求调

P1141 01迷宫

bhr123456 @ 2024-05-19 13:04:18

#include <bits/stdc++.h>
using namespace std;
int n, m, x, y;
char g[2000][2000];
bool vis[2000][200];
int col[2000][3], cnt[2000000], res = 1;
int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void dfs(int x, int y) {
    for (int i = 0; i < 4; i++) {
        int xx = x + dir[i][0];
        int yy = y + dir[i][1];
        if (vis[xx][yy] == 0 && xx <= n && yy <= n && xx > 0 && yy > 0 && g[xx][yy] != g[x][y]) {
            vis[xx][yy] = 1;
            cnt[res]++;
            col[xx][yy] = res;
            dfs(xx, yy);
        }
    }
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> g[i][j];
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (vis[i][j] == 0) {
                vis[i][j] = 1;
                col[i][j] = res;
                cnt[res] = 1;
                dfs(i, j);
                res++;
            }
        }
    }
    for (int i = 1; i <= m; i++) {
        cin >> x >> y;
        cout << cnt[col[x][y]] << endl;
    }
    return 0;
}

萌新60分WA求助,求调


by Ooj_bai @ 2024-06-15 08:45:29

vis数组开小了


|