有时候挺无助的,抄的b站上代码却编译不出来

P1162 填涂颜色

kelibzd @ 2024-03-07 22:53:35

#include<bits/stdc++.h>
using namespace std;
int a[35][35],b[35][35];
int n;
void dfs(int x,int y)
{
    if(x<0||x>n+1||y<0||y>n+1||a[x][y]!=0)
    {
        return;
    }
    a[x][y]=1;
    dfs(x-1,y);
    dfs(x+1,y);
    dfs(x,y-1);
    dfs(x,y+1);
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
          cin>>a[i][j];
          if(a[i][j]==0)
          b[i][j]=0;
          else
          b[i][j]=1
        }
    }
    dfs(0,0);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(a[i][j]==0)
            {
                cout<<2<<" ";
            }
            else
            {
                cout<<b[i][j]<<" ";
            }
        }
        cout<<endl;
    }
     return 0;
}

by MC小萌新 @ 2024-03-07 22:56:32

建议查看编译信息/qd

b[i][j]=1 这行没加分号。


by huashuo @ 2024-04-02 23:20:06

@MC小萌新

能帮忙看看为什么只过了两个吗

#include<bits/stdc++.h>
using namespace std;
int Map[32][32];
queue<int> nowx;
queue<int> nowy;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int vis[32][32];
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            scanf("%d", &Map[i][j]);
        }
    }

    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(Map[i][j] == 0)
            Map[i][j] = 2;
        }
    }
    for(int i = 1; i <= n; i++) if(Map[1][i] == 2)Map[1][i] = 0;
    for(int i = 1; i <= n; i++) if(Map[i][1] == 2)Map[i][1] = 0;
    for(int i = 1; i <= n; i++) if(Map[i][n] == 2)Map[i][n] = 0;
    for(int i = 1; i <= n; i++) if(Map[n][i] == 2)Map[n][i] = 0;
    vis[1][1] = 1;
    nowx.push(1);
    nowy.push(1);
    while(!nowx.empty())
    {
        for(int i = 0; i < 4; i++)
        {
            int nextx = nowx.front() + dx[i];
            int nexty = nowy.front() + dy[i];
            if(Map[nextx][nexty]!=1 && vis[nextx][nexty] == 0 
                && nextx >=0 && nextx <= n
                && nexty >=0 && nexty <= n)
            {
                vis[nextx][nexty] = 1;
                Map[nextx][nexty] = 0;
                nowx.push(nextx);
                nowy.push(nexty);
            }
        }
        nowx.pop();
        nowy.pop();
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        printf("%d ",Map[i][j]);
    printf("\n");
    }
    return 0;

}

|