Farm Irrigation HDU1198 (并查集)

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

题意理解:对于该题,我们通过题意可以分析出,要让这个矩形的每一小块都得到灌溉,如果拼图让水管相连,就可以少建立些水源,这样我们就可以总结出该题其实就是求这个图有多少个块集是可以通过水管想连通的。但这题我们有需要注意以下几点

1.该题没有直接给出标号,所以需要我们自己通过矩形的位置设定标号。

2.该题的重难点其实并不是并查集,而是如何判断块与块之间的连通,这里我使用的是通过结构体,给定四个方向来判断。

3.我自己有个小问题就是在判断两个块是否连通时,一直WA的原因就是把两个块方向的bool值都是0的时候也判断他们是连通的了。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
using namespace std;

struct Node
{
    bool up, down, left, right;
    Node():up(0),down(0),left(0),right(0){}
    Node(bool a, bool b, bool c, bool d)
    {
        up = a, down = b;
        left = c, right = d;
    }
};

map<char, Node> Array;
char S[55][55];
int pre[2505];

int Find(int root)
{
    int son, temp;
    son = root;
    while(root != pre[root])    root = pre[root];
    while(son != root)
    {
        temp = pre[son];
        pre[son] = root;
        son = temp;
    }
    return root;
}

void Join(int root1, int root2)
{
    int x, y;
    //printf("%d --- %d\n", root1, root2);
    x = Find(root1);
    y = Find(root2);
    if(x != y)  pre[x] = y;
}

void Init()
{
    Array['A'] = Node(1, 0, 1, 0);
    Array['B'] = Node(1, 0, 0, 1);
    Array['C'] = Node(0, 1, 1, 0);
    Array['D'] = Node(0, 1, 0, 1);
    Array['E'] = Node(1, 1, 0, 0);
    Array['F'] = Node(0, 0, 1, 1);
    Array['G'] = Node(1, 0, 1, 1);
    Array['H'] = Node(1, 1, 1, 0);
    Array['I'] = Node(0, 1, 1, 1);
    Array['J'] = Node(1, 1, 0, 1);
    Array['K'] = Node(1, 1, 1, 1);
}

int main()
{
    int N, M;
    Init();
    while( scanf("%d %d", &M, &N) )
    {
        int cnt = 0;
        set<int> ans;
        if(N == -1) break;
        for(int i = 0; i < M; i++)
           scanf("%s",S[i]);
        for(int k = 1; k <= N*M; k++)
            pre[k] = k;
        for(int i = 0; i < M; i++)
        {
            for(int j = 0; j< N; j++)
            {
                if(i > 0 && Array[ S[i-1][j] ].down == Array[ S[i][j] ].up)
                    if( Array[ S[i-1][j] ].down == 1 )
                        Join((i-1)*N+j+1,i*N+j+1);
                if(i < M-1 && Array[ S[i+1][j] ].up == Array[ S[i][j] ].down)
                    if(Array[ S[i+1][j] ].up == 1)
                        Join((i+1)*N+j+1, i*N+j+1);
                if(j > 0 && Array[ S[i][j-1] ].right == Array[ S[i][j] ].left)
                    if(Array[ S[i][j-1] ].right == 1)
                        Join(i*N+j, i*N+j+1);
                if(j < N-1 && Array[ S[i][j+1] ].left == Array[ S[i][j] ].right)
                    if(Array[ S[i][j+1] ].left == 1)
                        Join(i*N+j+2, i*N+j+1);
            }
        }

        for(int i = 1; i <= M*N; i++)
            ans.insert(Find(i));
        printf("%d\n", ans.size());
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值