【原创】【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;
}


  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值