⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pku2386.txt

📁 北京大学 在线acm在线判题系统 一些题目解答
💻 TXT
字号:
Lake Counting 
Time Limit:1000MS  Memory Limit:65536K
Total Submit:2522 Accepted:1179 

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

Hint
OUTPUT DETAILS: 

There are three ponds: one in the upper left, one in the lower left,and one along the right side.

Source
USACO 2004 November

Source

Problem Id:2386  User Id:Drizzle 
Memory:536K  Time:45MS
Language:G++  Result:Accepted

Source 

/*
   农夫三拳@seu
   drizzlecrj.gmail.com
   2007-03-02
*/
#include <iostream>
#include <cstring>

using namespace std;

const int delta[8][2] = 
{
	{-1, 1}, {0, 1},{1, 1},
	{-1, 0}, {1, 0},
	{-1, -1},{0, -1}, {1, -1}
};

int N, M;
char map[101][101];
int res[101][101];
int n;

void DFS(int i, int j, int n)
{
	if(res[i][j] != 0)
		return;
	res[i][j] = n;
	int k;
	for(k = 0; k < 8; k++)
	{
		int x = i + delta[k][0];
		int y = j + delta[k][1];
		if(x >= 0 && x < N && y >= 0 && y < M)
		{
			if(map[x][y] == 'W')
				DFS(x, y, n);
		}
	}
}

void Color()
{
	for(int i = 0; i < N; i++)
	{
		for(int j = 0; j < M; j++)
		{
			if(map[i][j] == 'W' && res[i][j] == 0)
				DFS(i, j, n++);
		}
	}
}

void Output()
{
	int max = 0;
	for(int i = 0; i < N; i++)
	{
		for(int j = 0; j < M; j++)
		{
			if(res[i][j] > max)
				max = res[i][j];
		}
	}
	cout << max << endl;
}

int main()
{
	int i, j;
	while( cin >> N >> M )
	{
		for( i = 0; i < N; i++ )
		{
			for( j = 0; j < M; j++ )
				cin >> map[i][j];
		}
		memset(res, 0, sizeof(res));
		n = 1;
		Color();
		Output();
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -