第1和第5Ac,其他wa,有人帮我看看嘛

P1162 填涂颜色

kikis @ 2024-08-20 22:32:09


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

int main() {
    int n, a[40][40];
    cin >> n;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            cin >> a[i][j];
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if (a[i][j] == 0){
                if (i == 1 || i == n || j == 1 ||j == n){
                    a[i][j] = 3;
                    continue;
                }
                if (a[i-1][j] == 3 || a[i+1][j] == 3 || a[i][j-1] == 3 || a[i][j+1] == 3){
                    a[i][j] = 3;
                    continue;
                }
            }

        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if (a[i][j] == 3){
                cout << 0 << " ";
                continue;
            }
            if (a[i][j] == 0){
                cout << 2 << " ";
                continue;
            }else{
                cout << 1 << " ";
                continue;
            }
        }
        cout << endl;
    }
    return 0;
}

by haimingbei @ 2024-08-20 22:52:13

@kikis 你的没看懂

深搜:

/*
1、P1162 填涂颜色
*/
#include<bits/stdc++.h>
using namespace std;
int a[40][40],n,b[40][40];
int xp[5]={0,1,0,-1,0};
int yp[5]={0,0,1,0,-1};
void dfs(int x,int y){
    if(x<0 || x>n+1 || y<0 || y>n+1 || b[x][y]!=0)return ;
    b[x][y]=1;
    for(int i=1;i<=4;i++)dfs(x+xp[i],y+yp[i]);
}
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]=2;
        }
    }
    dfs(0,0);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(b[i][j]==0)cout<<2<<" ";
            else cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

by kikis @ 2024-08-21 09:32:06

@haimingbei 我的是简单染色。在1外圈染成3,最后分别输出。


by dyh_120 @ 2024-08-21 22:36:48

@kikis 只考虑四个角是不行的,外层一圈都要考虑

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

const int N=35;
int n,mp[N][N];
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};

bool check(int x,int y){
    return x>0 && x<=n && y>0 && y<=n && mp[x][y]==0;
}

void dfs(int x,int y){
    if (mp[x][y]==1) return;
    for (int i=0;i<=4;i++){
        int dx=x+dir[i][0];
        int dy=y+dir[i][1];
        if (check(dx,dy)){
            mp[dx][dy]=-1;
            dfs(dx,dy);
        }

    }
}

int main (){
    cin >> n;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            cin >> mp[i][j]; 
    for (int i=1;i<=n;i++){
        if (mp[i][n]==0) dfs(i,n);
        if (mp[i][1]==0) dfs(i,1);
        if (mp[1][i]==0) dfs(1,i);
        if (mp[n][i]==0) dfs(n,i);
    } 
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            if (mp[i][j]==-1)
                cout << 0 << " ";
            else if (mp[i][j]==1)
                cout << 1 << " ";
            else cout << 2 << " ";
        }
        cout << endl;
    }
    return 0;
}

看30到35行


by kikis @ 2024-08-24 15:46:43

@dyh_120 谢谢谢谢


by dyh_120 @ 2024-08-24 16:12:36

@kikis 不谢,求关QWQ


|