tle on #4

P1162 填涂颜色

esquigybcu @ 2021-06-19 15:55:43

#include <stdio.h>

const int SIZE = 35;
int qwq[SIZE][SIZE], vis[SIZE][SIZE], mark[SIZE][SIZE], n;

inline bool can_search(int i, int j)
{
    if (i < 0 || i > n + 1 || j < 0 || j > n + 1)
        return false;
    return (qwq[i][j] == 0) && !vis[i][j];
}

void dfs(int i, int j)
{
    if (!can_search(i, j))
        return;
    vis[i][j] = true;
    mark[i][j] = true;
    dfs(i + 1, j    );
    dfs(i - 1, j    );
    dfs(i    , j + 1);
    dfs(i    , j - 1);
    vis[i][j] = false;
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            scanf("%d", &qwq[i][j]);
    dfs(0, 0);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
            if (mark[i][j])
                printf("0 ");
            else if (qwq[i][j])
                printf("1 ");
            else
                printf("2 ");
        printf("\n");
    }
    return 0;
}

by esquigybcu @ 2021-06-19 15:56:14

@高远哲 @fast_photon


by zhangruozhong @ 2021-06-19 15:56:51

#include<bits/stdc++.h>
using namespace std;
const int maxn=50;
struct node{
    int x,y;
}p;
int n,a[maxn][maxn];
int X[4]={0,0,1,-1};
int Y[4]={1,-1,0,0};
bool inq[maxn][maxn]={false};
bool judge(int x,int y){
    if(x<0||x>n+1||y<0||y>n+1) return false;
    else if(inq[x][y]==true||a[x][y]==1) return false;
    return true;
}
void BFS(){
    queue<node> q;
    q.push(p);
    while(!q.empty()){
        node top=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int newX=top.x+X[i];
            int newY=top.y+Y[i];
            if(judge(newX,newY)){
                node s;
                s.x=newX,s.y=newY;
                q.push(s);
                inq[newX][newY]=true;
            }
        }
    }
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
        }
    }
    p.x=0,p.y=0;
    BFS();
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(a[i][j]!=0) inq[i][j]=true;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(inq[i][j]==false) cout<<2<<" ";
            else cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
} 

by esquigybcu @ 2021-06-19 16:10:35

ACed. 把dfs里的vis[i][j] = false;删掉就行。


|