HDU1198 ZOJ2412 Farm Irrigation【并查集+位运算】

 

Farm Irrigation

 

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

 

 

 

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

 

 

 

问题链接HDU1198 ZOJ2412 Farm Irrigation

问题简述

  农场由方格组成,每个方格铺有管道,管道有11种类型,各个方格间的管道如果相通则只需要共用一个水井。给定一个农场的方格,问需要挖多少口井才能使得所有的方格得到灌溉?

问题分析

  这个问题可以用并查集来解决。  

  各个方格的管道用二进制位表示,便于逻辑运算。

程序说明

  这里给出两种程序,其中一个为二维并查集。

题记:(略)

参考链接:(略)

 

AC的C++语言程序如下:

/* HDU1198 ZOJ2412 Farm Irrigation */

#include <iostream>
#include <stdio.h>

using namespace std;

/* 按NESW(北东南西)顺序,用二进制表示管道类型,
 * 1表示有,0表示没有
 * */
const int pipetype[] = {
     9      // A 1001
    ,12    // B 1100
    ,3      // C 0011
    ,6      // D 0110
    ,10    // E 1010
    ,5      // F 0101
    ,13    // G 1101
    ,11    // H 1011
    ,7      // I 0111
    ,14    // J 1110
    ,15    // K 1111
};
const int N = 50;
int f[N * N + 1], cnt, m, n;
char farm[N + 1][N + 2];

void Init(int k)
{
    for (int i = 1; i <= k; i++)
        f[i] = i;
    cnt = k;
}

int Find(int a) {
    return a == f[a] ? a : f[a] = Find(f[a]);
}

void merge(int x1, int y1, int x2, int y2, int dirt)
{
    if(x2 > m || y2 > n)
        return;

    bool mark; // 连通标志
    int ta = farm[x1][y1] - 'A';
    int tb = farm[x2][y2] - 'A';

    if(dirt)
        mark = ((pipetype[ta] >> 1) & 1) && ((pipetype[tb] >> 3) & 1); // 竖直方向连通判断
    else
        mark = ((pipetype[ta] >> 2) & 1) && (pipetype[tb] & 1); //水平方向连通判断

    // 连通则合并,并查集
    if(mark) {
        int px = Find((x1 - 1) * n + y1);
        int py = Find((x2 - 1) * n + y2);
        if(px != py) {
            f[py] = px;
            cnt--;
        }
    }
}

int main()
{
    while(~scanf("%d%d", &m, &n) && (m != -1 || n != -1)) {
        Init(m * n);

        for(int i = 1; i <= m; i++)
            scanf("%s", farm[i] + 1);

        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", cnt);
    }

    return 0;
}


AC的C++语言程序(二维并查集)如下:

/* HDU1198 ZOJ2412 Farm Irrigation */

#include <iostream>
#include <stdio.h>

using namespace std;

/* 按NESW(北东南西)顺序,用二进制表示管道类型,
 * 1表示有,0表示没有
 * */
const int pipetype[] = {
     9      // A 1001
    ,12    // B 1100
    ,3      // C 0011
    ,6      // D 0110
    ,10    // E 1010
    ,5      // F 0101
    ,13    // G 1101
    ,11    // H 1011
    ,7      // I 0111
    ,14    // J 1110
    ,15    // K 1111
};
const int N = 50;
int f[N][N];
char farm[N][N + 1];
int m, n, cnt;

void Init(int row, int col)
{
    for (int i = 0; i < row; i++)
        for(int j = 0; j < col; j++)
            f[i][j] = i * n + j;
    cnt = row * col;
}

int Find(int row, int col)
{
    return (f[row][col] == row * n + col) ? f[row][col] : f[row][col] = Find(f[row][col] / n, f[row][col] % n);
}

void Union(int row1, int col1, int row2, int col2)
{
    int p1 = Find(row1, col1);
    int p2 = Find(row2, col2);
    if(p1 != p2) {
        f[p2 / n][p2 % n] = p1;
        cnt--;
    }
}

void judge(int row, int col)
{
    // 与左侧连通则合并
    if(col > 0 && (pipetype[farm[row][col] - 'A'] & 1) && ((pipetype[farm[row][col-1] - 'A'] >> 2) & 1))
        Union(row, col, row, col - 1);

    // 与上方连通则合并
    if(row > 0 && ((pipetype[farm[row][col]-'A'] >> 3) & 1) && ((pipetype[farm[row-1][col] - 'A'] >> 1) & 1))
        Union(row, col, row - 1, col);
}

int main()
{
    while(~scanf("%d%d", &m, &n) && (m != -1 || n != -1)) {
        Init(m, n);

        for(int i = 0; i < m; i++)
            scanf("%s", farm[i]);

        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                judge(i, j);

//        int cnt = 0;
//        for(int i = 0; i < m; i++)
//            for(int j = 0; j < n; j++)
//                if(f[i][j] == i * n + j)
//                    cnt++;

        printf("%d\n", cnt);
    }

    return 0;
}

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值