【poj】2386 Lake Counting bfs||dfs
来源:互联网 发布:mac os beta降级 编辑:程序博客网 时间:2024/06/10 01:12
链接
大大大大大水题…
题意没什么好解释的,注意可以往八个方向扩展就可以了…
可用bfs和dfs两种姿势水过。!
上边是bfs的用时,下面是dfs的用时
放上这两种姿势…
dfs:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN=100+10;char maps[MAXN][MAXN];int n,m;void dfs(int x,int y){ maps[x][y]='.'; for(int dx=-1;dx<=1;dx++) { for(int dy=-1;dy<=1;dy++) { int nx=x+dx; int ny=y+dy; if(nx<=n&&ny<=m&&nx>=1&&ny>=1&&maps[nx][ny]=='W') { maps[nx][ny]='.'; dfs(nx,ny); } } }}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { cin>>maps[i][j]; } } int cnt=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(maps[i][j]=='W') { dfs(i,j); cnt++; } } } printf("%d\n",cnt); return 0;}
bfs:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int MAXN=100+10;char maps[MAXN][MAXN];struct Point{ int x,y;};queue<Point>q;int n,m;void bfs(int x,int y){ maps[x][y]='.'; q.push((Point){x,y}); while(!q.empty()) { Point p=q.front(); q.pop(); for(int dx=-1;dx<=1;dx++) { for(int dy=-1;dy<=1;dy++) { int nx=p.x+dx; int ny=p.y+dy; if(maps[nx][ny]=='W'&&nx<=n&&ny<=m&&nx>=1&&ny>=1) { maps[nx][ny]='.'; q.push((Point){nx,ny}); } } } }}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { cin>>maps[i][j]; } } int cnt=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(maps[i][j]=='W') { bfs(i,j); cnt++; } } } printf("%d\n",cnt); return 0;}
0 0
- poj 2386Lake Counting(DFS, BFS)
- POJ 2386 Lake Counting (DFS or BFS)
- POJ 2386 Lake Counting(dfs or bfs)
- 【poj】2386 Lake Counting bfs||dfs
- poj 2386 Lake Counting (BFS~)
- POJ 2386 Lake Counting-------BFS
- 【BFS】-POJ-2386-Lake Counting
- Poj 2386 Lake Counting【Bfs】
- 【BFS】poj 2386 Lake Counting
- POJ 2386 Lake Counting (DFS)
- poj 2386 Lake Counting (DFS~)
- POJ 2386 Lake Counting (dfs)
- POJ Lake Counting 2386(DFS)
- DFS:Lake Counting POJ 2386
- POJ 2386 Lake Counting DFS
- POJ 2386 Lake Counting [DFS]
- POJ 2386 Lake Counting - DFS
- POJ 2386 Lake Counting(DFS)
- Android 面试题二
- 【原创】东方耀reactnative 视频38之-物理返回键
- Kubernetes1.4正式发布
- cocoa pods
- 测试环境准备
- 【poj】2386 Lake Counting bfs||dfs
- BZOJ 2049, 洞穴勘测
- lesson21 函数在汇编中的过程
- 自定义监听
- listener.ora--sqlnet.ora--tnsnames.ora的关系以及手工配置举例(转载:http://blog.chinaunix.net/uid-83572-id-5510.ht)
- layout_weidget简单用法
- [LeetCode392]Is Subsequence
- 开源框架ViewPagerIndictor(指示器)的使用
- 第一次用cmd调用python打出i love acm