poj 2386 Lake Counting
来源:互联网 发布:telnet修改端口 编辑:程序博客网 时间:2024/05/13 21:36
Lake Counting
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 25183 Accepted: 12688
Description
Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.
Given a diagram of Farmer John's field, determine how many ponds he has.
Given a diagram of Farmer John's field, determine how many ponds he has.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
Output
* Line 1: The number of ponds in Farmer John's field.
Sample Input
10 12W........WW..WWW.....WWW....WW...WW..........WW..........W....W......W...W.W.....WW.W.W.W.....W..W.W......W...W.......W.
Sample Output
3
一道典型的dfs,思路并不难,,可是在计算一个点周围的八个点的时候,,我是单独一个一个写出来的,,
后来看了挑战程序设计,才知道大牛用的两层循环解决,,而且是是把遍历后的'W'改成'.'而不是像我一样另外设置一个flag数组(既麻烦又容易错),大牛,,这种小地方在比赛中时往往可以拉开差距
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char map[101][101];int num,n,m;bool bian(int x,int y){ map[x][y]='.'; for(int i=-1;i<=1;i++) for(int j=-1;j<=1;j++) if(map[x+i][y+j]=='W') bian(x+i,y+j); return 0;}int main(){ while(cin>>n>>m) { num=0; memset(map,0,sizeof(map)); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>map[i][j]; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(map[i][j]=='W') { bian(i,j); num++; } cout<<num<<endl; } return 0;}
0 0
- poj 2386 Lake Counting
- poj 2386 Lake Counting
- POJ 2386 Lake Counting
- poj 2386 Lake Counting
- poj 2386 Lake Counting
- poj 2386 Lake Counting
- POJ 2386 Lake Counting
- poj 2386 Lake Counting
- POJ 2386 Lake Counting
- POJ 2386 Lake Counting
- poj 2386 Lake Counting
- POJ 2386 Lake Counting
- POJ 2386 Lake Counting
- POJ-2386-Lake Counting
- POJ 2386 Lake Counting
- poj 2386 Lake Counting
- Poj 2386 Lake Counting
- poj 2386 Lake Counting
- 此功能公司用不了
- ETL
- HTTP协议版本介绍
- Integer忽视的地方
- MySQL索引的查看创建和删除
- poj 2386 Lake Counting
- 第十二周实践项目一--图及存储结构算法库
- 第八周 计数的模式匹配
- 民意调查Django实现(一)
- GC之路
- 利用android Accessibility实现免root的自动安装、卸载
- Unity 3D网页游戏与flash网页游戏的较量
- 多态图标(1)-PS制作
- HashMap原理