POJ 2386 Lake Counting
2013-12-28 13:54
260 查看
/* * 题目:Lake Counting * * 地址:http://poj.org/problem?id=2386 * * 题意:求w相连集合的个数 * */ #include <iostream> #include <cstring> #include <cstdio> #include <queue> using namespace std; struct node { int x,y; }s,e; queue <node> q; int d[8][2] = {-1,0, 0,1, 1,1, 1,0, 0,-1 , -1,1, 1,-1, -1,-1}; //搜索到A点时,用来向A点周围的点延生 bool f[110][110]; // 标记 char map[110][110]; int n,m; bool is_ok(int x, int y) { if(x>=1 && x<=n && y>=1 && y<=m && map[x][y] == 'W') return true; return false; } void bfs(int x,int y) { f[x][y] = true; // 标记此点已经访问过 s.x = x; s.y = y ; q.push(s); // 第一个点入队 while(!q.empty()) { s = q.front(); q.pop(); for(int i=0;i<8;i++) { int xx = s.x + d[i][0]; int yy = s.y + d[i][1]; while(is_ok(xx,yy)) { if(f[xx][yy]==false) { f[xx][yy] = true; e.x = xx; e.y = yy; q.push(e); } xx += d[i][0]; yy += d[i][1]; } } } } int main() { int i,j; int ans; while(scanf("%d %d",&n,&m)!=EOF) { ans=0; memset(f,false ,sizeof(f)); while(!q.empty()) q.pop(); memset(map,0,sizeof(map)); for(i=1;i<=n;i++) for(j=1;j<=m;j++) scanf(" %c",&map[i][j]); for(i=1;i<=n;i++) { // 从起点开始,遍历所有没有被标记的'W',然后用BFS把与此点相连的点全部标记。 for(j=1;j<=m;j++) if(map[i][j]=='W'&&f[i][j]==false){ bfs(i,j); ans++;} } printf("%d\n",ans); } return 0; }
相关文章推荐
- (Relax DFS专题1.2)POJ 2386 Lake Counting(使用DFS来计算有多少坨东西是连通的)
- POJ 2386 Lake Counting
- POJ 2386 Lake Counting
- POJ 2386 Lake Counting
- POJ2386——Lake Counting
- Poj 2386 Lake Counting
- poj-【2386 Lake Counting】
- POJ 2386 Lake Counting(水淹菜地,DFS,八连通,连通分量)
- POJ 2386(Lake Counting)DFS
- POJ 2386 Lake Counting
- POJ 2386 Lake Counting
- POJ 2386 ----- Lake Counting
- [POJ] 2386 Lake Counting
- POJ - 2386 Lake Counting(简单深搜)
- 【BFS】poj 2386 Lake Counting
- POJ-2386-Lake Counting
- POJ-2386-Lake Counting-解题报告
- POJ 2386 Lake Counting(DFS)
- POJ 2386 Lake Counting (dfs)
- POJ_2386: Lake Counting