当前位置:网站首页>信息学奥赛一本通 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;
}
边栏推荐
- Current market situation and development prospect forecast of global concrete shrinkage reducing agent industry in 2022
- 写在eclipse里面,与数据库连接查询之后的问题
- 网上期货开户安全么?
- 新产品新人事新服务,英菲尼迪继续深耕中国未来可期!
- IDEA 官网插件地址
- Analysis of shardingsphere core source code
- New products, new personnel and new services, Infiniti will continue to plough into China's future!
- [notice of the Association] notice on holding summer special teacher training in the field of artificial intelligence and Internet of things
- 产学合作协同育人,麒麟软件携手南开大学合力完成《软件测试与维护》实践课程
- How to use the low code platform of the Internet of things for picture management?
猜你喜欢

Allocate aligned heap space

laravel框架中 定时任务的实现

How to view the index information of MySQL tables?

MySQL中的行转列和列转行

JS event binding and common events

Two methods of MySQL database login and logout

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

清华徐勇、段文晖研究组开发出高效精确的第一性原理电子结构深度学习方法与程序

New products, new personnel and new services, Infiniti will continue to plough into China's future!
![[UVM foundation] UVM_ Is in agent_ Active variable definition](/img/55/a14fbde43b25ccdc5a7996121396f9.jpg)
[UVM foundation] UVM_ Is in agent_ Active variable definition
随机推荐
[UVM basics] UVM tree organizational structure
2022年信创行业空间测算
破解仓储难题?WMS仓储管理系统解决方案
Daily leetcode force deduction (31~35)
How to rewrite tdengine code from 0 to 1 with vscode in "technical class"
Good news - British software 2022 has obtained 10 invention patents!
Recommend several open source IOT platforms
Ansible environment installation and data recovery
数据分析师太火?月入3W?用数据告诉你这个行业的真实情况
如何实现IM即时通讯“消息”列表卡顿优化
Three methods to quickly open CMD in a specified folder or place
InfluxDB集群功能不再开源,TDengine集群功能更胜一筹
Oracle的NUMBER长度超过19之后,实体使用Long映射,导致出现问题是为什么?
[Tang Laoshi] C -- encapsulation: member method
阿里巴巴的使命、愿景、核心价值观
Wanzhou gold industry: what are the common gold investment and warehouse building modes?
TP5 restrict access frequency
开源之夏 2022 | openGauss 项目中选公布
Summary of domestic database certification test guide (updated on June 16, 2022)
Stored procedures of PostgreSQL