Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4323 Accepted Submission(s): 1867
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
题意:
根据题目所给的A~F块带有水源的田地组成一块大的田地,输出有几段水源没有相连接的数量。如例题
ADC
FJK
IHE
A与 F为单水源,而剩下的都已经相连通,合并为单水源,故总水源数有3。
思路:我先将A~F的每一块的上下左右标记好,若有出口则为1,若没有则为0;eg:A:up=1,donw=0,left=1,right=0;
在二维数组中,只要不为第一行,就往上查找,只要不为第一列,就往左边查找;
代码:
#include<iostream>
#include<stdio.h>
using namespace std;
int root[2510];
bool up[12],d[12],l[12],r[12];
void init()//打表,上下左右,有水管的为1,否则为0
{
up[0]=1;up[1]=1;up[2]=0;up[3]=0;//up
up[4]=1;up[5]=0;up[6]=1;up[7]=1;
up[8]=0;up[9]=1;up[10]=1;
d[0]=0;d[1]=0;d[2]=1;d[3]=1;//down
d[4]=1;d[5]=0;d[6]=0;d[7]=1;
d[8]=1;d[9]=1;d[10]=1;
l[0]=1;l[1]=0;l[2]=1;l[3]=0;//left
l[4]=0;l[5]=1;l[6]=1;l[7]=1;
l[8]=1;l[9]=0;l[10]=1;
r[0]=0;r[1]=1;r[2]=0;r[3]=1;//right
r[4]=0;r[5]=1;r[6]=1;r[7]=0;
r[8]=1;r[9]=1;r[10]=1;
}
int Find(int x)
{
if(x!=root[x])
root[x]=Find(root[x]);
return root[x];
}
void Uion(int a,int b)
{
int x=Find(a);
int y=Find(b);
if(x!=y)
root[x]=y;
}
int main()
{
int n,m,i,j;
init();
while(~scanf("%d%d",&n,&m))
{
if(n==-1&&m==-1)
break;
char s[51][51];
for(i=0;i<n*m;i++)
root[i]=i;
for(i=0;i<n;i++)
scanf("%s",s[i]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(i&&up[s[i][j]-'A']&&d[s[i-1][j]-'A'])
Uion(i*m+j,(i-1)*m+j);//只要不是第一行就往上查找,如果[i][j]的上和[i-1][j]的下都为1,合并
if(j&&l[s[i][j]-'A']&&r[s[i][j-1]-'A'])
Uion(i*m+j,i*m+j-1);//只要不是第一列就往左查找,如果[i][j]的左和[i][j-1]的右都为1,合并
}
int ans=0;
for(i=0;i<m*n;i++)
{
if(root[i]==i)
ans++;
}
printf("%d\n",ans);
}
return 0;
}