当前位置:网站首页>Informatics Orsay all in one 1335: [example 2-4] connected block
Informatics Orsay all in one 1335: [example 2-4] connected block
2022-06-27 18:59:00 【Junyi_ noip】
【 Topic link 】
ybt 1335:【 example 2-4】 Connected block
【 Topic test site 】
1. Search for : Connected block problem
【 Their thinking 】
Set array vis,vis[i][j] Express (i,j) The location has been visited .
Traverse every location in the map , Try searching from every location .
If the location is not 0 And I haven't visited , Then access the location , And try to search from its top, bottom, left and right .
When looking at a new location , If the location is within the map , Not visited and not 0, Then continue to search from this location .
In the process of traversing the grid , A successful search can determine a connected block , Count the number of connected blocks , Is the result .
The search method can be deep search or wide search .
【 Solution code 】
solution 1: Deep search
#include<bits/stdc++.h>
using namespace std;
#define N 105
int n, m, a[N][N], ans;//ans: Number of connected blocks
bool vis[N][N];//vis[i][j]:(i,j) Have you ever visited
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)
{
// If in the map 、 Never visited 、 It's black
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)// If this is black and you haven't visited
{
vis[i][j] = true;
dfs(i, j);
ans++;// Each successful deep search can determine a connected block
}
}
cout << ans;
return 0;
}
solution 2: Guang Shu
#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: Number of connected blocks
bool vis[N][N];//vis[i][j]:(i,j) Have you ever visited
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)// If this is black and you haven't visited
{
bfs(i, j);
ans++;// Each successful search can determine a connected block
}
}
cout << ans;
return 0;
}
边栏推荐
- 脉脉热帖:为啥大厂都热衷于造轮子?
- Win10 LTSC 2021 wsappx CPU 占用高
- Teach you to use elastic search: run the first hello world search command
- Android kotlin learning
- Market status and development prospect of resorcinol derivatives for skin products in the world in 2022
- Rxjs mergeMap 的使用场合
- 一位平凡毕业生的大学四年
- SQL update批量更新
- 网上期货开户安全么?
- Redis系列2:数据持久化提高可用性
猜你喜欢

实施MES管理系统前,要对哪些问题进行评估

Simple anti shake for wechat applet

数据分析师太火?月入3W?用数据告诉你这个行业的真实情况

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

Recommend several open source IOT platforms

产学合作协同育人,麒麟软件携手南开大学合力完成《软件测试与维护》实践课程

SQL update批量更新

China's Industrial Software Market Research Report is released, and SCADA and MES of force control enrich the ecology of domestic industrial software

MySQL数据库登录和退出的两种方式
![[webinar] mongodb and Google cloud accelerate enterprise digital innovation](/img/ea/4680381ce78fd5d956ed009692b424.png)
[webinar] mongodb and Google cloud accelerate enterprise digital innovation
随机推荐
别焦虑了,这才是中国各行业的工资真相
SQL update批量更新
Hikvision tools manager Hikvision tools collection (including sadp, video capacity calculation and other tools) a practical tool for millions of security practitioners
[webinar] mongodb and Google cloud accelerate enterprise digital innovation
TP5 restrict access frequency
Row to column and column to row in MySQL
IDEA 官网插件地址
Google Earth Engine(GEE)——ImageCollection (Error)遍历影像集合产生的错误
maxwell 报错(连接为mysql 8.x)解决方法
芯动联科冲刺科创板:年营收1.7亿 北方电子院与中城创投是股东
Market status and development prospect forecast of global off-road recovery rope industry in 2022
如何查看 MySQL 表的索引信息?
R language statistics a program running time function system time
Seata server database connection user and service database undo_ What permissions do log users need?
VS code 运行yarn run dev 报yarn : 无法加载文件XXX的问题
Explain in detail the differences between opentsdb and tdengine in system functions
Android kotlin learning
网上期货开户安全么?
推荐几个开源的物联网平台
电脑安全证书错误怎么处理比较好