70分爆RE求调

P1141 01迷宫

chengkelin2024 @ 2024-10-13 08:05:19

#include<bits/stdc++.h>
using namespace std;
int a[1000][1000];
int qq[6][3] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int sum;
int ans[1010][1010];
int n;

void dfs(int x, int y){
    if(x > n || x < 1 || y > n || y < 1) return ;
    if(ans[x][y] > -1) return ;
    sum++;
    ans[x][y] = 0;
    for(int i = 0; i < 4; i++){
        if(a[x+qq[i][0]][y+qq[i][1]] != a[x][y])
            dfs(x+qq[i][0], y+qq[i][1]);
    }
}

int main(){
    memset(ans, -1, sizeof(ans));
    int m;
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++){
        char str[1010];
        scanf("%s", &str);
        for(int j = 1; j <= n; j++){
            a[i][j] = str[j-1] - 48;
        }
    }
    for(int i = 1; i <= m; i++){
        int x, y;
        sum = 0;
        scanf("%d %d", &x, &y);
        if(ans[x][y] > 0){
            printf("%d\n", ans[x][y]);
            continue;
        }
        dfs(x, y);
        printf("%d\n", sum);
        for(int u = 1; u <= n; u++){
            for(int v = 1; v <= n; v++){
                if(ans[u][v] == 0){
                    ans[u][v] = sum;
                }
            }
        }
    }
}

by andycode @ 2024-10-13 10:17:19

@chengkelin2024 a数组开小了


by by0113 @ 2024-10-14 13:06:54

能不能帮帮我???? 看一楼


|