当前位置:网站首页>-Fill in color-
-Fill in color-
2022-08-11 04:42: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;
}
边栏推荐
猜你喜欢

无线电射频能量的收集

【FPGA】abbreviation

使用jackson解析json数据详讲

leetcode刷题第13天二叉树系列之《98 BST及其验证》

Object Creation and Display Transformation
![[C Language] Getting Started](/img/5e/484e3d426a6f1cc0d792a9ba330695.png)
[C Language] Getting Started

I wrote some code in my resume, but I still can't pass the interview

LeetCode Brush Questions Day 11 String Series "58 Last Word Length"

JVM 垃圾回收的概述与机制

干货:服务器网卡组技术原理与实践
随机推荐
What is machine learning?Explain machine learning concepts in detail
Dry goods: The principle and practice of server network card group technology
解决多线程调用sql存储过程问题
洛谷P4847 银河英雄传说V2
JVM 垃圾回收的概述与机制
Add PRODUCT_BOOT_JARS and classes to provide jar packages to applications
洛谷P2150 寿司晚宴
"110 Balanced Binary Tree Judgment" in leetCode's 14-day binary tree series
Map中的getOrDefualt方法
如何将360全景图导出高清短视频分享到视频平台上?
Alibaba Cloud releases 3 high-performance computing solutions
梅克尔工作室--OpenEuler培训笔记(1)
Redis deletes keys in batches according to regular rules
0 Basic software test for career change, self-study for 3 months, 12k*13 salary offer
c语言fprintf、fscanf、sscanf以及sprintf函数知识要点总结
Redis:解决分布式高并发修改同一个Key的问题
The principle, architecture, implementation, practice of "transfer" and "search", no need to be afraid of interviews
Mysql: set the primary key to automatically increase the starting value
使用百度EasyDL实现施工人员安全装备检测
I wrote some code in my resume, but I still can't pass the interview