国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

POJ2386——Lake Counting

2019-11-06 08:04:53
字體:
供稿:網(wǎng)友
Lake Counting
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 33004 Accepted: 16459

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 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.

Source

USACO 2004 November

題意:

找出水洼的數(shù)量,所有八連通的看做一個

解:

從任意‘w’開始,把能連接的部分用 '  .  '表示,一次dfs之后找出一個水洼,直到所有變成 ‘  .  ’ 為止。進行dfs次數(shù)為最終答案。

#include <stdio.h>#include <string.h>#include <iostream>using namespace std;char map[105][105];int n,m;int dis[8][2] = {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,-1},{-1,1}};void dfs(int x,int y){	if(x<0 || y<0 || x>=n ||y>=m || map[x][y]=='.')		return;	map[x][y]='.';	for(int i=0;i<8;i++){		int newx = x+dis[i][0];		int newy= y+dis[i][1];		if(newx>=0 && newx<n && newy>=0 && newy<m && map[newx][newy]=='W')		{			dfs(newx,newy);		}	} }int main(){	while(~scanf("%d %d",&n,&m))	{		memset(map,0,sizeof(0));		int count=0;		for(int i=0;i<n;i++)		{			for(int j=0;j<m;j++)			{				cin>>map[i][j];			}			getchar();		}				for(int i=0;i<n;i++)		{			for(int j=0;j<m;j++)			{				if(map[i][j]=='W')				{					dfs(i,j);					count++;				}			}		}		printf("%d/n",count);			}	return 0;} 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 吉安县| 焉耆| 梅州市| 赫章县| 修武县| 兴和县| 湛江市| 南岸区| 睢宁县| 阿拉善右旗| 万全县| 高安市| 湖州市| 桓台县| 喜德县| 邵阳县| 尤溪县| 忻州市| 闽清县| 华亭县| 庐江县| 从江县| 苏州市| 常宁市| 旬阳县| 安国市| 夹江县| 武山县| 江都市| 德庆县| 樟树市| 土默特左旗| 光山县| 资源县| 渝中区| 金乡县| 黎川县| 巴青县| 彭水| 观塘区| 黄龙县|