HDU Farm Irrigation

来源:互联网 发布:ubuntu退出vim命令 编辑:程序博客网 时间:2024/05/19 19:40
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.
题目大意:每次有多组测试数据,测试以输入两个-1结束,每组数据第一行先输入m,n,表示有m行n列的网格。接下来是一个m*n的“字符矩阵(无空格)”,字符从A~K,分别代表一种方块(每个方块的样子如图一所示,途中蓝色的线段表示道路)。在这个矩阵中,相邻的方块上的道路若是联通的(联通指的是如BC,FGH这样的横排,像DH,AFI这样的竖排,具体见样例)则归到同一个集合中,对于每组数据,输出图中有多少个集合。

例:向图二这样的网格,集合共有3个,分别为“A”,“F”,“DCJKIHE”。

Output
For each test case, output in one line the least number of wellsprings needed.
 

Sample Input
2 2DKHF3 3ADCFJKIHE-1 -1
 

Sample Output
23

题解:

一题很明显的并查集,只是道路的联通有一些细节处理的问题,具体见代码。
(我写的时候是按第一想法写的,代码很难看。虽然过了,但不知道有没有更快的方法)
#include<cstdio>#include<cstring>#include<iostream>#include<cstdlib>#include<cmath>using namespace std;int m,n,tot,a[55][55],fa[2505],ans;char map[55][55];struct dit{int s,x,l,r;} h[12];int find(int x){if(fa[x]!=x) fa[x]=find(fa[x]);return fa[x];}void doit(){for(int i=2;i<=m;i++)for(int j=1;j<n;j++)   {int s=map[i][j]-64, s1=map[i-1][j]-64, s2=map[i][j-1]-64; int r=find(a[i][j]), r1=find(a[i-1][j]), r2=find(a[i][j-1]);if(h[s].s==1&&h[s1].x==1&&r!=r1)   {fa[r]=r1; r=r1; ans--;}if(h[s].l==1&&h[s2].r==1&&r!=r2)   {fa[r]=r2; ans--;}   }}int main(){memset(h,0,sizeof(h));h[1].l=1; h[1].s=1;  h[2].r=1; h[2].s=1;h[3].l=1; h[3].x=1;  h[4].r=1; h[4].x=1;h[5].s=1; h[5].x=1;  h[6].l=1; h[6].r=1;h[7].l=1; h[7].r=1; h[7].s=1;  h[8].l=1; h[8].x=1; h[8].s=1;h[9].l=1; h[9].r=1; h[9].x=1;  h[10].x=1; h[10].r=1; h[10].s=1;h[11].x=1; h[11].r=1; h[11].s=1; h[11].l=1;scanf("%d%d",&m,&n);while(m!=-1)   {    for(int i=1;i<=m;i++) scanf("%s",map[i]);tot=0; ans=n*m;for(int i=1;i<=m;i++)for(int j=0;j<n;j++)   a[i][j]=(++tot);for(int i=1;i<=tot;i++) fa[i]=i;for(int j=1;j<n;j++)    {int s1=map[1][j-1]-64,s2=map[1][j]-64;int r1=find(a[1][j-1]),r2=find(a[1][j]);    if(h[s1].r==1&&h[s2].l==1) {fa[r2]=r1; ans--;}   }for(int i=2;i<=m;i++)    {int s1=map[i-1][0]-64, s2=map[i][0]-64;int r1=find(a[i-1][0]),r2=find(a[i][0]);    if(h[s1].x==1&&h[s2].s==1) {fa[r2]=r1; ans--;}   }doit();printf("%d\n",ans);scanf("%d%d",&m,&n);   }return 0;}


0 0
原创粉丝点击