当前位置:网站首页>信息学奥赛一本通 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;
}
边栏推荐
- How to rewrite tdengine code from 0 to 1 with vscode in "technical class"
- After the number length of Oracle exceeds 19, the entity uses long mapping. Why does this cause problems?
- Simple anti shake for wechat applet
- im即时通讯开发之双进程守护保活实践
- Seata中 SelectForUpdateExecutor 可以先获取全局锁,再执行 sql吗?
- [elt.zip] openharmony paper Club - witness file compression system erofs
- Market status and development prospect forecast of global epoxy resin active toughener industry in 2022
- Ansible environment installation and data recovery
- Asemi rectifier bridge kbp307 parameters, kbp307 details, kbp307 pictures
- How to create a login interface
猜你喜欢
![[notice of the Association] notice on holding summer special teacher training in the field of artificial intelligence and Internet of things](/img/ef/5b86170e60a7e03c4db512445540b9.jpg)
[notice of the Association] notice on holding summer special teacher training in the field of artificial intelligence and Internet of things

Wechat applet map displays the current position with annotation

Asemi rectifier bridge kbp210 parameters, kbp210 specifications, kbp210 dimensions

Row to column and column to row in MySQL

Recommend several open source IOT platforms

原创 | 2025实现“5个1”奋斗目标!解放动力全系自主非道路国四产品正式发布

2022年信创行业空间测算

中国工业软件市场研究报告出炉,力控SCADA、MES丰富国产工业软件生态

Wechat applet payment countdown
![Contest3182 - the 39th individual training match for 2021 freshmen_ C: [string] ISBN number](/img/98/11ca12889a1b653ce8158920bf5136.jpg)
Contest3182 - the 39th individual training match for 2021 freshmen_ C: [string] ISBN number
随机推荐
[webinar] mongodb and Google cloud accelerate enterprise digital innovation
Application of tdengine in monitoring of CNC machine tools
Seata server database connection user and service database undo_ What permissions do log users need?
Contest3182 - the 39th individual training match for 2021 freshmen_ F: ss
The power of code refactoring: how to measure the success of refactoring
云原生数据库:数据库的风口,你也可以起飞
Mise à jour SQL mise à jour par lots
Wanzhou gold industry: what are the common gold investment and warehouse building modes?
SQL update批量更新
Explain in detail the differences between opentsdb and tdengine in system functions
Wechat applet payment countdown
Why migrate from opentsdb to tdengine
Differences between mongodb and MySQL
Summary of domestic database certification test guide (updated on June 16, 2022)
Hi,你有一份Code Review攻略待查收!
After the number length of Oracle exceeds 19, the entity uses long mapping. Why does this cause problems?
SQL update batch update
TIA博途_基于SCL语言制作模拟量输入输出全局库的具体方法
Wanzhou gold industry: what are the differences between gold t+d investment and other investments?
【网络研讨会】MongoDB 携手 Google Cloud 加速企业数字化创新