Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7635 Accepted Submission(s): 3276
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.
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
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
题目大意:有一个农田,该农由几块地组成,每块地都铺设着一些管道,如果地之间的管道相通,那么它们就可以共用一个水源,问最少需要多少水源可以灌溉整块农田
结体思路:这道题目的关键在于建立一张关系图,来说明某块地的连通情况。
type[j][i],i=0,1,2,3;分别表示第j块地的左,下,右,上的连通情况。又用其值表示状态1表示连通,0,表示不通。最后处理方法和油田问题一样dfs就行了。
代码如下:
#include<stdio.h>
#include<string.h>
int n,m;
char mark[55][55];
int vist[55][55];
int type[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}
}; //type[][[4] 0,1,2,3左上右下
int dis[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
int check(int x,int y,int fx,int fy,int i)
{
if(fx<0||fy<0||fx>=n||fy>=m)return 0;
if(vist[fx][fy])return 0;
int ta,tb;
ta=mark[x][y]-'A';
tb=mark[fx][fy]-'A';
if(i==0){//向左
if(type[ta][0] && type[tb][2])return 1;
}else if(i==1){//向上
if(type[ta][1]&&type[tb][3])return 1;
}else if(i==2){//向右
if(type[ta][2]&&type[tb][0])return 1;
}else if(i==3){//向下
if(type[ta][3]&&type[tb][1])return 1;
}
return 0;
}
void dfs(int x,int y)
{
if(vist[x][y])return;
vist[x][y]=1;
for(int i=0;i<4;i++)
{
int fx=x+dis[i][0];
int fy=y+dis[i][1];
if(check(x,y,fx,fy,i))
{
dfs(fx,fy);
}
}
}
int main(){
int flag;
while(scanf("%d %d",&n,&m)!=EOF){
if(n==-1 && m==-1)break;
memset(vist,0,sizeof(vist));
flag=0;
for(int i=0;i<n;i++)
scanf("%s",mark[i]);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(!vist[i][j])
{
dfs(i,j);
flag++;
}
}
}
printf("%d\n",flag);
}
return 0;
}