32分求助!!!!

P1162 填涂颜色

nopic @ 2022-07-19 10:19:18

#include<bits/stdc++.h>
using namespace std;
int a[31][31]={0},n,dx[4]={1,-1,0,0},dy[4]={0,0,1,-1},c;
bool b[31][31]={0};
void dfs(int x,int y)
{
    int nx=x,ny=y;
    for(int i=0;i<=3;i++)
    {
        nx=dx[i]+nx,ny=dy[i]+ny;
        if(a[nx][ny]==0&&b[nx][ny]==0&&nx>=1&&nx<=n&&ny>=1&&ny<=n)
        {
            b[nx][ny]=1;
            dfs(nx,ny);
        }
    }
}
int main()
{
    memset(b,0,sizeof(b));
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            scanf("%d",&a[i][j]);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==1||j==1||i==n||j==n)
            {
                if(a[i][j]==0)
                    dfs(i,j);
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(a[i][j]==0)
            {
                if(b[i][j]==1)
                    printf("0 ");
                else
                    printf("2 ");
            }
            else
                printf("1 ");
        }
        printf("\n");
    }
    return 0;
}

哪里错了?


by _dream_ @ 2022-07-19 10:28:39

nx=dx[i]+nx,ny=dy[i]+ny;

应为

nx=dx[i]+x,ny=dy[i]+y;

by nopic @ 2022-07-19 14:18:23

@dream 谢谢!!


|