ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ 2386 Lake Counting

2020-11-15 21:35:18  阅读:295  来源: 互联网

标签:movey WW field int Lake movex ++ POJ Counting


Lake Counting(POJ 2386)

原题目如下:

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


解题思路:

这道题是一道非常经典的深度优先算法题目,具体思路为:我们先任意找到一个W,从那个W开始进行遍历,在遍历时寻找下一个W,直到没有W为止,算作一个水坑。并且,当我们遍历到W时,我们需要将这个W改为. 目的是为了不重复进行遍历。注意,在每一次示例之后,都要清一次count。不然的话上一个示例的水坑数将导致这一次的水坑数不正确。(其实,这道题的基本思想就是图的遍历)(实在不懂的话,阅读一下代码再结合实例就会明白)


代码如下:

#include <iostream>
#include <cstdio>
using namespace std;
char graph[100][100];   
int n,m;
pair<int, int> Move[3][3];   //定义一个pair类型的数组,代表能移动的方向(需要用field数组构造)(分别对应着九个移动的方向)
int field[3] = { -1,0,1 };   
void dfs(int x, int y);    //代表进行深度优先搜索的函数

void dfs(int x, int y)
{
	int movex, movey;          //定义移动后的x,y坐标
	int k, l;
	graph[x][y] = '.';   //将当前所在的位置设置为没有积水
	for (k = 0; k < 3; k++)
	{
		for (l = 0; l < 3; l++)
		{
			movex = x + Move[k][l].first;
			movey = y + Move[k][l].second;
			if (movex >= 0 &&  movex < n && movey >= 0 && movey < m && graph[movex][movey] == 'W')    //当在图中移动时,x的移动,代表了坐标向上或下移动了多少。y的移动,代表了坐标向左或向右移动了多少
			{
				dfs(movex, movey);
			}
		}
	}
	
}
int main()
{
	int i, j;
	int count=0;
	while (scanf("%d %d",&n,&m)!=EOF)
	{
		for (i = 0; i < n; i++)
		{
			for (j = 0; j < m; j++)
			{
				cin >> graph[i][j];
			}
		}
		for (i = 0; i < 3; i++)
		{
			for (j = 0; j < 3; j++)
			{
				Move[i][j] = make_pair(field[i], field[j]);
			}
		}
		for (i = 0; i < n; i++)
		{
			for (j = 0; j < m; j++)
			{
				if (graph[i][j] == 'W')   //再图中进行搜索,看有没有值为W的节点,如果有,从这个节点开始遍历
				{
					dfs(i, j);     //对这个节点以及跟这个节点的所有联通节点,进行深度优先搜索
					count++;       //一次深度优先搜索之后,水坑数就+1(这个地方不懂的话,就参考上面的输入实例自己思考一下把)
				}
			}
		}
		printf("%d\n", count);
		count = 0;
	}
}

标签:movey,WW,field,int,Lake,movex,++,POJ,Counting
来源: https://www.cnblogs.com/gao79135/p/13979047.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有