(HDU

来源:互联网 发布:云计算和大数据的关系 编辑:程序博客网 时间:2024/06/07 05:31

(HDU - 1198)Farm Irrigation

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10438 Accepted Submission(s): 4587

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.

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种类型的地组成一块农场,如果两块地之间有管道连通则他们属于同一块灌溉地(即只需要共用同一口井),给出一块农场,问最少需要多少口井。

思路:并查集,属于同一灌溉区域的地合并,最后答案就是不同集合的数目。

#include<cstdio>using namespace std;const int maxn=55;char a[maxn][maxn];int fa[maxn*maxn];//这里用将二维坐标转化为一维坐标//记录11种类型,1表示有管道,顺序:左上右下 const int type[11][4]={{1,1,0,0},{0,1,1,0},{1,0,0,1},{0,0,1,1},                       {0,1,0,1},{1,0,1,0},{1,1,1,0},{1,1,0,1},                       {1,0,1,1},{0,1,1,1},{1,1,1,1}}; int m,n,ans;int find(int x){    if(x==fa[x]) return x;    else return fa[x]=find(fa[x]);}void merge(int ax,int ay,int bx,int by,bool dir)//dir标记方向:1-竖直,2-水平 {    if(bx>m||by>n) return;    bool flag=false;//标记两块地是否连通    int ta,tb;//两块地的类型值0-10    ta=a[ax][ay]-'A';    tb=a[bx][by]-'A';    if(dir)//竖直方向    {        if(type[ta][3]&&type[tb][1]) flag=true;    }      else//水平方向     {        if(type[ta][2]&&type[tb][0]) flag=true;    }    if(flag)    {        int fx=find(n*(ax-1)+ay),fy=find(n*(bx-1)+by);        if(fx!=fy)        {            fa[fx]=fy;            ans--;        }    }}int main(){    while(scanf("%d%d",&m,&n)!=EOF&&m!=-1&&n!=-1)    {        for(int i=1;i<=m;i++) scanf("%s",a[i]+1);        for(int i=1;i<=m;i++)            for(int j=1;j<=n;j++)                fa[n*(i-1)+j]=n*(i-1)+j;        ans=m*n;//初始每块地单独成一个集合,一共有m*n个集合         for(int i=1;i<=m;i++)            for(int j=1;j<=n;j++)            {                merge(i,j,i+1,j,1);                merge(i,j,i,j+1,0);            }        printf("%d\n",ans);    }    return 0;}