Farm Irrigation
Farm Irrigation
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.![](http://acm.hdu.edu.cn/data/images/1121-1.gif)
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![](http://acm.hdu.edu.cn/data/images/1121-2.gif)
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.
![](http://acm.hdu.edu.cn/data/images/1121-1.gif)
ADC FJK IHE
then the water pipes are distributed like
![](http://acm.hdu.edu.cn/data/images/1121-2.gif)
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
Author
Source
Zhejiang University Local Contest 2005
解法:暴力搜索
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 int LenX,LenY,num[55][55],sign[55][55],SIGN; 5 int Map[11][4]={1,1,0,0, 0,1,1,0, 1,0,0,1, 0,0,1,1, 0,1,0,1, 1,0,1,0, 1,1,1,0, 1,1,0,1, 1,0,1,1, 0,1,1,1, 1,1,1,1}; 6 void Deal(int y,int x) 7 { 8 int i,ii,jj; 9 if(sign[y][x]==1)return; 10 if(x<0||y<0||x>=LenX||y>=LenY)return; 11 sign[y][x]=1; 12 if(Map[num[y][x]][0]==1) 13 if((x-1>=0)&&(Map[num[y][x-1]][2]==1))Deal(y,x-1); 14 if(Map[num[y][x]][2]==1) 15 if((x+1<LenX)&&(Map[num[y][x+1]][0]==1))Deal(y,x+1); 16 if(Map[num[y][x]][1]==1) 17 if((y-1>=0)&&(Map[num[y-1][x]][3]==1))Deal(y-1,x); 18 if(Map[num[y][x]][3]==1) 19 if((y+1<LenY)&&(Map[num[y+1][x]][1]==1))Deal(y+1,x); 20 } 21 int main() 22 { 23 int i,x,y,j,ii,jj; 24 char str[55][55]; 25 while(scanf("%d%d",&y,&x)!=EOF) 26 { 27 getchar(); 28 if(x==-1&&y==-1)return; 29 memset(sign,0,sizeof(sign)); 30 memset(num,0,sizeof(num)); 31 LenX=x;LenY=y; 32 for(i=0;i<y;i++) 33 gets(str[i]); 34 for(j=0;j<y;j++) 35 for(i=0;i<x;i++) 36 {num[j][i]=str[j][i]-'A';sign[j][i]=0;} 37 for(j=0,SIGN=0;j<y;j++) 38 for(i=0;i<x;i++) 39 if(sign[j][i]==0) 40 {Deal(j,i);SIGN++;} 41 printf("%d\n",SIGN); 42 } 43 return 0; 44 }
更新:2015.8.1(并查集)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <stdio.h> 3 #include <math.h> 4 #include <algorithm> 5 #define MAX 1008056 6 using namespace std; 7 int ID[MAX]; 8 int Map[11][4]={1,1,0,0, 0,1,1,0, 1,0,0,1, 0,0,1,1, 0,1,0,1, 1,0,1,0, 1,1,1,0, 1,1,0,1, 1,0,1,1, 0,1,1,1, 1,1,1,1}; 9 10 int Sign; 11 void Cread(int N) 12 { 13 for(int i=0;i<=N;i++)ID[i]=i; 14 } 15 int Find(int x) 16 { 17 if(x!=ID[x])ID[x]=Find(ID[x]); 18 return ID[x]; 19 } 20 void Update(int a,int b) 21 { 22 int A,B; 23 A=Find(a);B=Find(b); 24 if(A!=B){ID[A]=B;Sign++;} 25 } 26 int main() 27 { 28 int N,M,i,j; 29 char Str[551][551]; 30 while(scanf(" %d %d",&N,&M)!=EOF) 31 { 32 if(N<0||M<0)break; 33 Cread(N*M);Sign=0; 34 for(i=0;i<N;i++) 35 for(j=0;j<M;j++) 36 scanf(" %c",&Str[i][j]); 37 for(i=0;i<N;i++) 38 { 39 for(j=0;j<M;j++) 40 { 41 if(i+1<N&&Map[Str[i][j]-'A'][3]) 42 { 43 if(Map[Str[i+1][j]-'A'][1])Update(i*M+j,(i+1)*M+j); 44 } 45 if(j+1<M&&Map[Str[i][j]-'A'][2]) 46 { 47 if(Map[Str[i][j+1]-'A'][0])Update(i*M+j,i*M+(j+1)); 48 } 49 } 50 } 51 printf("%d\n",N*M-Sign); 52 } 53 return 0; 54 }
转载请备注:
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************