01迷宫0分求条!!!

P1141 01迷宫

yupeishan2012 @ 2024-09-15 09:07:11


#include<bits/stdc++.h>
using namespace std;

int n,m;
int sum=1;
int dx[6]={0,0,0,-1,1};
int dy[6]={0,-1,1,0,0};
char a[1005][1005];
bool b[1005][1005];

void dfs( int x, int y, int step ){
    cout << x << y << step << endl;
    for( int i=1; i<=4; i++ ){
        int xx=x+dx[i];
        int yy=y+dy[i];
        if( (a[x][y]=='1' and a[xx][yy]=='0') or (a[x][y]=='0' and a[xx][yy]=='1') and xx>0 and yy>0 and xx<=n and yy<=n and !b[xx][yy] ){
            sum++;
            //cout<<xx<<" "<<yy<<" ";
            b[xx][yy]=1;
            dfs( xx,yy,step+1 );
        }
    }
}

int main(){
    //freopen("in.txt","r",stdin);
    cin>>n>>m;
    for( int i=1; i<=n; i++ ){
        for( int j=1; j<=n; j++ ){
            cin>>a[i][j];
        }
    }
    int c,d;
    for( int i=1; i<=m; i++ ){
        memset( b,0,sizeof(b) );
        sum=1;
        cin>>c>>d;
        b[c][d]=1;
        dfs( c,d,0 );
        cout<<sum/2+1<<"\n";
    }
    return 0;
}

by liruizhou_lihui @ 2024-09-15 09:12:51

@yupeishan2012 你dfs有多余的输出


by yupeishan2012 @ 2024-09-15 09:16:52

@liruizhou_lihui 我是没有注释掉


by yupeishan2012 @ 2024-09-15 09:18:34

但是我的代码输出的题面上要求的结果是错的


by ggpw_XNW @ 2024-09-15 11:03:53

@yupeishan2012 一下是我的抽搐小代码:

#include<bits/stdc++.h>
using namespace std;
int n , m;
char a[1005][1005];
int c[1005][1005] , ans[100005];
void dfs(int x , int y , int z , int step){
    if(x<1||x>n||y<1||y>n||a[x][y]-'0'!=z||c[x][y]!=-1)return;
    c[x][y] = step , ans[step]++;
    dfs(x,y-1,!z,step);
    dfs(x,y+1,!z,step);
    dfs(x-1,y,!z,step);
    dfs(x+1,y,!z,step);
}
int main(){
    cin >> n >> m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin >> a[i][j];
            c[i][j] = -1;
        }
    }
    for(int i=1;i<=m;i++){
        int xx , yy;
        cin >> xx >> yy;
        if(c[xx][yy]==-1){
            dfs(xx,yy,a[xx][yy]-'0',i);
        }else ans[i] = ans[c[xx][yy]];
    }
    for(int i=1;i<=m;i++)cout << ans[i] << endl;
    return 0;
} 

by ggpw_XNW @ 2024-09-15 11:04:13

@yupeishan2012 但是能A


by yupeishan2012 @ 2024-09-15 11:18:37

@User1025109 谢谢咯


|