当前位置:网站首页>【洛谷】P1162 填涂颜色(bfs)
【洛谷】P1162 填涂颜色(bfs)
2022-04-22 22:05:00 【percation】

关键对矩阵周围一圈的0进行判断哪些0和他联通,进行标记
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int,int> pii;
const int N = 2e2 + 10;
int g[N][N];
int n;
pii q[N*N];
bool st[N][N];
bool vis[N][N];
int dx[4] = {
-1,0,1,0};
int dy[4] = {
0,1,0,-1};
void bfs(int sx, int sy){
int hh = 0, tt = 0;
q[0] = {
sx,sy};
while(hh <= tt){
pii t = q[hh++];
for(int i = 0; i < 4; i++){
int a = t.x + dx[i], b = t.y + dy[i];
if(a >= 0 && a < n && b >= 0 && b < n && !st[a][b] && g[a][b] == 1){
st[a][b] = true;
q[++tt] = {
a,b};
}
}
}
}
void bfs2(int sx, int sy){
//记录与边界0联通的0
int hh = 0, tt = 0;
q[0] = {
sx,sy};
int flag = 0;
while(hh <= tt){
pii t = q[hh++];
for(int i = 0; i < 4; i++){
int a = t.x + dx[i], b = t.y + dy[i];
if(a < 0 || a >= n || b < 0 || b >= n){
//如果不从矩形的边界进行判断,加上下面注释的代码要不2测试点错,要不4测试点错
// flag = 1;
// if(g[t.x][t.y] == 0){
// vis[t.x][t.y] = true;
// }
continue;
}
// if(flag){
if(!vis[a][b] && g[a][b] == 0){
vis[a][b] = true;
q[++tt] = {
a,b};
}
// }
}
}
}
int main(){
cin >> n;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> g[i][j];
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(!st[i][j] && g[i][j] == 1){
bfs(i,j);
}
}
}
// cout << "1sdrf" << endl;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if((i == 0 || i == n - 1 || j == 0 || j == n - 1) &&!vis[i][j] && g[i][j] == 0){
bfs2(i,j);
}
}
}
// cout << "2sdrf" << endl;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(!st[i][j] && !vis[i][j]){
cout << "2" <<" ";
}
else{
cout << g[i][j]<<" ";
}
}
puts("");
}
return 0;
}
版权声明
本文为[percation]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_45866781/article/details/124355097
边栏推荐
- These two kinds of people can't do well in we media and can't make a lot of money all their life
- repeat_dijkstra
- Transport layer - connectionless transport: UDP (2)
- 秒云助力中电科32所发布“基于拟态应用集成框架的SaaS云管理平台解决方案”
- 餐饮行业收银系统源码,C# .NET + MSSQL WPF
- KunlunDB对MySQL私有DML语法的支持
- 7. Comparable to JMeter Net pressure measurement tool - crank Summary - what does crank bring
- 赛微微电上市首日破发:市值蒸发超15亿元,经营规模略输一筹
- 0基础UnityURP渲染管线之阴影ShadowCaster-ShadowMask-Map傻傻分不清楚(代码向)
- 【零散知识点总结5】
猜你喜欢

MetaWork:拜托,这样远程结对编程超酷的!

加法逆元(a^a=0)异或操作,这个并没有性能上的优势,只是一个智力游戏

Basic practice of C language (001-1)

Pushing hand of industrial Internet innovation iteration

Best buy website EDI test process

appinventor拓展开发

Reinforcement learning (practice): dqn, double dqn, dueling dqn

未来可期,PlatoFarm的生态通证登录Bitmart等全球四大平台

How to use lightly to teach programming classes gracefully?

深开鸿新闻直播间首次开播 共同见证时代成长全历程
随机推荐
vickers威格士比例阀的特点
These two kinds of people can't do well in we media and can't make a lot of money all their life
Lily medical IPO was terminated: Huang Weiguo, the father of Huang Kai, the actual controller, was once the vice mayor of Foshan
SMB+MSSQL
【4.1】flink窗口算子的trigger触发器和Evictor清理器
kubeflow创建新用户用户密码
静态和动态控制数码管
Come to a Vue boss and see if there is an iframe cache scheme?
2.60-假设我们将一个w位的字中的字节从0(最低位)到w/8- 1(最高位)编号。写出下面C函数的代码,它会返回一个无符号值,其中参数x的字节i被替换成字节b。
01 背包问题
hawe哈威液压泵站的液压冲击分析
Mathematics - Bezier curve
【Paper】2019_Distributed fixed-time consensus-based formation tracking for multiple nonholonomic whee
千亿级IM独立开发指南!全球即时通讯全套代码4小时速成(二)
41.0:GemBox.Spreadsheet|.Document|.Pdf|.Presentation
io_uring技术在分布式云原生数据库中的应用
输入一行字符,单词之间用一个空格分隔,统计其中有多少个单词
删除 vector 内所有指定的元素
【深入浅出强化学习】2 马尔可夫决策过程
Common commands of Linux