题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2412
Time Limit: 2 Seconds Memory Limit: 65536 KB

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

Problem solving report:

Description: 有11种土地块,其中有修好的水渠,现在一片土地由这11种土地组成,需要浇水,问需要打多少口井。
Problem solving: 一开始首先想到的就是并查集,就是集合的合并,最后求多少个集合。我们只需要对每个土地块与右方和下方的进行合并即可。合并之前要先判断是否连通,若能连通则合并。另外一种就是DFS了,这个比较简单,类似于
Oil Deposits问题(https://blog.csdn.net/lzyws739307453/article/details/80251657).
并查集:

#include <stdio.h>
#define MAXN 55
int n, m, ans, f[MAXN * MAXN];
char map[MAXN][MAXN]; 
int dir[2][2] = {{0, 1}, {1, 0}};
int arr[] = {3, 6, 9, 12, 10, 5, 7, 11, 13, 14, 15};
int getf(int v) {
    if (f[v] != v)
        return f[v] = getf(f[v]);
    return v;
}
bool merge(int u, int v) {
    int t1 = getf(u);
    int t2 = getf(v);
    if (t1 != t2) {
        f[t2] = t1;
        return true;
    }
    return false;
}
void edge(int x, int y) {
    int ta = map[x][y] - 'A';
    for (int i = 0; i < 2; i++) {
        bool temp = false;
        int tx = x + dir[i][0];
        int ty = y + dir[i][1];
        if (tx >= n || ty >= m)
            continue;
        int tb = map[tx][ty] - 'A';
        if (!i) {
            if (((arr[ta] >> 2) & 1) && (arr[tb] & 1))
                temp = true;
        }
        else {
            if (((arr[ta] >> 3) & 1) && ((arr[tb] >> 1) & 1))
                temp = true;
        }
        if (temp)
            if (merge(x * m + y, tx * m + ty))
                ans++;
    }
}
int main() {
    while (scanf("%d%d", &n, &m), n > 0 && m > 0) {
        ans = 0;
        for (int i = 0; i < n * m; i++)
            f[i] = i;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                scanf(" %c", &map[i][j]);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                edge(i, j);
        printf("%d\n", n * m - ans);
    }
    return 0;
}

DFS:

#include <stdio.h>
#include <string.h>
#define MAXN 55
int n, m, ans, vis[MAXN][MAXN];
char map[MAXN][MAXN]; 
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int arr[] = {3, 6, 9, 12, 10, 5, 7, 11, 13, 14, 15};
void DFS(int x, int y) {
    vis[x][y] = 1;
    int ta = map[x][y] - 'A';
    for (int i = 0; i < 4; i++) {
        bool temp = false;
        int tx = x + dir[i][0];
        int ty = y + dir[i][1];
        if (tx >= n || tx < 0 || ty >= m || ty < 0 || vis[tx][ty])
            continue;
        int tb = map[tx][ty] - 'A';
        if (!i) {
            if (((arr[ta] >> 2) & 1) && (arr[tb] & 1))
                temp = true;
        }
        else if (!(i - 1)){
            if (((arr[ta] >> 3) & 1) && ((arr[tb] >> 1) & 1))
                temp = true;
        }
        else if (!(i - 2)) {
            if ((arr[ta] & 1) && ((arr[tb] >> 2) & 1))
                temp = true;
        }
        else {
            if (((arr[ta] >> 1) & 1) && ((arr[tb] >> 3) & 1))
                temp = true;
        } 
        if (temp)
            DFS(tx, ty);
    }
}
int main() {
    while (scanf("%d%d", &n, &m), n > 0 && m > 0) {
        ans = 0;
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                scanf(" %c", &map[i][j]);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if (!vis[i][j])
                    DFS(i, j), ans++;
        printf("%d\n", ans);
    }
    return 0;
}