poj2386(BFS与DFS)
来源:互联网 发布:东京食尸鬼面具淘宝 编辑:程序博客网 时间:2024/06/18 04:18
这个题就是求联通块
bfs代码
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <queue>using namespace std;char s[233][233];int n,m,ans = 0;bool vis[233][233];int dx[9]={0,1,0,-1,1,-1,1,-1};int dy[9]={1,0,-1,0,1,-1,-1,1};struct point{ int x,y;};queue <point> q;void bfs(int x,int y){ q.push((point){x,y}); while(!q.empty()) { point p = q.front(); q.pop(); for(int i = 0; i < 8; i ++) { int nx = dx[i]+ p.x; int ny = dy[i]+ p.y; if(s[nx][ny] == 'W' && !vis[nx][ny] && nx <= n && nx > 0 && ny <= m&&ny > 0) { q.push((point){nx,ny}); vis[nx][ny] = 1; } } }}int main(){ scanf("%d%d",&n,&m); for(int i = 1; i <= n; i ++) for(int j = 1; j <= m; j ++) cin >>s[i][j]; for(int i = 1; i <= n; i ++) for(int j = 1; j <= m; j ++) { if(s[i][j] == 'W'&&!vis[i][j]) { ans ++; bfs(i,j); } } printf("%d",ans); return 0;}
dfs代码
#include <cstdio>#include <iostream>#include <cstring>#include <queue>using namespace std;char s[233][233];int n,m,ans = 0;bool vis[233][233];int dx[9] = {0,1,0,-1,1,-1,-1,1};int dy[9] = {1,0,-1,0,1,-1,1,-1};void dfs(int x,int y){ vis[x][y] = 1; for(int i = 0; i < 8; i ++) { int nx = dx[i]+x; int ny = dy[i]+y; if(s[nx][ny]=='W' && !vis[nx][ny] && nx >0 &&nx <= n&& ny>0 && ny <=m) { vis[nx][ny] = 1; 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 >> s[i][j]; for(int i = 1 ;i <= n; i ++) for(int j = 1; j <= m; j ++) { if(s[i][j] == 'W' && !vis[i][j]) { ans ++; dfs(i,j); } } printf("%d" ,ans); return 0;}
0 0
- poj2386(BFS与DFS)
- POJ2386-简单BFS/DFS
- poj2386(dfs)
- poj2386 (DFS)
- bfs和dfs:poj2386和leetcode130
- DFS(POJ2386)精简版
- POJ2386:Lake Counting(dfs)
- poj2386-dfs
- poj2386 DFS
- poj2386 Lake Counting(简单DFS)
- POJ2386:Lake Counting(DFS)
- poj2386 Lake Counting DFS
- 简单dfs--poj2386
- POJ2386 Lake Counting(DFS)
- POJ2386 Lake Counting 【DFS】
- poj2386水洼dfs
- POJ2386经典DFS深搜
- POJ2386 Lake Counting (dfs)
- matlab 判断参数是否都赋值了。
- matlab for循环执行进度条,每50更新一次进度条
- 查看Eclipse32位还是64位,查看JDK是32位还是64位
- JAVA刷网站流量的技术研究总结
- PHP5.5以上V9会一直报错, mysql 如何改为 mysqli
- poj2386(BFS与DFS)
- openjudge约瑟夫问题
- TCP BBR算法与Reno/CUBIC的对比
- codevs1459n个数的最小公倍数
- 搞懂树状数组
- OpenJudge noi 1804小游戏(POJ1101)
- OpenJudge noi253 丛林中的路
- LICS
- 【模板】最短路&&最小生成树