当前位置:网站首页>信息学奥赛一本通 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;
}
边栏推荐
- Good news - British software 2022 has obtained 10 invention patents!
- 如何制作登录界面
- Explain in detail the differences between opentsdb and tdengine in system functions
- MySQL数据库登录和退出的两种方式
- MySQL读取Binlog日志常见错误和解决方法
- 利用OpenCV执行相机校准
- The first in China! EMQ joined Amazon cloud technology's "startup acceleration - global partner network program"
- Application of scaleflux CSD 2000 in Ctrip
- Optimal binary search tree
- If the storage engine of time series database wants to be the best, it has to do its own research
猜你喜欢

Row to column and column to row in MySQL

Recommend several open source IOT platforms

技术分享 | kubernetes pod 简介

Rxjs mergeMap 的使用场合

【网络研讨会】MongoDB 携手 Google Cloud 加速企业数字化创新

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

2022年信创行业空间测算

Tdengine connector goes online Google Data Studio store

Wanzhou gold industry: what are the differences between gold t+d investment and other investments?

Industry university cooperation cooperates to educate people, and Kirin software cooperates with Nankai University to complete the practical course of software testing and maintenance
随机推荐
On array-_-
脉脉热帖:为啥大厂都热衷于造轮子?
OpenSSF 安全计划:SBOM 将驱动软件供应链安全
「技术课堂」如何用 VSCode 从 0 到 1 改写 TDengine 代码
xctf攻防世界 MISC薪手进阶区
Push NFT out of the regulatory dilemma, and BSN launched NFT supporting infrastructure network
数据分析师太火?月入3W?用数据告诉你这个行业的真实情况
How to turn off the server terminal name of vscode
Teach you how to install Oracle 19C on Windows 10 (detailed picture and text with step on pit Guide)
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
Teach you to use elastic search: run the first hello world search command
Campus book resource sharing platform
If the storage engine of time series database wants to be the best, it has to do its own research
手把手教你在Windows 10安装Oracle 19c(详细图文附踩坑指南)
MFS分布式文件系统
Galaxy Kirin V10 system activation
Stored procedures of PostgreSQL
Analysis of shardingsphere core source code
China's Industrial Software Market Research Report is released, and SCADA and MES of force control enrich the ecology of domestic industrial software
Recommend several open source IOT platforms