hdoj 1198 Farm Irrigation

来源:互联网 发布:tem和sem的区别知乎 编辑:程序博客网 时间:2024/04/29 05:48

Farm Irrigation

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


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 2DKHF3 3ADCFJKIHE-1 -1

Sample Output
23
并查集ac:附上解题思路
 
#include<stdio.h>#include<string.h>#define max 3000+10int set[max];int road[12][4]=//方向为上左右下,有管道即为1.  按上左右下排序 有利于下面的计算 {    0,0,0,0,    1,1,0,0,    1,0,1,0,    0,1,0,1,    0,0,1,1,    1,0,0,1,    0,1,1,0,    1,1,1,0,    1,1,0,1,    0,1,1,1,    1,0,1,1,    1,1,1,1};char map[55][55];int find(int p){    int child=p;    int t;    while(p!=set[p])    p=set[p];    while(child!=p)    {        t=set[child];        set[child]=p;        child=t;    }    return p;}void merge(int x,int y){    int fx=find(x);    int fy=find(y);    if(fx!=fy)    set[fx]=fy;}int main(){    int n,m,i,j,k,sum;    int move[4][2]={-1,0,0,-1,0,1,1,0};//上0左1右2下3      int next_x,next_y;    char c;    while(scanf("%d%d",&n,&m)&&(n!=-1||m!=-1))    {        c=getchar();//输入回车         for(i=1;i<=n*m;i++)        set[i]=i;        for(i=1;i<=n;i++)        {            for(j=1;j<=m;j++)            {                scanf("%c",&map[i][j]);            }            c=getchar();//回车         }        for(i=1;i<=n;i++)        {            for(j=1;j<=m;j++)            {                for(k=0;k<4;k++)//上+下=左+右=3 对0<=k<=3 只要有road[区域i][k]==1&&road[区域j][k-3]==1 即说明两区域连通                 {                    next_x=i+move[k][0];                    next_y=j+move[k][1];                    if(next_x>=1&&next_x<=n&&next_y>=1&&next_y<=m)//不能越界                     {                        if(road[map[next_x][next_y]-'A'+1][3-k]&&road[map[i][j]-'A'+1][k])//两边都有管道 连通                         //下面的是我的一个想法,用数组里面的序号来代表区域序号                         /*这是一个二维数组:                             j=1  j=2   j=3   j=4                     i=1   a(1), b(2), c(3), d(4);                     i=2   e(5), f(6), g(7), h(8);                     i=3   h(9), i(10),j(11),k(12);                     i=4   l(13),m(14),n(15),o(16);                        括号里面的数字代表坐标(i,j)上的元素的次序 t,则有相应公式t= j +总列数*(i-1);                          */                         merge(next_y+m*(next_x-1),j+m*(i-1));//合并                     }                }            }        }        sum=0;        for(i=1;i<=n*m;i++)        {            if(set[i]==i)            sum++;        }        printf("%d\n",sum);    }    return 0;}

0 0