当前位置:网站首页>Planning garlic guest (outing) (DFS and BFS solution of dyeing block problem)
Planning garlic guest (outing) (DFS and BFS solution of dyeing block problem)
2022-04-23 01:25:00 【OldLeft】
Garlic king and his friends are going to Summoner Canyon for an outing at the weekend . They found that the map of Summoner canyon was made up of squares , Some lattices are covered with grass , There is plenty of open space . The grass passes up, down, left and right 4 Expand other grasses in one direction to form a grassland , The grid in any piece of grass is grass , And all grids can be connected up, down, left and right . If you use ’#‘ Stands for grass ,’.' On behalf of the open space , There are... In the canyon below 2 A piece of grass .
##..
..##
In the same grass 2 Individuals can see each other , The open space can't see the people in the grass . They found a friend missing , Now we need to find it separately , Everyone is responsible for a piece of grass , Garlic king wants to know how many people they need at least .
Input format
First line input n, m (1 <= n,m <= 100)(1≤n,m≤100) Indicates the size of the Canyon .
Next input n The line string represents the terrain of the Canyon .
Input format
At least how many people are needed to output .
The sample input
5 6
.#....
..#...
..#..#
...##.
.#....
Sample output
5
DFS solution
#include<bits/stdc++.h>
using namespace std;
int n,m;
char s[105][105];
bool vis[105][105];
int ans;
int dir[4][2]={
{
1,0},{
-1,0},{
0,1},{
0,-1}};
void dfs(int x,int y){
vis[x][y]=true;
for(int i=0;i<4;i++){
int tx=x+dir[i][0];
int ty=y+dir[i][1];
if(tx<n&&tx>=0&&ty<m&&ty>=0&&!vis[tx][ty]&&s[tx][ty]=='#'){
dfs(tx,ty);
}
}
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
scanf("%s",s[i]);
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(!vis[i][j]&&s[i][j]=='#'){
dfs(i,j);
ans++;
}
}
}
cout<<ans<<endl;
return 0;
}
BFS solution
#include<bits/stdc++.h>
using namespace std;
char s[105][105];
bool vis[105][105];
int dir[4][2]={
{
1,0},{
-1,0},{
0,-1},{
0,1}};
int n,m,ans;
struct Node{
int x,y;
};
void bfs(int x,int y){
queue<Node> q;
q.push({
x,y});
vis[x][y]=true;
while(!q.empty()){
Node now=q.front();
q.pop();
for(int i=0;i<4;i++){
int tx=now.x+dir[i][0];
int ty=now.y+dir[i][1];
if(tx<n&&tx>=0&&ty<m&&ty>=0&&!vis[tx][ty]&&s[tx][ty]=='#'){
q.push({
tx,ty});
vis[tx][ty]=true;
}
}
}
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>s[i];
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(!vis[i][j]&&s[i][j]=='#'){
bfs(i,j);
ans++;
}
}
}
cout<<ans<<endl;
return 0;
}
版权声明
本文为[OldLeft]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230120462771.html
边栏推荐
- 10月24号是什么?中国的高级程序员为什么没有五六十岁的,而国外人、、、,玉念聿辉带你过1024程序员节日
- Software maintenance exercises
- Project manager's thinking mode worth trying: project success equation
- Gbase 8s Group by 功能介绍
- 清研环境深交所上市:年营收1.8亿 市值41亿
- Introduction and management of gbase 8s database log
- From thinking to practice, digital transformation is the successful path of it operation
- Is it difficult for girls to learn software testing?
- 最新流程引擎 flowable 6.7.2 更新说明
- Is 2022 software testing easy to learn? How long will it take? (learning roadmap attached)
猜你喜欢

Get in the car, the era of intelligent database autonomy has come, and Tencent cloud database x AI has made a new breakthrough

JD side: how can a child thread get the value of the parent thread ThreadLocal? I got...

In the context of Internet plus, how can enterprises innovate customer service?

Configuration of imx6ull bare metal development analysis and configuration process of elcdif lighting RGB LCD

gin--hello

京东一面:子线程如何获取父线程 ThreadLocal 的值?我蒙了。。。

Application of safe electricity management platform in Jingbian Museum safe electricity management system

Activity preview | on April 23, a number of wonderful openmldb sharing came, which lived up to the good time of the weekend!

无关联进程间通信 —— 命名管道的创建和使用

Three technical solutions of ant group were selected as "typical solutions for information technology application and innovation in 2021"
随机推荐
After ten years of testing experience, I have sorted out the most suitable software testing learning guide for you
Analysis and configuration process of epit periodic timer for imx6ull bare metal development
Text justify, orientation, combine text attributes
GBASE 8s分片表管理操作
Gbase 8s客户端与服务器的通信
Gbase 8s shared memory segment deletion
Generating class diagram with EA reverse engineering code
Software maintenance exercises
Research and application of power monitoring system in sports training center
Introduction to gbase 8s storage structure and space management
GBase 8t索引
What is October 24th? Why are there no senior programmers in China in their fifties and sixties, while foreigners,,, Yu Nianyu Hui take you to celebrate the 1024 programmer Festival
清研环境深交所上市:年营收1.8亿 市值41亿
World reading day: 18 it books with Douban score above 9.0 are worth collecting
计蒜客:方程的解数
Originally, this is the correct posture for developers to open world book day!
Ampere computing releases the computing power of observation cloud "core" and jointly promotes the development of observability
Abused "architect"!
Huawei CDN is fast everywhere
iTextSharp 基础结构