2521: 【普及-】P1162 填涂颜色
内存限制:128 MB
时间限制:1.000 S
评测方式:文本比较
命题人:
提交:8
解决:5
题目描述
由数字 组成的方阵中,有一任意形状闭合圈,闭合圈由数字 构成,围圈时只走上下左右 个方向。现要求把闭合圈内的所有空间都填写成 。例如: 的方阵(),涂色前和涂色后的方阵如下:
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
输入
每组测试数据第一行一个整数 。
接下来 行,由 和 组成的 的方阵。
方阵内只有一个闭合圈,圈内至少有一个 。
输出
已经填好数字 的完整方阵。
样例输入 复制
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
提示
对于 的数据,。
#include<bits/stdc++.h> using namespace std; struct point{ int x,y; }; int n,land[35][35]={0},mark[35][35]={0},tmp; int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1}; queue<point> q; int main(){ cin>>n; for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) { cin>>tmp; if(tmp) land[i][j]=1; } } mark[0][0]=1; q.push(point{0,0}); while(!q.empty()) { point p=q.front(); q.pop(); for (int i=0;i<4;i++) { int x=p.x+dx[i],y=p.y+dy[i]; if(x<0||x>n+1||y<0||y>n+1) continue; if (land[x][y]==0&&mark[x][y]==0) { mark[x][y]=1; q.push(point{x,y}); } } } for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) { if (land[i][j]==0&&mark[i][j]==0) { cout<<2<<" "; }else if(land[i][j]==1) { cout<<1<<" "; }else { cout<<0<<" "; } } cout<<endl; } return 0; }
#include<bits/stdc++.h> using namespace std; int n,land[35][35]={0},mark[35][35]={0},tmp; int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1}; void dfs(int x,int y) { for(int i=0;i<4;i++) { int tx=x+dx[i],ty=y+dy[i]; if (tx<0||tx>n+1||ty<0||ty>n+1) continue; if (land[tx][ty]==0&&mark[tx][ty]==0) { mark[tx][ty]=1; dfs(tx,ty); } } } int main(){ cin>>n; for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) { cin>>tmp; if(tmp) land[i][j]=1; } } dfs(0,0); for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) { if (land[i][j]==0&&mark[i][j]==0) { cout<<2<<" "; }else if(land[i][j]==1) { cout<<1<<" "; }else { cout<<0<<" "; } } cout<<endl; } return 0; }