您的位置:首页 > 其它

hdu 1198

2015-08-10 12:07 363 查看

Farm Irrigation

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4962 Accepted Submission(s): 2130

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.



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



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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case, output in one line the least number of wellsprings needed.

[align=left]Sample Input[/align]

2 2 DK HF 3 3 ADC FJK IHE -1 -1

[align=left]Sample Output[/align]

2 3

[align=left]Author[/align]
ZHENG, Lu
解题思路:
并查集的题目。这题一开始想的是把每块田看成一个结构体,包含上下左右四个方向,看有没有通道,如果有,就为1,无就为0,相邻的节点如果相反的方向都为1,则可以合并为一个集合。但后来做了一阵子没做出来,后来换了一个思路。遍历每块田地,只要考虑它和左边和上边的田地是否能并合并就可以了。对于左边和上边哪两块能够合并,看图枚举就可以了。

比如 当前地对左边的那个是否能合并,(左边田地)就找图中田地右边有管道的有B D F G I J K 当前田地左边有管道的图中有 A C F G H I K 这样两块田地就可以合并

对当前地和上边的田地是否能合并,也是枚举出来。

#include <iostream>

using namespace std;
char a[60][60];
int  pre[3600];
int find(int x)
{
int r=x;
while(pre[r]!=r)
{
r=pre[r];
}
int i=x,j;
while(i!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
void join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
pre[fx]=fy;
}
int main()
{
int n,m,num;
while(cin>>n>>m)
{
if(n==-1||m==-1)
break;
for(int i=1;i<=n*m;i++)
{
pre[i]=i;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(j>=2)
{
if((a[i][j-1]=='B'||a[i][j-1]=='D'||a[i][j-1]=='F'||a[i][j-1]=='G'||a[i][j-1]=='I'||a[i][j-1]=='J'||a[i][j-1]=='K')&&(a[i][j]=='A'||a[i][j]=='C'||a[i][j]=='F'||a[i][j]=='G'||a[i][j]=='H'||a[i][j]=='I'||a[i][j]=='K'))
join((i-1)*m+j-1,(i-1)*m+j);
}
if(i>=2)
{
if((a[i-1][j]=='C'||a[i-1][j]=='D'||a[i-1][j]=='E'||a[i-1][j]=='H'||a[i-1][j]=='I'||a[i-1][j]=='J'||a[i-1][j]=='K')&&(a[i][j]=='A'||a[i][j]=='B'||a[i][j]=='E'||a[i][j]=='G'||a[i][j]=='H'||a[i][j]=='J'||a[i][j]=='K'))
join((i-2)*m+j,(i-1)*m+j);
}
}
}
num=0;
for(int i=1;i<=n*m;i++)
{
if(i==pre[i])
num++;
}
cout << num<< endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: