码迷,mamicode.com
首页 > 其他好文 > 详细

Farm Irrigation

时间:2014-05-25 19:42:33      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   c   code   

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.
bubuko.com,布布扣
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 
bubuko.com,布布扣
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

Author

ZHENG, Lu

Source

Zhejiang University Local Contest 2005
bubuko.com,布布扣
 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 }
View Code

 

Farm Irrigation,布布扣,bubuko.com

Farm Irrigation

标签:des   style   class   blog   c   code   

原文地址:http://www.cnblogs.com/LWF5201314614/p/3750308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!