HDU 1198 Farm Irrigation(二维转化一维)

来源:互联网 发布:淘宝购买失败系统异常 编辑:程序博客网 时间:2024/04/30 01:05

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.

此题看discuss中有人用dfs水果,但是读懂题意后发现实际上就是并查集,只需判断网格中有多少个联通的格子,然后就放入几个水源即可。

将此题可以分为如下几个问题。
Pro1:如何判断2个格子是否联通?
输入的数据为一个字符数组。因为每个格子有四个方向,又因为总共有A~K种情况,可以想到设置一个只有0和1的二维数组bo[][](0代表无关管道,1代表有管道)。二维数组的每一行存每个格子的四个方向有关管道的标志,每一行分别与相应的字母对应。对于格子(i,j),只需判断下和右的相邻格子是否与其联通。当两个格子相邻的方向都有管道时,即可判断联通。
Pro2:如何将二维转化为一维?
对于此题的每个格子都可以看作为并查集中的每个元素,然后把联通的格子合并,并统计有多少个联通块即可。那么只需将(i,j)转化为一个数即可,可以将(0,0)标记为1,(0,1)为2,(n,m)为n*m。转化公式可为x=i*m+j+1。
解决以上2个问题后,此题就相当于一道裸的并查集,代码如下。

PS:本人是新手,叙述可能有不佳的地方,代码可能比较挫,请见谅。

#include<stdio.h>const int maxn = 550;int bo[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 far[maxn*maxn+1];int a[maxn][maxn];int n,m;int co;void init(int n)//初始化{    for(int i=1;i<=n;i++)         far[i]=i;    co=n;}int find(int x)//找根函数{    if(far[x]==x) return x;    return far[x]=find(far[x]);}void union_(int x1,int y1,int x2,int y2,int k)//合并并统计{    int s,t;    s=find(x1*m+y1+1);    t=find(x2*m+y2+1);    if(x2>=n||y2>=m) return;//判断是否越界    bool tag=false;    if(bo[a[x1][y1]][1]&&bo[a[x2][y2]][3]&&k==1) tag=true;    if(bo[a[x1][y1]][2]&&bo[a[x2][y2]][0]&&k==2) tag=true;    if(tag)    {        if(s!=t)        {            far[s]=t;            co--;        }    }}int main(){    char x;    while(scanf("%d%d",&n,&m)!=EOF)    {        getchar();        if(n==-1&&m==-1) break;        init(n*m);        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                x=getchar();                a[i][j]=x-'A';               }            getchar();        }        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                union_(i,j,i+1,j,2);                union_(i,j,i,j+1,1);            }        }        printf("%d\n",co);    }    return 0;}


0 0
原创粉丝点击