当前位置:网站首页>信息学奥赛一本通 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;
}
边栏推荐
- Rxjs mergeMap 的使用场合
- R language statistics a program running time function system time
- MongoDB和MySQL的区别
- Good news - British software 2022 has obtained 10 invention patents!
- 网上期货开户安全么?
- seata性能可以通过什么方式提高?比如增加数据库的计算节点?
- 如何制作登录界面
- Two methods of MySQL database login and logout
- 云原生数据库:数据库的风口,你也可以起飞
- Seata server database connection user and service database undo_ What permissions do log users need?
猜你喜欢

Hikvision tools manager Hikvision tools collection (including sadp, video capacity calculation and other tools) a practical tool for millions of security practitioners

PostgreSQL database Wal - resource manager rmgr

Lvgl8.x migrating to stm32f4

TP5 generates the most detailed two-dimensional code tp6 (also available)

MySQL数据库登录和退出的两种方式

Asemi rectifier bridge kbp307 parameters, kbp307 details, kbp307 pictures

在arcgis中以txt格式导出点的坐标

Good news - British software 2022 has obtained 10 invention patents!

How to use the low code platform of the Internet of things for picture management?

Vscode suggests that you enable gopls. What exactly is it?
随机推荐
国信证券是国企吗?在国信证券开户资金安全吗?
Alibaba's mission, vision and core values
使用 WebDAV 替代445端口文件共享
Redis系列2:数据持久化提高可用性
Is Guosen Securities a state-owned enterprise? Is it safe to open an account with Guosen Securities?
Win10 LTSC 2021 wsappx CPU 占用高
Camera calibration with OpenCV
[webinar] mongodb and Google cloud accelerate enterprise digital innovation
Wechat applet map displays the current position with annotation
Industry university cooperation cooperates to educate people, and Kirin software cooperates with Nankai University to complete the practical course of software testing and maintenance
Technology sharing | introduction to kubernetes pod
电脑安全证书错误怎么处理比较好
Keras深度学习实战(12)——面部特征点检测
Oracle的NUMBER长度超过19之后,实体使用Long映射,导致出现问题是为什么?
Seata中 SelectForUpdateExecutor 可以先获取全局锁,再执行 sql吗?
推荐几个开源的物联网平台
[Tang Laoshi] C -- encapsulation: member method
Contest3182 - the 39th individual training match for 2021 freshmen_ F: ss
Recommend several open source IOT platforms
If the storage engine of time series database wants to be the best, it has to do its own research