Luo Gu P1162 TianTu color (bfs)

https://www.luogu.org/problem/P1162

Thinking: FIG save up from 1-n, the outside circle filled with zeros, and (0,0) began searching

// luogu-judger-enable-o2
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <cstring>
#include <map>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long lll;
const int maxn = 200005;
const lll INF = 0x3f3f3f3f3f;
int dir[8][2]= {0,1,0,-1,1,0,-1,0,-1,-1,1,-1,-1,1,1,1};
int dir2[4][2]= {0,1,0,-1,1,0,-1,0};
bool flag;
int a[35][35],b[35][35],n;
struct node{
    int x,y;
    node(){};
    node(int xx,int yy):x(xx),y(yy){};
};

void bfs(int x,int y)
{
    queue<node>q;
    q.push(node(0,0));//(0,0)一定是0
    while(!q.empty()){
        int fx = q.front().x,fy = q.front().y;
        q.pop();
        b[fx][fy] = 3 ; // The out-labeled 0 3 
        for ( int I = 0 ; I < . 4 ; I ++ ) 
        { 
            int EX dir2 = FX + [I] [ 0 ], FY + EY = dir2 [I] [ . 1 ];
             IF (EX < 0 || EX> n-+ . 1 || EY < 0 || EY> n-+ . 1 || B [EX] [EY] == . 1 || B [EX] [EY] == . 3 )
                 Continue ; 
            q.push (Node (EX, EY)); 
        } 
    } 
} 
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] = 2;
        else b[i][j] = 1;
    }
    bfs(0,0);
    for(int i = 1; i <= n; i++){
        for(int j =1; j <= n; j++){
            if(b[i][j] == 3 ) cout << "0";
        else
                cout << b[i][j];
            if(j < n) cout << " ";
            else cout << endl;
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/LLLAIH/p/11294038.html