HDU 1198

Farm Irrigation

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

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 2
DK
HF

3 3
ADC
FJK
IHE

-1 -1

Sample Output

2
3

我的并查集,写的真是失败= =。

#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <cmath>
using namespace std;
const int N=55;
int n,m;
int f[N*N];
char maze[N][N];
int t[12][4] ={{1,1,0,0},{0,1,1,0},{1,0,0,1},{0,0,1,1},
                 {0,1,0,1},{1,0,1,0},{1,1,1,0},{1,1,0,1},
                 {1,0,1,1},{0,1,1,1},{1,1,1,1},{0,0,0,0}};
int find(int x)
{
    if(f[x] == x) return x;
    else return f[x] = find(f[x]);
}
set<int> st;
void init()
{
    for(int i = 1; i <= n*m; i++)
        f[i] = i;
    memset(maze, ‘A‘+11, sizeof(maze));
    st.clear();
}
int fun(int i, int j)
{
    return (i-1)*m + j;
}
void Union(int x, int y)
{
    x = find(x);
    y = find(y);
    if(x != y) f[x] = y;
}
void judge(int i, int j)
{
    char now = maze[i][j];
    char up = maze[i-1][j];
    char left = maze[i][j-1];
    if(t[now-‘A‘][1] == 1 && t[up-‘A‘][3] == 1) {
                    int a = fun(i,j);
                    int b = fun(i-1,j);
                    Union(a,b);
                }
    if(t[now-‘A‘][0] == 1 && t[left-‘A‘][2] == 1) {
                    int a = fun(i,j);
                    int b = fun(i,j-1);
                    Union(a,b);
                }
}
int main(){
    while(scanf("%d %d", &n, &m)){
        if(n == -1 && m == -1) break;
        init();
        for(int i = 1; i <= n; i++)
            scanf("%s", maze[i]+1);
        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++){
            judge(i,j);

        }
        for(int i = 1; i <= n*m; i++)
            st.insert(find(i));
        printf("%d\n", st.size());

    }
    return 0;
}

  

时间: 2024-10-13 18:08:06

HDU 1198的相关文章

hdu 1198 Farm Irrigation (搜索或并查集)

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5818    Accepted Submission(s): 2521 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle

Farm Irrigation HDU - 1198 (并查集)

Farm Irrigation HDU - 1198 题意:给11种管道,问草地最少需要打多少个井才可以全部灌溉. 把每种管道的状态用二进制表示一下,然后对每一块草地,判断能否和上面或者左面的草地的管道连接. 然后并查集搞一下. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn=55; 4 5 int g[12]={10,9,6,5,12,3,11,14,7,13,15}; 6 int f[maxn*maxn]

HDU 1198 Farm Irrigation (并查集)

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5809    Accepted Submission(s): 2516 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle,

hdu 1198 Farm Irrigation(深搜dfs || 并查集)

转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 ----------------------------------------------------------------------------------------------------------------------

HDU 1198 Farm Irrigation (并查集优化,构图)

本题和HDU畅通工程类似,只不过畅通工程给出了数的连通关系, 而此题需要自己判断连通关系,即两个水管是否可以连接到一起,也是本题的难点所在. 记录状态,不断combine(),注意只需要判断左方和上方就行,这样不会重复判断,而且肯定都可以遍历到所有的状态. #include<stdio.h> #include<iostream> #include<string> //记录水管的形状,每种水管用一个由'0'和'1'组成的长度为4的字符串代表, //分别表示上下左右四边是否

HDU 1198 Farm Irrigation

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7897    Accepted Submission(s): 3418 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle

HDU 1198(并查集)

题意:给你11个图,每一个都有管道,然后给一张由这11个正方形中的n个组成的图,判断有几条连通的管道: 思路:在大一暑假的时候做过这道题,当时是当暴力来做的,正解是并查集,需要进行一下转换: 转换1:将子图中的管道转换为数字码,通为1,不通为0: 转换2:一维--->二维,i,j换成在n*m中的第几个,p[i][j] = i*n+j,而且find_set,Union也需要独一无二的坐标来进行判断,然后转换成2维去具体比较: 1 #include<stdio.h> 2 char a[11]

hdu 1198 Farm Irrigation(15ms)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6608    Accepted Submission(s): 2848 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided

hdu 1198(并查集 )

自从懂了并查集只后,感觉好多题都是并查集,就像哪一天的字典树一样,这道题一看就是一个并查集,最后查询父节点有几个, 难点:建模的时候应该吧上下联通的和左右联通的标记一下,只要他们和上下左右的都能连通,就把他们并到一个集合里面,我是只判断下和右即可, 源代码: #include<stdio.h> #include<string.h> int up[8], down[8], right[8], left[8]; int par[2504]; char map[54][54]; char