蒟蒻求助

P1162 填涂颜色

daydayWA @ 2021-11-03 19:33:26

蒟蒻求助

啊,蒟蒻到底哪错了,为什么外面0也输出了2

#include<bits/stdc++.h>
using namespace std;
int dx[4]= {0,1,0,-1},dy[4]= {1,0,-1,0};
int a[32][32];
int n;
queue <int> q; 
void dfs(int x,int y) {
    a[x][y]=2;
    for (int i=0; i<4; i++)
        if(x>=0&&x<=n+1&&y>=0&&y<=n+1&&a[x][y]==0)
            dfs(x+dx[i],y+dy[i]);
}
int main(){ 
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
        }
    }
    dfs(0,0);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(a[i][j]==0){
                cout<<2<<" ";
            }
            if(a[i][j]==1){
                cout<<1<<" ";
            }
            if(a[i][j]==2){
                cout<<0<<" ";
            }
        }
        cout<<endl;
    }
    return 0;
}

by CE_自动机 @ 2021-11-03 19:51:09

@liu_zimo

你的扩展判断为什么判断的是脚下的点

if(x+dx[i]>=0&&x+dx[i]<=n+1&&y+dy[i]>=0&&y+dy[i]<=n+1&&a[x+dx[i]][y+dy[i]]==0)

改成这样就行了


by daydayWA @ 2021-11-03 19:55:43

@CE_自动机 谢谢dalao


|