求调

P1162 填涂颜色

lizihangrq @ 2024-10-13 09:42:44

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#define N 35
//#include<cmath>
using namespace std;
int n,a[N][N],tmp[N][N];
int X[]={0,1,-1,0,0};
int Y[]={0,0,0,-1,1}; 
void dfs(int x,int y){
    for(int i=1;i<=4;i++){
        int idx=x+X[i],idy=y+Y[i];
        if(idx<1||idx>n||idy<1||idy>n||tmp[idx][idy]!=0) return ;
        dfs(idx,idy);
        tmp[idx][idy]=1;
    }
    return ;
}
int main(){
//  freopen("GRQ.in","r",stdin);
//  freoprn("GRQ.out","w",stdout);
    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++){
        if(a[i][1]==0) dfs(i,1);
        if(a[i][n]==0) dfs(i,n);
    }
    for(int i=1;i<=n;i++){
        if(a[1][i]==0) dfs(1,i);
        if(a[n][i]==0) dfs(n,i);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(tmp[i][j]==0){
                printf("2 ");
                continue;
            }
            printf("%d ",a[i][j]);
        }
        puts("");
    }
    return 0;
}

by fyn1234 @ 2024-10-27 09:22:17

\color{white}\texttt{qp}

|