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
有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇水,问需要打多少口井。
————————————————————————————————————–如图对于能相连的地只需要打一口井,所以以上需要打三口井就能浇所有的块。稍加分析就可得出本质上就是集合的合并,最后求有几个集合的问题,很容易想到并查集。只需要对每个地块与左方和上方的地块进行合并即可。合并之前先判断是否能连通,若能连通则合并,不能连通,则不能合并。能连通的时候就是正常的并查集了。
可以把每个地块的四个方向用二进制来表示,用位运算来判断可连性。
同时采取倒计数方式,每当合并一个新方格,在总数基础上减一,而若同时联通两个方向,且最后汇聚一点,则由于先判断了竖直方向,使得方格已被连接,接着判断水平向时,会被证否,从而连接不成立。
本题亦可通过DFS解决
#include<iostream>
using namespace std;
const int maxn = 550;
int type[11] = { 3,6,9,12,10,5,7,11,13,14,15 };//二进制结合位运算表达方格状态
char map[maxn + 5][maxn + 5];
int par[maxn*maxn + 5]; //par数组仅可为int类型且为一维数组//相当于制作HASH表,由于方格状态会有重复,因而以坐标为参数更佳
bool vertical = true;
int cnt, M, N;
void init(int n)
{
for (int i = 1; i <= n; i++)
{
par[i] = i;
}
cnt = n;//将计数器初始化为总方格数,每当合并一个方格,减一
}
int find(int x)
{
if (x != par[x])
//return find(par[x]);
par[x] = find(par[x]);//路径压缩
return par[x];
}
void unite(int ay, int ax, int by, int bx, bool vertical)
{
if (!bx || !by)
return;
int a = type[map[ay][ax] - 'A'];
int b = type[map[by][bx] - 'A'];
if (vertical)
{
bool u = (a >> 1) & 1 && (b >> 3) & 1;
if (u)
{
int pa = find(N*(ay - 1) + ax);//?
int pb = find(N*(by - 1) + bx);
if (pa != pb)
{
par[pa] = pb;
cnt--;
}
}
}
else
{
bool l = a & 1 && (b >> 2) & 1;
if (l)
{
int pa = find(N*(ay - 1) + ax);
int pb = find(N*(by - 1) + bx);
if (pa != pb)
{
par[pa] = pb;
cnt--;
}
}
}
}
int main()
{
while (cin >> M >> N&&M != -1 && N != -1)
{
if (M == -1 && N == -1)
break;
init(M*N);
for (int i = 1; i <= M; i++) //每行输入字符间没有间隔
cin >> map[i] + 1;
for (int i = 1; i <= M; i++)
for (int j = 1; j <= N; j++)
{
unite(i, j, i-1, j, vertical);//每个方格的情况,需对上方及左方进行判断,而判断方式基本相同,因而可以加一参数vertical,从而一函两用
unite(i, j, i, j-1, !vertical);
}
cout << cnt << endl;
}
return 0;
}