Farm Irrigation
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.
![](https://i-blog.csdnimg.cn/blog_migrate/98438320b6a336844ea7a50507e0c94c.gif)
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
![](https://i-blog.csdnimg.cn/blog_migrate/c0c0d1bb80698cbbb549ba460e1ce196.gif)
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
这个题目挺有意思的,用另一种方式来判断有多少个集合
ABCDEFGHIJK..
我们先把每个图的构造存储下来
然后要用的时候直接调用
然后
比如说,n,m都是4的时候
ABCD
EFGH
AKKH
DECB
(这些事随便写的)
我们可以自己编号
这样子就变成了这个
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
因为题目相当于他们相互之间的关系
只有相邻的才有可能连接在一起
所以我们可以这样子判断
这样子相互之间的关系就都能判断你了
#include <iostream>
using namespace std;
int N[11][4]={{1,0,1,0},{1,0,0,1},{0,1,1,0},{0,1,0,1},{1,1,0,0},{0,0,1,1},{1,0,1,1},{1,1,1,0},{0,1,1,1},{1,1,0,1},{1,1,1,1}};
int p[5555];
int find(int x)
{
if(x!=p[x])
p[x]=p[find(p[x])];
return p[x];
}
void merge(int x,int y)
{
p[find(x)]=find(y);
}
int main()
{
int n,m,i,j,k;
char g[111][111];
while(~scanf("%d%d",&n,&m)&&n!=-1&&m!=-1)
{
for(i=0;i<n;i++)
scanf("%s",g[i]);
for(i=0;i<n*m;i++)
p[i]=i;
int cnt=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(N[g[i][j]-'A'][1]&&N[g[i+1][j]-'A'][0]&&i<n-1)
merge(i*m+j,(i+1)*m+j);
if(N[g[i][j]-'A'][3]&&N[g[i][j+1]-'A'][2]&&j<m-1)
merge(i*m+j,i*m+j+1);
}
}
for(i=0;i<n*m;i++)
if(p[i]==i)cnt++;
printf("%d\n",cnt);
}
return 0;
}