70分TLE了求改进,玄关

P1141 01迷宫

ZiJin26910914 @ 2024-07-12 17:25:34

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+5;
struct node{
    int bx,by;
};
queue<node>Q;
char a[N][N];
int vis[N][N],n,m,X,Y,nx[]={0,0,1,0,-1},ny[]={0,1,0,-1,0},ans;
void bfs(int x,int y){
    ans=1;
    Q.push((node){x,y});
    vis[x][y]=1;
    node now;
    while(!Q.empty()){
        now=Q.front();
        Q.pop();
        for(int i=1;i<=4;i++){
            int xx=now.bx+nx[i],yy=now.by+ny[i];
            if(xx>0&&xx<=n&&yy>0&&yy<=n&&vis[xx][yy]==0&&a[now.bx][now.by]!=a[xx][yy]){
                Q.push((node){xx,yy});
                vis[xx][yy]=1;
                ans++;
            }
        }
    }
    cout<<ans<<endl;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            cin>>a[i][j];
    while(m--){
        cin>>X>>Y;
        while(!Q.empty()) Q.pop();
        memset(vis,0,sizeof(vis));
        bfs(X,Y);
    }
    return 0;
}

求助大佬。。。


by scratch_szc @ 2024-07-12 17:46:07

联通快


by heyichen201209 @ 2024-07-12 18:32:12

@ZiJin26910914 6666666


|