BFS求连通块
2018-02-01 10:46
120 查看
代码略繁杂,当时没有想到简化写法,也懒得改了。
附上链接: https://vjudge.net/contest/208801#problem/H
附上链接: https://vjudge.net/contest/208801#problem/H
#include <iostream> #include <algorithm> #include <stack> #include <queue> #include <stdio.h> #include <string> #include <string.h> using namespace std; char a[101][101]; int sum=0; void solve(int m,int n){ a[m] ='.'; if(a[m-1][n-1]=='W'){ solve(m-1,n-1); } if(a[m-1] =='W'){ solve(m-1,n); } if(a[m-1][n+1]=='W'){ solve(m-1,n+1); } if(a[m][n-1]=='W'){ solve(m,n-1); } if(a[m][n+1]=='W'){ solve(m,n+1); } if(a[m+1][n-1]=='W'){ solve(m+1,n-1); } if(a[m+1] =='W'){ solve(m+1,n); } if(a[m+1][n+1]=='W'){ solve(m+1,n+1); } } int main(){ ios::sync_with_stdio(0); int n,m; while(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]=='W'){ solve(i,j); sum++; } } } cout<<sum<<endl; sum=0; } return 0; }
相关文章推荐
- codeforces 590C C. Three States(bfs+连通块之间的最短距离)
- zoj 3864 Quiz for EXO-L(连通块 bfs)
- HDU ~ 1241 ~ Oil Deposits (BFSorDFS求连通块)
- 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
- GPLT L3-004. 肿瘤诊断【三维bfs求连通块】
- HDU1241 DFS/BFS 求连通块
- bfs求连通块的个数
- 2017百度之星初赛(A) 1006 度度熊的01世界(bfs找连通块)
- hdu 1548 A strange lift (bfs)
- POJ 1166 The Clocks(状压BFS)
- Cheese Aizu - 0558-bfs
- POJ 3026 Borg Maze BFS+MST
- ZOJ 3652 Maze 模拟,bfs,读题 难度:2
- POJ3984 迷宫问题 —— BFS
- HDU-2128 Tempter of the Bone II BFS
- hdu 1240 3维迷宫 求起点到终点的步数 (BFS)
- 迷宫的最短路径(BFS,起点到终点)
- UVA 439 Knight Moves 基础BFS
- POJ - 3278 - Catch That Cow (BFS)
- bzoj1116 [POI2008]CLO(记录带环连通块)