【原创】【Openjudge】1388 : Lake Counting

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 <= 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.
输入
* 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.
输出
* Line 1: The number of ponds in Farmer John's field.
样例输入
10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
样例输出
3
提示
OUTPUT DETAILS:

There are three ponds: one in the upper left, one in the lower left,and one along the right side.
来源
USACO 2004 Novembe

题目大意  
大雨过后,农夫John的农场被水淹没不知所措有了积水,农场地图中‘W’代表积水,'.'代表干燥的土地。八连通的积水在同一个水洼里。农夫John想知道农场里有多少水洼。

这是一道搜索题。与其他搜索题一样,都是不断地搜索,直到达到目的。但是,这道题的目的是什么呢?
目的是求出有多少水洼,如果递归的话,怎么才能知道求出了数量呢?
所以这样,不能单纯的只调用一次函数(主程序中),否则最多只会找出一个水洼。
我一开始是这样想的,把每个水洼只留下一个W,其余换成干燥,最后再看有几个W。可是这样搜索的时候,留下的W也会被搜到,搜到就会变成'.',答案就会是0。为了避免这个问题,需要设立一个辅助数组或者把这个W换成另一个字符,但是这样既浪费时间也浪费空间(还浪费我的脑容量)。
于是我把函数写在循环里,把一个水洼消灭干净后,水洼数量就加1。我用DFS,BFS实现了这个想法。

代码如下:
DFS:
#include<iostream>
#include<cstdio>
using namespace std;
int m,n,num;
int dr[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
char farm[120][120];
void search(int i,int j)
{
	farm[i][j]='.';
	for(int p=0;p<8;p++)
	{
		int x=i+dr[p][0],y=j+dr[p][1];  
		if(x>=0 && y>=0 && x<m && y<n && farm[x][y]=='W') search(x,y);
	}
}
int main()
{
	cin>>m>>n;
	for (int i=0;i<m;i++) scanf("%s",farm[i]);
	for (int i=0;i<m;i++)  
		for (int j=0;j<n;j++)  
			if (farm[i][j]=='W')
			{  
				search(i,j);  
				num++;  
			}  
   printf("%d\n",num);  
}

BFS:用结构体数组模拟队列。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<stack>
using namespace std;
int m,n,num;
int dr[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,-1},{-1,1}};
char farm[120][120];
struct Epic
{
	int x,y;
};//x-x坐标,y-y坐标
void search(int i,int j)
{
	Epic p,q;
	p.x=i,p.y=j;
	queue <Epic> k;
	k.push(p);
	farm[i][j]='.';
	while(! k.empty() )
	{
		p=k.front();
		k.pop();
		for(int o=0;o<8;o++)
		{
			q.x=p.x+dr[o][0];
			q.y=p.y+dr[o][1];
			if (q.x>=0 && q.x<m && q.y>=0 && q.y<n)
				if (farm[q.x][q.y]=='W' )
				{
					k.push(q);
					farm[q.x][q.y]='.';
				}
		}
	}
}
int main()
{
	scanf("%d %d",&m,&n);
	
	for(int i=0;i<m;i++)
		scanf("%s",farm[i]);
	
	for(int i=0;i<m;i++)
		for(int j=0;j<n;j++)
			if(farm[i][j]=='W')
			{
				search(i,j);
				num++;
			}
				
/*
	for(int i=0;i<m;i++)
	{
		printf("\n");
		for(int j=0;j<n;j++)
			printf("%c",farm[i][j]);
	}
	printf("\n");
	*/

	printf("%d\n",num);
	return 0;
}


