当前位置:网站首页>信息学奥赛一本通 1335:【例2-4】连通块
信息学奥赛一本通 1335:【例2-4】连通块
2022-06-27 16:35:00 【君义_noip】
【题目链接】
【题目考点】
1. 搜索:连通块问题
【解题思路】
设数组vis,vis[i][j]表示(i,j)位置已经访问过。
遍历地图中的每个位置,尝试从每个位置开始进行搜索。
如果该位置不是0且没有访问过,那么访问该位置,并尝试从其上下左右四个位置开始搜索。
在看一个新的位置时,如果该位置在地图内,没有访问过且不是0,那么继续从该位置开始进行搜索。
在遍历网格的过程中,一次成功开始的搜索可以确定一个连通块,统计连通块的个数,即为结果。
搜索方法可以采用深搜或广搜。
【题解代码】
解法1:深搜
#include<bits/stdc++.h>
using namespace std;
#define N 105
int n, m, a[N][N], ans;//ans:连通块个数
bool vis[N][N];//vis[i][j]:(i,j)是否访问过
int dir[4][2] = {
{
0, 1}, {
0, -1}, {
1, 0}, {
-1, 0}};
void dfs(int sx, int sy)
{
for(int i = 0; i < 4; ++i)
{
int x = sx + dir[i][0], y = sy + dir[i][1];
if(x >= 1 && x <= n && y >= 1 && y <= m && vis[x][y] == false && a[x][y] == 1)
{
//如果在地图内、没访问过、是黑色的
vis[x][y] = true;
dfs(x, y);
}
}
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
cin >> a[i][j];
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
{
if(a[i][j] == 1 && vis[i][j] == false)//如果这里是黑色的且没访问过
{
vis[i][j] = true;
dfs(i, j);
ans++;//每次成功进行深搜可以确定一个连通块
}
}
cout << ans;
return 0;
}
解法2:广搜
#include<bits/stdc++.h>
using namespace std;
#define N 105
struct Node
{
int x, y;
Node(){
}
Node(int a, int b):x(a),y(b){
}
};
int n, m, a[N][N], ans;//ans:连通块个数
bool vis[N][N];//vis[i][j]:(i,j)是否访问过
int dir[4][2] = {
{
0, 1}, {
0, -1}, {
1, 0}, {
-1, 0}};
void bfs(int sx, int sy)
{
queue<Node> que;
vis[sx][sy] = true;
que.push(Node(sx,sy));
while(que.empty() == false)
{
Node u = que.front();
que.pop();
for(int i = 0; i < 4; ++i)
{
int x = u.x + dir[i][0], y = u.y + dir[i][1];
if(x >= 1 && x <= n && y >= 1 && y <= m && vis[x][y] == false && a[x][y] == 1)
{
vis[x][y] = true;
que.push(Node(x,y));
}
}
}
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
cin >> a[i][j];
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
{
if(a[i][j] == 1 && vis[i][j] == false)//如果这里是黑色的且没访问过
{
bfs(i, j);
ans++;//每次成功进行广搜可以确定一个连通块
}
}
cout << ans;
return 0;
}
边栏推荐
- im即时通讯开发之双进程守护保活实践
- 银河麒麟V10系统激活
- 原创 | 2025实现“5个1”奋斗目标!解放动力全系自主非道路国四产品正式发布
- 推荐几个开源的物联网平台
- Shardingsphere sharding proxy actual combat scenario
- 利用OpenCV执行相机校准
- Hi,你有一份Code Review攻略待查收!
- Market status and development prospect of 4-butyl resorcinol used in skin care industry in the world in 2022
- Oracle的NUMBER长度超过19之后,实体使用Long映射,导致出现问题是为什么?
- [Tang Laoshi] C -- encapsulation: member method
猜你喜欢

Set up your own website (10)

SQL update批量更新

im即时通讯开发之双进程守护保活实践

TDengine在数控机床监控中的应用

Contest3182 - the 39th individual training match for 2021 freshmen_ E: ringring

laravel框架中 定时任务的实现

【协会通知】关于举办人工智能与物联网领域暑假专题师资培训的通知

VSCode 建议你启用 gopls,它到底是个什么东东?

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

国内首家!EMQ加入亚马逊云科技“初创加速-全球合作伙伴网络计划”
随机推荐
电脑安全证书错误怎么处理比较好
Asemi rectifier bridge kbp210 parameters, kbp210 specifications, kbp210 dimensions
Campus book resource sharing platform
Wechat applet payment countdown
The computer voice is blurry, the video is also out of the card, and the sound cannot be played. It is normal to insert the headset
Common errors and solutions of MySQL reading binlog logs
阅文、中文在线等网文平台如何布局数字藏品?未来是否会推出“Read/Write-to-Earn”产品?
如何实现IM即时通讯“消息”列表卡顿优化
Ansible environment installation and data recovery
Set up your own website (10)
Good news - British software 2022 has obtained 10 invention patents!
Rxjs mergeMap 的使用场合
On array-_-
脉脉热帖:为啥大厂都热衷于造轮子?
Application of scaleflux CSD 2000 in Ctrip
Technology sharing | introduction to kubernetes pod
Contest3182 - the 39th individual training match for 2021 freshmen_ F: ss
ansible环境安装及数据恢复
Teach you to use elastic search: run the first hello world search command
[Tang Laoshi] C -- encapsulation: member method