90RE悬关

P1141 01迷宫

weberzcm @ 2024-11-27 22:00:45

#include<bits/stdc++.h>
using namespace std;
int n,m,ans,dp[1005][1005],d[100005][2],dx[]={0,0,1,-1},dy[]={1,-1,0,0};
char a[1005][1005];
void dfs(int x,int y)
{
    ans++;
    d[ans][0]=x; 
    d[ans][1]=y;
    dp[x][y]=1;
    for(int i=0;i<4;++i)
        {
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx<1||nx>n||ny<1||ny>n||dp[nx][ny]>0||a[nx][ny]==a[x][y]) continue;
        dfs(nx,ny);
    }
}
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--)
        {
        ans=0;
        int x,y;
        cin>>x>>y;
        if(dp[x][y]>0)
                {
            cout<<dp[x][y]<<endl;
            continue;
        }
        dfs(x,y);
        for(int j=1;j<=ans;++j)
            dp[d[j][0]][d[j][1]]=ans;
        cout<<ans<<endl;
    }
    return 0;
}

各位大佬,救救58~


|