码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1198 Farm Irrigation(深搜dfs || 并查集)

时间:2014-07-17 15:18:32      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:hdu   bfs   并查集   

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198


----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋:http://user.qzone.qq.com/593830943/main

----------------------------------------------------------------------------------------------------------------------------------------------------------


Problem Description
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of samll squares. Water pipes are placed in these squares. Different square has a different type of pipe. There are 11 types of pipes, which is marked from A to K, as Figure 1 shows.

bubuko.com,布布扣

Figure 1

Benny has a map of his farm, which is an array of marks denoting the distribution of water pipes over the whole farm. For example, if he has a map

ADC
FJK
IHE

then the water pipes are distributed like

bubuko.com,布布扣

Figure 2


Several wellsprings are found in the center of some squares, so water can flow along the pipes from one square to another. If water flow crosses one square, the whole farm land in this square is irrigated and will have a good harvest in autumn.

Now Benny wants to know at least how many wellsprings should be found to have the whole farm land irrigated. Can you help him?

Note: In the above example, at least 3 wellsprings are needed, as those red points in Figure 2 show.
 
Input
There are several test cases! In each test case, the first line contains 2 integers M and N, then M lines follow. In each of these lines, there are N characters, in the range of ‘A‘ to ‘K‘, denoting the type of water pipe over the corresponding square. A negative M or N denotes the end of input, else you can assume 1 <= M, N <= 50.
 
Output
For each test case, output in one line the least number of wellsprings needed.

Sample Input
2 2 DK HF 3 3 ADC FJK IHE -1 -1
 
Sample Output
2 3

题意:有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇水,问需要打多少口井。

代码一(dfs):

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define TM 117
//存储11中类型的土地,二维中的0 1 2 3分别代表这种类型的土地的左上右下  
//为1表示这个方向有接口,为0表示这个方向没有接口 
int a[11][4]={{1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1,0},{1,0,1,0},{0,1,0,1},
{1,1,0,1},{1,0,1,1},{0,1,1,1},{1,1,1,0},{1,1,1,1}};
int map[TM][TM];
char s[TM][TM];
bool vis[TM][TM];
int n, m, coun;
void dfs(int x, int y)
{
	vis[x][y] = true;
	for(int i = 0; i < 4; i++)
	{
		if(i == 0)
		{
			if(a[map[x][y]][0]&&a[map[x-1][y]][2]&&x-1>=0&&!vis[x-1][y])
				dfs(x-1,y);
		}
		else if(i == 1)
		{
			if(a[map[x][y]][1]&&a[map[x][y+1]][3]&&y+1<m&&!vis[x][y+1])
				dfs(x,y+1);
		}
		else if(i == 2)
		{
			if(a[map[x][y]][2]&&a[map[x+1][y]][0]&&x+1<n&&!vis[x+1][y])
				dfs(x+1,y);
		}
		else if(i == 3)
		{
			if(a[map[x][y]][3]&&a[map[x][y-1]][1]&&y-1>=0&&!vis[x][y-1])
				dfs(x,y-1);
		}
	}
	return;
}
void init()
{
	memset(vis,false,sizeof(vis));
	coun = 0;
}
int main()
{
	int i, j;
	while(cin>>n>>m)
	{
		init();
		if(n==-1 && m==-1)
			break;
		for(i = 0; i < n; i++)
		{
			cin>>s[i];
			for(j = 0; j < m; j++)
			{
				map[i][j] = s[i][j]-'A';
			}
		}
		for(i = 0; i < n; i++)
		{
			for(j = 0; j < m; j++)
			{
				if(!vis[i][j])
				{
					coun++;
					dfs(i,j);
				}
			}
		}
		cout<<coun<<endl;
	}
	return 0;
}

代码二:(并查集)

待更新…………


hdu 1198 Farm Irrigation(深搜dfs || 并查集),布布扣,bubuko.com

hdu 1198 Farm Irrigation(深搜dfs || 并查集)

标签:hdu   bfs   并查集   

原文地址:http://blog.csdn.net/u012860063/article/details/37902749

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!