码迷,mamicode.com
首页 > 其他好文 > 详细

填涂颜色

时间:2018-09-21 23:18:05      阅读:436      评论:0      收藏:0      [点我收藏+]

标签:class   code   ||   ret   mat   clu   ==   turn   cout   

由数字0组成的方阵中,有一任意形状闭合圈,闭合圈由数字1构成,围圈时只走上下左右4个方向。现要求把闭合圈内的所有空间都填写成2.例如:6×6的方阵(n=6),涂色前和涂色后的方阵如下:

0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1


 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 using namespace std;
 5 const int maxn=37;
 6 int n;
 7 int dx[5]={0,0,-1,1};
 8 int dy[5]={1,-1,0,0};
 9 queue<int>q1;
10 queue<int>q2;
11 int map[maxn][maxn];
12 bool vis[maxn][maxn];
13 bool check(int x,int y){
14   if(x<1||x>n||y<1||y>n) return false;
15   return true;
16 }
17 void bfs(int x,int y){
18   vis[x][y]=true;
19   q1.push(x);q2.push(y);
20   while(!q1.empty()){
21     int xx=q1.front();q1.pop();
22     int yy=q2.front();q2.pop();
23     for(int i=0;i<4;i++){
24       for(int j=0;j<4;j++){
25         int ix=xx+dx[i];int iy=yy+dy[i];
26         if(!check(ix,iy)) continue;
27         if(map[ix][iy]==0&&!vis[ix][iy]) bfs(ix,iy);
28       }
29     } 
30   }
31 }
32 int main(){
33   cin>>n;
34   for(int i=1;i<=n;i++){
35     for(int j=1;j<=n;j++){
36       cin>>map[i][j];
37       if(map[i][j]==1) vis[i][j]=true;
38     }
39   }
40   for(int i=1;i<=n;i=i+n-1){
41     for(int j=1;j<=n;j++){
42       if(!vis[i][j]) bfs(i,j);
43     }
44   }
45   for(int i=1;i<=n;i+=n-1){
46     for(int j=1;j<=n;j++){
47       if(!vis[j][i]) bfs(j,i);
48     }
49   }
50   for(int i=1;i<=n;i++){
51     for(int j=1;j<=n;j++){
52       if(map[i][j]==1) cout<<1<<" ";
53       if(map[i][j]==0){
54         if(vis[i][j]) cout<<0<<" ";
55         else cout<<2<<" ";
56       }
57     }
58     cout<<endl;
59   }
60   return 0;
61 }

 

填涂颜色

标签:class   code   ||   ret   mat   clu   ==   turn   cout   

原文地址:https://www.cnblogs.com/lcan/p/9688656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!