全局变量做并查集样例正确求调

P1141 01迷宫

AAA0721ciallo @ 2024-12-05 19:13:35

#include <iostream>
using namespace std;
long long ans = 0;
int move1[4][2] = { {0,1},{1,0},{-1,0},{0,-1} };
long long n, m;
int  board[1003][1003];
int  dp[1003][1003];
bool visited[1003][1003];//bfs洪水覆盖,再查点,每个点仅来一次即可
void dfs(int i, int j) {
    if (visited[i][j]) { return; }
    visited[i][j] = true;
    ans++;
    for (int k = 0; k < 4; k++) {
        int nx = i + move1[k][0];
        int ny = j + move1[k][1];
        if (nx >= 1 && nx <= n && ny >= 1 && ny <= m&&!visited[nx][ny]&&board[i][j]!=board[nx][ny]) {
            dfs(nx, ny);
        }
    }
    dp[i][j] = ans;
    return;
}
int main()
{

    cin >> n >> m;
    char ch;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> ch;
            board[i][j] = ch - '0';
        }
    }int a, b;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            dp[i][j] = 0;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (!visited[i][j]) {
                ans = 0;
                dfs(i, j);
            }//怎么直接标记最大值;
        }
    }
    for (int i = 1; i <= m; i++) {
        cin >> a >> b;
        ans = dp[a][b];
        cout << ans << endl;
    }

    return 0;
}//2 2
//01
//10
//1 1
//2 2

by DevilsFlame @ 2024-12-05 19:59:53

第16行有个错误


|