当前位置:网站首页>-填涂颜色-
-填涂颜色-
2022-08-11 04:36:00 【-JMY-】
题目描述
由数字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
输入
每组测试数据第一行一个整数n(1≤n≤30)
接下来n行,由0和1组成的n×n 的方阵。
方阵内只有一个闭合圈,圈内至少有一个0。
输出
已经填好数字2的完整方阵。
样例输入
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≤n≤30
参考代码:
#include<bits/stdc++.h>
using namespace std;
int n;
int q[22500][2],hh,tt,kx,ky,gx,gy;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int d[150][150];
char a[150][150];
bool b[150][150];
bool around(int y,int x){
bool s=true;
if(y<=0||y>=n-1||x<=0||x>=n-1)
return false;
b[y][x]=true;
if(a[y+1][x]=='0'&&!b[y+1][x])
s=s&&around(y+1,x);
if(a[y][x+1]=='0'&&!b[y][x+1])
s=s&&around(y,x+1);
if(a[y-1][x]=='0'&&!b[y-1][x])
s=s&&around(y-1,x);
if(a[y][x-1]=='0'&&!b[y][x-1])
s=s&&around(y,x-1);
return s;
}
void bfs(){
while(hh!=tt){
kx=q[hh][1];
ky=q[hh][0];
a[ky][kx]='2';
hh++;
for(int i=0;i<4;i++){
int xx=kx+dx[i];
int yy=ky+dy[i];
if(xx>=0&&xx<n&&yy>=0&&yy<n&&a[yy][xx]=='0'&&d[yy][xx]==0){
tt++;
q[tt-1][0]=yy;
q[tt-1][1]=xx;
d[yy][xx]=d[ky][kx]+1;
}
}
}
return;
}
int main(){
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(!b[i][j]&&a[i][j]=='0')
if(around(i,j)){
q[0][0]=i;
q[0][1]=j;
d[i][j]=1;
tt++;
bfs();
for(int i2=0;i2<n;i2++){
for(int j2=0;j2<n;j2++)
cout<<a[i2][j2]<<' ';
cout<<'\n';
}
return 0;
}
return 0;
}
边栏推荐
- Harvesting of radio frequency energy
- Solve the problem of multi-thread calling sql stored procedure
- 关于数据分页显示
- 2022新员工公司级安全教育基础培训(118页)
- 增加PRODUCT_BOOT_JARS及类 提供jar包给应用
- Provincial level of Echart maps, as well as all prefecture-level download and use
- 无线电射频能量的收集
- 洛谷P6586 蒟蒻火锅的盛宴
- LeetCode刷题第17天之《3 无重复字符的最长子串》
- 洛谷P4847 银河英雄传说V2
猜你喜欢
使用jackson解析json数据详讲
How to learn machine learning?machine learning process
LeetCode刷题第12天二叉树系列之《104 二叉树的最大深度》
洛谷P2150 寿司晚宴
What is machine learning?Explain machine learning concepts in detail
"3 Longest Substring Without Repeating Characters" on the 17th day of LeetCode brushing
一文读懂 高性能可预期数据中心网络
To break the bottleneck of transactional work, the gentleman signs the electronic contract to release the "source power" of HR!
JVM 垃圾回收的概述与机制
LeetCode刷题第10天字符串系列之《125回文串验证》
随机推荐
力扣——青蛙跳台阶问题
map和set--天然的搜索和查找语义
洛谷P2245 星际导航
Callable实现多线程
使用jackson解析json数据详讲
Mysql中事件和定时任务
直播软件搭建,流式布局,支持单选、多选等
MQ框架应用比较
【ImageNet】数据集1000个类的名称
uni-app - city selection index list / city list sorted by A-Z (uview component library IndexList index list)
自研能力再获认可,腾讯云数据库入选 Forrester Translytical 报告
如何给网页添加icon图标?
Jetson Orin平台4-16路 GMSL2/GSML1相机采集套件推荐
【小记】BatchSize的数值是设置的越大越好吗
The FTP error code list
【Web3 系列开发教程——创建你的第一个 NFT(9)】如何在手机钱包里查看你的 NFT
洛谷P4032 火锅盛宴
Object Creation and Display Transformation
ALSA音频架构 -- snd_pcm_open函数分析
JVM 垃圾回收的概述与机制