poj2386 Lake Counting(简单DFS)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents

题目链接:http://poj.org/problem?id=1562

----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋http://user.qzone.qq.com/593830943/main

----------------------------------------------------------------------------------------------------------------------------------------------------------

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.

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.

Output

* Line 1: The number of ponds in Farmer John‘s field.

Sample Input

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output

3

代码如下:

#include <iostream>
#include <algorithm>
using namespace std;
#include <cstring>
#define TM 100+17
int N, M;
char map[TM][TM];
bool vis[TM][TM];
int xx[8]={0,1,1,1,0,-1,-1,-1};
int yy[8]={1,1,0,-1,-1,-1,0,1};
void DFS(int x, int y)
{
	vis[x][y] = true;
	for(int i = 0; i < 8; i++)
	{
		int dx = x+xx[i];
		int dy = y+yy[i];
		if(dx>=0&&dx<N&&dy>=0&&dy<M&&!vis[dx][dy]&&map[dx][dy] == 'W')
		{
			vis[dx][dy] = true;
			DFS(dx,dy);
		}
	}
}
int main()
{
	int i, j;
	while(cin>>N>>M)
	{
		int count = 0;
		memset(vis,false,sizeof(vis));
		for(i = 0; i< N; i++)
		{
			cin>>map[i];
		}
		for(i = 0; i < N; i++)
		{
			for(j = 0; j < M; j++)
			{
				if(map[i][j] == 'W' && !vis[i][j])
				{
					count++;
					DFS(i,j);
				}
			}
		}
		cout<<count<<endl;
	}
	return 0;
}

poj2386 Lake Counting(简单DFS)

时间: 2024-10-29 00:42:17

poj2386 Lake Counting(简单DFS)的相关文章

POJ2386 Lake Counting 【DFS】

Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20782   Accepted: 10473 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 <= 10

POJ2386 Lake Counting 图DFS

找出有多少个"水洼"(W的联通块) Sample Input 10 12 W--..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 代码 #include <cstdio> #include <cstring> #include <algorithm> using namesp

POJ2386 Lake Counting

这个题与UVa572 Oil Deposits完全相同,程序改两个字符,改了一下结束条件就通过了. 问题链接:POJ2386 Lake Counting. 题意简述:给定m×n矩阵 (1 <= N <= 100; 1 <= M <= 100),其中'W'代表水域,'.'代表陆地,问有几片湖. 本题可以使用深度优先搜索求解,用广度优先搜索也可以求解,差别不大. 这个程序说明如下: 1.方向数组 使用方向数组后,各个方向的试探的程序就会变得简洁了,用循环处理即可. 2.避免重复搜索 将

《挑战》2.1 POJ 2386 Lake Counting (简单的dfs)

1 # include<cstdio> 2 # include<iostream> 3 4 using namespace std; 5 6 # define MAX 123 7 8 char grid[MAX][MAX]; 9 int nxt[8][2] = { {1,0},{0,-1},{-1,0},{0,1},{1,1},{1,-1},{-1,1},{-1,-1} }; 10 int n,m; 11 12 int can_move( int x,int y ) 13 { 14

【POJ - 2386】Lake Counting (dfs+染色)

-->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= 100; 1 <= M <= 100) 的正方形来表示.农场中的每个格子可以用'W'或者是'.'来分别代表积水或者土地,约翰想知道他的农场中有多少池塘.池塘的定义:一片相互连通的积水.任何一个正方形格子被认为和与它相邻的8个格子相连. 给你约翰农场的航拍图,确定有多少池塘 Input Line 1

Openjudge1388 Lake Counting【DFS/Flood Fill】

http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制:   1000ms   内存限制:   65536kB 描述 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 <= 1

POJ2386 Lake Counting(DFS)

从任意的W开始,不停地把邻接的部分用'.'代替.1次DFS后与初始的这个W连接的所有W就都被替换成了'.',因此直到图中不再存在W为止,总共进行DFS的次数就是答案了.8个方向共对应了8种状态转移,每个格子作为DFS的参数至多被调用一次,所以复杂度为O(8×N×M)=O(N×M). #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace

POJ 2386 Lake Counting(DFS)

题意:有一个大小为N×M的园子,雨后积起了水.八连通的积水被认为是连在一起的.求园子里一共有多少水洼? * * * * W*    (八连通指的就是左图中相对W的*的部分) * * * Sample Input 10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.....W. .W.W......W. ..W.......W. Sample O

poj 2386  Lake Counting DFS

n*m的矩阵W是水 .是地问有多少池塘池塘的定义是:W通过 八个 方向连接成的一片算作是一个池塘 样例输入中,有左上.左下.右侧三片连接在一起的W因而样例输出给出了3个池塘的答案 #include <cstdio> #include <iostream> #define N 110 using namespace std; int n,m; char s[N][N]; void dfs(int x,int y) { s[x][y]='.'; int i,j; for(i=-1; i