Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6024 Accepted Submission(s): 2603
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
Author
ZHENG, Lu
Source
Zhejiang University Local Contest 2005
#include<stdio.h>
#include<string.h>
struct clock
{
int id;
char ch;
}clock[55][55];
int a[2600];
int find(int x)
{
if(x != a[x])
{
a[x] = find(a[x]);
}
return a[x];
}
int merge(int x, int y)
{
x = find(x);
y = find(y);
if(x != y)
{
a[x] = y;
return 1;
}
return 0;
}
int check(char x, char y, int dir)
{
if(x == 'A')
return 0;
else if(x == 'B')
{
if( (y == 'C' || y == 'F' || y == 'G' || y == 'H' || y == 'I' || y == 'K' || y == 'A') && dir == 1)
return 1;
else
return 0;
}
else if(x == 'C')
{
if( dir == 2 && (y == 'E' || y == 'G' || y == 'H' || y == 'J' || y == 'K' || y == 'A' || y == 'B'))
return 1;
else return 0;
}
else if( x == 'D')
{
if(dir == 1 && (y == 'A' || y == 'C' || y == 'F' || y == 'G' || y == 'H' || y == 'I' || y == 'K'))
return 1;
else if(dir == 2 && ( y == 'A' || y == 'B' || y == 'E' || y == 'G' || y == 'H' || y == 'J' || y == 'K'))
return 1;
else
return 0;
}
else if(x == 'E' || x == 'H')
{
if(dir == 2 && ( y == 'A' || y == 'B' || y == 'G' || y == 'H' || y == 'J' || y == 'K' || y == 'E'))
return 1;
else
return 0;
}
else if(x == 'F' || x == 'G')
{
if(dir == 1 && (y == 'A' || y == 'C' || y == 'F' || y == 'G' || y == 'H' || y == 'I' || y == 'K'))
return 1;
else
return 0;
}
else if(x == 'I')
{
if(dir == 1 && (y == 'A' || y == 'C' || y == 'F' || y == 'G' || y == 'H' || y == 'I' || y == 'K'))
return 1;
else if(dir == 2 && ( y == 'E' || y == 'G' || y == 'H' || y == 'J' || y == 'K' || y == 'A' || y == 'B'))
return 1;
else
return 0;
}
else if(x == 'J')
{
if(dir == 1 && (y == 'C' || y == 'F' || y == 'G' || y == 'H' || y == 'I' || y == 'K' || y == 'A'))
return 1;
else if( dir == 2 && ( y == 'A' || y == 'B' || y == 'G' || y == 'H' || y == 'J' || y == 'K' || y == 'E'))
return 1;
else return 0;
}
else if(x == 'K')
{
if(dir == 1 && (y == 'A' || y == 'C' || y == 'F' || y == 'G' || y == 'H' || y == 'I' || y == 'K'))
return 1;
else if(dir == 2 && ( y == 'A' || y == 'B' || y == 'G' || y == 'H' || y == 'J' || y == 'K' || y == 'E'))
return 1;
else return 0;
}
}
int main()
{
int m , n;
while(scanf("%d%d", &m, &n) != EOF && ( m >= 0 && n >= 0))
{
getchar();
int num = 1;
for( int i = 1; i <= m; i++)
{
for(int j = 1; j <= n; j++)
{
clock[i][j].id = num;
scanf("%c", &clock[i][j].ch);
num++;
}
getchar();
}
num--;
for(int i = 1; i <= m*n; i++)
a[i] = i;
for(int i = 1; i <= m; i++)
{
for(int j = 1; j <= n; j++)
{
int dir = 1;
if(j+1 <= n)
{
if(check(clock[i][j].ch, clock[i][j+1].ch, dir) && merge(clock[i][j].id, clock[i][j+1].id))
num--;
}
dir++;
if(i+1 <= m)
{
if(check(clock[i][j].ch, clock[i+1][j].ch, dir) && merge(clock[i][j].id, clock[i+1][j].id))
num--;
}
}
}
printf("%d\n", num);
}
return 0;
}
题意:给出m*n块田地,每个田地中都有一根水管,若是两块田地的水管相连,则这两个田地灌溉只需要一个水源,现在给出m*n个田地的水管形状,求灌溉着m*n块田地最少需要多少个水源。
思路 :可以将m*n块田地从左到右从上到下编号,1~m*n。再以先从左到右的顺寻再从上倒下的顺序对这m*n块进行遍历,以向右和向下两个方向遍历。若相邻两块田地的水管相连,则将这两块田地的编号合并到一个集合里面,以此类推,知道遍历完为止。合并一块田地说明水源需求个数减一,最初需要的个数是m*n个。
关键 :关键在于水管的处理,最终以并查集的方法求出最小的水源数。