可恶,卡常卡不进去!
查看原帖
可恶,卡常卡不进去!
657484
hebowenAb31415926楼主2023/6/29 21:35

Here

有大佬帮我卡一下么Thanks

#include<stdio.h>
using namespace std;
const int N=1e3+11;
int n,m;
bool ma[N][N];
int mp[N][N];
struct node{
    int x,y;
}que[N*N];
int top,now;
int xx[4]={0,1,0,-1},yy[4]={-1,0,1,0};
inline char read(){
    char c=getchar();
    while(c!='1'&&c!='0')
        c=getchar();
    return c;
}
int bfs(int x,int y){
    int i,j,k;
    int top=0,now=1,all=1;
    bool b[N][N]={};
    for(i=0;i<=n+1;i++){
        b[0][i]=1;
        b[i][0]=1;
        b[n+1][i]=1;
        b[i][n+1]=1;
    }
    b[x][y]=1;
    que[++top]={x,y};
    while(now<=top){
        for(i=0;i<4;i++){
            int nx=que[now].x+xx[i],ny=que[now].y+yy[i];
            if(!b[nx][ny]&&ma[que[now].x][que[now].y]!=ma[nx][ny]){
                b[nx][ny]=1;
                all++;
                que[++top]={nx,ny};
            }
        }
        now++;
    }
    for(i=1;i<=top;i++){
        mp[que[i].x][que[i].y]=all;
    }
    return all;
}
int main(){
    int i,j,k;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            ma[i][j]=read()-'0';
        }
    }
    int a,b;
    while(m--){
        scanf("%d%d",&a,&b);
        printf("%d\n",mp[a][b]?mp[a][b]:bfs(a,b));
    }
    return 0;
}
2023/6/29 21:35
加载中...