0pts求调(WA#1,剩下的RE)
  • 板块P1141 01迷宫
  • 楼主wbl1
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/6/20 17:32
  • 上次更新2025/6/21 14:05:04
查看原帖
0pts求调(WA#1,剩下的RE)
1349297
wbl1楼主2025/6/20 17:32
#include<bits/stdc++.h>
using namespace std;
int n,m,mp[1005][1005],tp[1005][1005];
int f[4][2] = {{-1,0},{1,0},{0,1},{0,-1}};
int q[1000005][2];
char ch;
void bfs(int x,int y){
    int l = 0,r = 1,cnt = 1;
    q[1][0] = x,q[1][1] = y;
    tp[x][y] = -1;
    while(l < r){
        l++;
        int xx = q[l][0],yy = q[l][1];
        for(int i = 0;i < 4;i ++){
            int X = xx + f[i][0],Y = yy + f[i][1];
            if(X >= 1 && X <= n && Y >= 1 && Y <= n 
               && mp[X][Y] != mp[xx][yy]
               && tp[X][Y] == 0){
                q[++r][0] = X,q[r][1] = Y;
                cnt++;
                tp[X][Y] = -1;
            }
        }
    }
    for(int i = 1;i <= r;i ++) tp[q[i][0]][q[i][1]] = cnt;
}
int main(){
    cin >> n >> m;
    for(int i = 1;i <= n;i ++){
        cin.get();
        for(int j = 1;j <= n;j ++) {
            ch = cin.get();
            mp[i][j] = ch - '0';
        }
    }
    for(int i = 1;i <= n;i ++){
        for(int j = 1;j <= n;j ++){
            if(tp[i][j] == 0) bfs(i,j);
        }
    }
    while(m --){
        int x,y;
        cin >> x >> y;
        cout << tp[x][y] << endl;
    }
    return 0;
}

记录

2025/6/20 17:32
加载中...