再次求调

P1141 01迷宫

Walledjc @ 2024-08-11 10:49:37

dfs:


#include<bits/stdc++.h> 
using namespace std;
long long n,m,vis[1005][1005],mp[1005][1005],ans[1005][1005],cnt=1,tmp;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
void dfs(int sx,int sy,int tmp){
    vis[sx][sy]=1;
    for(int i=0;i<4;i++){
        int tx=sx+dx[i];
        int ty=sy+dy[i];
        if(tx<1||ty<1||tx>n||ty>n||vis[tx][ty]==1) continue;
        if(tmp==0&&mp[tx][ty]==0) continue;
        if(tmp==1&&mp[tx][ty]==1) continue;
        vis[tx][ty]=1;
        cnt++;
        dfs(tx,ty,!tmp);
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            char ch;
            cin>>ch;
            mp[i][j]=ch-'0';
        }
    } 
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(vis[i][j]==0){
                dfs(i,j,mp[i][j]);
                ans[i][j]=cnt;
                cnt=1;
                memset(vis,0,sizeof(vis));
            }
        }
    }
    for(int i=1;i<=m;i++){
        int a,b;
        cin>>a>>b;
        cout<<ans[a][b]<<endl;
    }
    return 0;
}

by Walledjc @ 2024-08-11 16:09:54

@_zhang 谢


by Walledjc @ 2024-08-12 15:54:41


上一页 |