50pts,求助

P1141 01迷宫

zafa2023 @ 2024-07-20 18:15:43

#include<bits/stdc++.h>
using namespace std;
char x[1001][1001];
int x2[1001][1001]={};
bool iffi=0;
int ans=1;
int n,m;
void bds(int x1,int y,char zt){
    x2[x1][y]=1;
    if(x1!=1&&x2[x1-1][y]==0&&x[x1-1][y]!=zt)
    {
        ans++;
        if(zt=='0')bds(x1-1,y,'1');
        else bds(x1-1,y,'0');
    }
    if(y!=1&&x2[x1][y-1]==0&&x[x1][y-1]!=zt)
    {
        ans++;
        if(zt=='0')bds(x1,y-1,'1');
        else bds(x1,y-1,'0');
    }
    if(x1!=n&&x2[x1+1][y]==0&&x[x1+1][y]!=zt)
    {
        ans++;
        if(zt=='0')bds(x1+1,y,'1');
        else    bds(x1+1,y,'0');
    }
    if(y!=n&&x2[x1][y+1]==0&&x[x1][y+1]!=zt)
    {
        ans++;
        if(zt=='0')bds(x1,y+1,'1');
        else bds(x1,y+1,'0');
    }
    if(ans==n*n&&iffi==0)
    {
        cout<<ans<<endl;
        iffi=1;
        return ;
    }
    if((x[x1+1][y]==zt||x2[x1+1][y]==1)&&(x[x1-1][y]==zt||x2[x1-1][y]==1)&&(x[x1][y+1]==zt||x2[x1][y+1]==1)&&(x[x1][y-1]==zt||x2[x1][y-1]==1))
    {
        return ;
    }
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int i1=1;i1<=n;i1++)
        {
            cin>>x[i][i1];
        }
    }
    x2[1][1]=1;
    int x3,y;
    for(int i=1;i<=m;i++)
    {
        cin>>x3>>y;
        x2[x3][y]=1;
        bds(x3,y,x[x3][y]);
        if(iffi==0)cout<<ans<<endl;
        ans=1;
        iffi=0;
        memset(x2,0,sizeof(x2));
    }
    return 0;
}

by FIRESTARS @ 2024-07-20 18:19:38

@zafa2023 虽然但是,哥你这要记搜


by zafa2023 @ 2024-07-20 18:23:39

请dalao赐教


by Maisie586_ @ 2024-07-20 18:30:17

@zafa2023 不是记忆化的题你直接硬上啊


|