Total 175 (delta 60), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 1% (1/60) remote: Resolving deltas: 3% (2/60) remote: Resolving deltas: 5% (3/60) remote: Resolving deltas: 6% (4/60) remote: Resolving deltas: 8% (5/60) remote: Resolving deltas: 10% (6/60) remote: Resolving deltas: 11% (7/60) remote: Resolving deltas: 13% (8/60) remote: Resolving deltas: 15% (9/60) remote: Resolving deltas: 16% (10/60) remote: Resolving deltas: 18% (11/60) remote: Resolving deltas: 20% (12/60) remote: Resolving deltas: 21% (13/60) remote: Resolving deltas: 23% (14/60) remote: Resolving deltas: 25% (15/60) remote: Resolving deltas: 26% (16/60) remote: Resolving deltas: 28% (17/60) remote: Resolving deltas: 30% (18/60) remote: Resolving deltas: 31% (19/60) remote: Resolving deltas: 33% (20/60) remote: Resolving deltas: 35% (21/60) remote: Resolving deltas: 36% (22/60) remote: Resolving deltas: 38% (23/60) remote: Resolving deltas: 40% (24/60) remote: Resolving deltas: 41% (25/60) remote: Resolving deltas: 43% (26/60) remote: Resolving deltas: 45% (27/60) remote: Resolving deltas: 46% (28/60) remote: Resolving deltas: 48% (29/60) remote: Resolving deltas: 50% (30/60) remote: Resolving deltas: 51% (31/60) remote: Resolving deltas: 53% (32/60) remote: Resolving deltas: 55% (33/60) remote: Resolving deltas: 56% (34/60) remote: Resolving deltas: 58% (35/60) remote: Resolving deltas: 60% (36/60) remote: Resolving deltas: 61% (37/60) remote: Resolving deltas: 63% (38/60) remote: Resolving deltas: 65% (39/60) remote: Resolving deltas: 66% (40/60) remote: Resolving deltas: 68% (41/60) remote: Resolving deltas: 70% (42/60) remote: Resolving deltas: 71% (43/60) remote: Resolving deltas: 73% (44/60) remote: Resolving deltas: 75% (45/60) remote: Resolving deltas: 76% (46/60) remote: Resolving deltas: 78% (47/60) remote: Resolving deltas: 80% (48/60) remote: Resolving deltas: 81% (49/60) remote: Resolving deltas: 83% (50/60) remote: Resolving deltas: 85% (51/60) remote: Resolving deltas: 86% (52/60) remote: Resolving deltas: 88% (53/60) remote: Resolving deltas: 90% (54/60) remote: Resolving deltas: 91% (55/60) remote: Resolving deltas: 93% (56/60) remote: Resolving deltas: 95% (57/60) remote: Resolving deltas: 96% (58/60) remote: Resolving deltas: 98% (59/60) remote: Resolving deltas: 100% (60/60) remote: Resolving deltas: 100% (60/60) remote: Counting objects: 3 remote: Counting objects: 4 remote: Counting objects: 6 remote: Counting objects: 10 remote: Counting objects: 13 remote: Counting objects: 15 remote: Counting objects: 19 remote: Counting objects: 22 remote: Counting objects: 28 remote: Counting objects: 34 remote: Counting objects: 36 remote: Counting objects: 38 remote: Counting objects: 40 remote: Counting objects: 42 remote: Counting objects: 49 remote: Counting objects: 50 remote: Counting objects: 51 remote: Counting objects: 64 remote: Counting objects: 69 remote: Counting objects: 71 remote: Counting objects: 81 remote: Counting objects: 91 remote: Counting objects: 96 remote: Counting objects: 101 remote: Counting objects: 104 remote: Counting objects: 114 remote: Counting objects: 120 remote: Counting objects: 131 remote: Counting objects: 144 remote: Counting objects: 153 remote: Counting objects: 165 remote: Counting objects: 179 remote: Counting objects: 187 remote: Counting objects: 197 remote: Counting objects: 213 remote: Counting objects: 220 remote: Counting objects: 231 remote: Counting objects: 241 remote: Counting objects: 258 remote: Counting objects: 270 remote: Counting objects: 274 remote: Counting objects: 289 remote: Counting objects: 298 remote: Counting objects: 332 remote: Counting objects: 353 remote: Counting objects: 375 remote: Counting objects: 17527 remote: Counting objects: 23033 remote: Counting objects: 26210 remote: Counting objects: 29370 remote: Counting objects: 37996 remote: Counting objects: 43676 remote: Counting objects: 46267 remote: Counting objects: 50326 remote: Counting objects: 52648 remote: Counting objects: 54660 remote: Counting objects: 58753 remote: Counting objects: 60436 remote: Counting objects: 64595 remote: Counting objects: 67979 remote: Counting objects: 70970 remote: Counting objects: 73122 remote: Counting objects: 74939 remote: Counting objects: 76795 remote: Counting objects: 79100 remote: Counting objects: 81443 remote: Counting objects: 83914 remote: Counting objects: 86900 remote: Counting objects: 90295 remote: Counting objects: 93691 remote: Counting objects: 95231 remote: Counting objects: 101642 remote: Counting objects: 108319 remote: Counting objects: 110386 remote: Counting objects: 115714 remote: Counting objects: 120384 remote: Counting objects: 125628 remote: Counting objects: 129481 remote: Counting objects: 131493 remote: Counting objects: 135234 remote: Counting objects: 138075 remote: Counting objects: 141316 remote: Counting objects: 144987 remote: Counting objects: 148761 remote: Counting objects: 152079 remote: Counting objects: 154997 remote: Counting objects: 161710 remote: Counting objects: 164913 remote: Counting objects: 167735 remote: Counting objects: 172413 remote: Counting objects: 174035 remote: Counting objects: 178740 remote: Counting objects: 180202 remote: Counting objects: 182779 remote: Counting objects: 188229 remote: Counting objects: 192854 remote: Counting objects: 194578 remote: Counting objects: 197468 remote: Counting objects: 202622 remote: Counting objects: 204426 remote: Counting objects: 208079 remote: Counting objects: 215423 remote: Counting objects: 220318 remote: Counting objects: 226199 remote: Counting objects: 227868, done remote: error: branch refs/heads/v2.1.15.1_dev: remote: To push into this reference you need 'Push' rights. remote: User: likun5 remote: Contact an administrator to fix the permissions remote: remote: Processing changes: refs: 1 remote: Processing changes: refs: 1, done error: failed to push some refs to 'ssh://pdgerrit.tp-link.com:29418/VMS/SERVER/vms' To ssh://pdgerrit.tp-link.com:29418/VMS/SERVER/vms ! refs/heads/v2.1.15.1_dev:refs/heads/v2.1.15.1_dev [remote rejected] (prohibited by Gerrit: not permitted: update)
最新发布
10-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值