poj 2386 Lake Counting 【dfs(模板)】
来源:互联网 发布:电脑文件管理器软件 编辑:程序博客网 时间:2024/06/05 18:52
Lake Counting
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 24434 Accepted: 12339
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
Hint
OUTPUT DETAILS:
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
Source
USACO 2004 November
思路:
找含有w的区域(包括左右上下,左上,右上,左下,右下),然后输出!
代码:
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int n,m;char a[105][105];int cnt;void dfs(int x,int y){if(x<1||x>n||y<1||y>m)return;if(a[x][y]=='.')return;a[x][y]='.';dfs(x+1,y);dfs(x-1,y);dfs(x,y+1);dfs(x,y-1);dfs(x-1,y-1);dfs(x-1,y+1);dfs(x+1,y-1);dfs(x+1,y+1);}int main(){while(scanf("%d%d",&n,&m)!=EOF){cnt=0;int i,j;for(i=1;i<=n;i++){scanf("%s",a[i]+1);}for(i=1;i<=n;i++)//不能和输入合并! {for(j=1;j<=m;j++){if(a[i][j]=='W'){cnt++;dfs(i,j);}}}printf("%d\n",cnt);}return 0;}
0 0
- poj 2386 Lake Counting 【dfs(模板)】
- 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)
- POJ - 2386 dfs-Lake Counting
- POJ 2386 Lake Counting (DFS)
- 【DFS】poj 2386 Lake Counting
- POJ 2386 Lake Counting dfs
- POJ - Lake Counting(DFS)
- poj Lake Counting (dfs)
- poj 2386 Lake Counting 深搜DFS
- poj 2386Lake Counting(DFS, BFS)
- 简单理解——tableview的重用机制
- hi3518e音频G711调试笔记
- scrapy&mongodb基本使用
- nginx的安装与配置
- 【codevs1722】【NOI1997】最优乘车,单元最短路?
- poj 2386 Lake Counting 【dfs(模板)】
- linux下文件夹的创建、复制、剪切、重命名、清空和删除命令
- css3--background属性
- 如何理解项目管理中的“无为”
- 字符串匹配算法之KMP算法
- 打开MySQL代码调试
- android sdk里的那些东西都是用来干嘛的?
- cocos2dx3.2 Other Linker Flags 设置成 -ObjC 真机编译报错
- HDoj 1695 (莫比乌斯反演)