HDU 1198 Farm Irrigation (并查集)

Farm Irrigation

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


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.

HDU 1198 Farm Irrigation (并查集)_第1张图片
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 

HDU 1198 Farm Irrigation (并查集)_第2张图片
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
大体题意:
给你n*m个图片,问最后连通块的个数!(大体上是这样,细节在看看!)
思路:
并查集!
在输入字符串的时候都从1开始,这样避免了越界的问题!
然后遍历每一个字符,对于每一个字符,判断能否向上,并且上面的能向下!  依次类推四个方向!
发现两个点的父亲不同,则--left。
left初始化为n*m个
最后输出left 即可!
教训:
1.错了好几次,两次数组开小了,保险点尽量多开!
2.要细心,打表打错了 T_T!
大体题意:
给你n*m个图片,问最后连通块的个数!(大体上是这样,细节在看看!)
思路:
并查集!
在输入字符串的时候都从1开始,这样避免了越界的问题!
然后遍历每一个字符,对于每一个字符,判断能否向上,并且上面的能向下!  依次类推四个方向!
发现两个点的父亲不同,则--left。
left初始化为n*m个
最后输出left 即可!
教训:
1.错了好几次,两次数组开小了,保险点尽量多开!
2.要细心,打表打错了 T_T!
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 5000 + 5;
char s[maxn][maxn];
int p[maxn];
int left;
int n,m;
int find(int x){return p[x] == x ? x : p[x] = find(p[x]);}
void add(int a,int b){
	int aa = find(a),bb = find(b);
	if (aa != bb)p[min(aa,bb)] = max(aa,bb);
}
void init(){
    memset(s,0,sizeof s);
    for (int i = 0; i < maxn; ++i)p[i] = i;
    left = n*m;
}
// 0 is up, 1 is down, 2 is left, 3 is right;
bool icon(char ch,int key){
    if (key == 0)
        return ch == 'A' || ch == 'B' || ch == 'E' || ch == 'G' || ch == 'H' || ch == 'J' || ch == 'K';
    if (key == 1)
        return ch == 'C' || ch == 'D' || ch == 'H' || ch == 'I' || ch == 'J' || ch == 'K' || ch == 'E';
    if (key == 2)
        return ch == 'A' || ch == 'C' || ch == 'F' || ch == 'G' || ch == 'H' || ch == 'I' || ch == 'K';
    if (key == 3)
        return ch == 'B' || ch == 'D' || ch == 'F' || ch == 'G' || ch == 'I' || ch == 'J' || ch == 'K';
}

int main(){
    while(scanf("%d%d",&n,&m) == 2 && n != -1 && m != -1){
        init();
        for (int i = 1; i <= n; ++i)
            scanf("%s",s[i] + 1);
        for (int i = 1; i <= n; ++i){
            for (int j = 1; j <= m; ++j){
                char ch = s[i][j];
                if (icon(ch,0) && icon(s[i-1][j],1)){
                    int u = find(i*m+j);
                    int v = find((i-1)*m+j);
                    if (u != v){
                        add(u,v);
                        --left;
                    }
                }
                if (icon(ch,1) && icon(s[i+1][j],0)){
                    int u = find(i*m+j);
                    int v = find((i+1)*m+j);
                    if (u != v){
                        add(u,v);
                        --left;
                    }
                }
                if (icon(ch,2) && icon(s[i][j-1],3)){
                    int u = find(i*m+j);
                    int v = find((i)*m+j-1);
                    if (u != v){
                        add(u,v);
                        --left;
                    }
                }
                if (icon(ch,3) && icon(s[i][j+1],2)){
                    int u = find(i*m+j);
                    int v = find((i)*m+j+1);
                    if (u != v){
                        add(u,v);
                        --left;
                    }
                }
            }
        }
        printf("%d\n",left);
    }
    return 0;
}


你可能感兴趣的:(HDU 1198 Farm Irrigation (并查集))