70分蒟蒻 TLE求调(玄关)

P1141 01迷宫

CHENGUOTAO @ 2024-11-01 13:52:16

#include<bits/stdc++.h>
using namespace std;
long long n,T,x,y,ans;
string s;
long long cx[10]={0,0,-1,1};
long long cy[10]={1,-1,0,0};
char a[1005][1005];
bool vis[1005][1005];
void dfs(int _x,int _y){
    vis[_x][_y]=1;
    ans++;
    for(int i=0;i<4;i++){
        int xx=_x+cx[i];
        int yy=_y+cy[i];
        if(xx>0&&xx<=n&&yy>0&&yy<=n
        &&vis[xx][yy]==0){
            if(a[_x][_y]=='1')
                if(a[xx][yy]=='0')
                    dfs(xx,yy);
            if(a[_x][_y]=='0')
                if(a[xx][yy]=='1')
                    dfs(xx,yy);
        }
    }
    return ;
}
int main(){
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);
    scanf("%lld%lld",&n,&T);
    for(int i=1;i<=n;i++){
        cin>>s;
        for(int j=1;j<=n;j++){
            a[i][j]=s[j-1];
        }
    }
    while(T--){
        scanf("%lld%lld",&x,&y);
        ans=0;
        dfs(x,y);
        memset(vis,false,sizeof(vis));
        printf("%lld\n",ans);
    }
    return 0;
}

|