hdu 1198 Farm Irrigation
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
F J K
I H E
then the water pipes are distributed like
Figure2
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
解题思路:
农场灌水的问题,每种田的沟都不太一样的,连通的时候才会流水,现在需要每一小块儿田都要灌上水。然后就是把题目对应的输入抽象化,就一个九宫格的0 1符就能表示清楚,就比如说图K是‘+’形状的所以就用
0 1 0
1 1 1
0 1 0
再比如说图I是个‘T’字的形状,用
0 0 0
1 1 1
0 1 0
来存就可以了,最后就是一道简单的搜索题目了,但是这里我错了好几次的,因为习惯性的将数组开大,然后就是我的二维数组就是map[1010][1010]就是一直错误,提示都是Segmentation Fault。然后我又改代码,还是没用。百度一下之后一般是数组越界,指针乱指才会出现的错误。最后我把数组开成map[510][510] 然后就直接过了的,好气
AC代码
#include <stdio.h>
#include <string.h>
#define N 510
int book[N][N];
int map[N][N];
int next[4][2]={{1,0},{0,-1},{-1,0},{0,1}};
int n,m;
void dfs(int x,int y)
{
int k;
int tx,ty;
for(k=0;k<4;k++){
tx=x+next[k][0];
ty=y+next[k][1];
if(tx<0||ty<0||tx>=n*3||ty>=m*3)
continue;
if(book[tx][ty]==0&&map[tx][ty]==1){
book[tx][ty]=1;
map[tx][ty]=0;
dfs(tx,ty);
book[tx][ty]=0;
}
}
}
int main()
{
int i,j,f,h,yy;
char a;
while(scanf("%d%d",&n,&m)!=EOF){
if(n==-1&&m==-1)
break;
f=0;
for(i=0;i<n;i++){
h=0;
getchar();
for(j=0;j<m;j++){
scanf("%c",&a);
switch(a){
case 'A':
map[f][h]=0;map[f][h+1]=1;map[f][h+2]=0;
map[f+1][h]=1;map[f+1][h+1]=1;map[f+1][h+2]=0;
map[f+2][h]=0;map[f+2][h+1]=0;map[f+2][h+2]=0;
break;
case 'B':
map[f][h]=0;map[f][h+1]=1;map[f][h+2]=0;
map[f+1][h]=0;map[f+1][h+1]=1;map[f+1][h+2]=1;
map[f+2][h]=0;map[f+2][h+1]=0;map[f+2][h+2]=0;
break;
case 'C':
map[f][h]=0;map[f][h+1]=0;map[f][h+2]=0;
map[f+1][h]=1;map[f+1][h+1]=1;map[f+1][h+2]=0;
map[f+2][h]=0;map[f+2][h+1]=1;map[f+2][h+2]=0;
break;
case 'D':
map[f][h]=0;map[f][h+1]=0;map[f][h+2]=0;
map[f+1][h]=0;map[f+1][h+1]=1;map[f+1][h+2]=1;
map[f+2][h]=0;map[f+2][h+1]=1;map[f+2][h+2]=0;
break;
case 'E':
map[f][h]=0;map[f][h+1]=1;map[f][h+2]=0;
map[f+1][h]=0;map[f+1][h+1]=1;map[f+1][h+2]=0;
map[f+2][h]=0;map[f+2][h+1]=1;map[f+2][h+2]=0;
break;
case 'F':
map[f][h]=0;map[f][h+1]=0;map[f][h+2]=0;
map[f+1][h]=1;map[f+1][h+1]=1;map[f+1][h+2]=1;
map[f+2][h]=0;map[f+2][h+1]=0;map[f+2][h+2]=0;
break;
case 'G':
map[f][h]=0;map[f][h+1]=1;map[f][h+2]=0;
map[f+1][h]=1;map[f+1][h+1]=1;map[f+1][h+2]=1;
map[f+2][h]=0;map[f+2][h+1]=0;map[f+2][h+2]=0;
break;
case 'H':
map[f][h]=0;map[f][h+1]=1;map[f][h+2]=0;
map[f+1][h]=1;map[f+1][h+1]=1;map[f+1][h+2]=0;
map[f+2][h]=0;map[f+2][h+1]=1;map[f+2][h+2]=0;
break;
case 'I':
map[f][h]=0;map[f][h+1]=0;map[f][h+2]=0;
map[f+1][h]=1;map[f+1][h+1]=1;map[f+1][h+2]=1;
map[f+2][h]=0;map[f+2][h+1]=1;map[f+2][h+2]=0;
break;
case 'J':
map[f][h]=0;map[f][h+1]=1;map[f][h+2]=0;
map[f+1][h]=0;map[f+1][h+1]=1;map[f+1][h+2]=1;
map[f+2][h]=0;map[f+2][h+1]=1;map[f+2][h+2]=0;
break;
case 'K':
map[f][h]=0;map[f][h+1]=1;map[f][h+2]=0;
map[f+1][h]=1;map[f+1][h+1]=1;map[f+1][h+2]=1;
map[f+2][h]=0;map[f+2][h+1]=1;map[f+2][h+2]=0;
break;
}
h+=3;
}
f+=3;
}
yy=0;
for(i=0;i<n*3;i++)
for(j=0;j<m*3;j++)
if(map[i][j]==1){
dfs(i,j);
yy++;
}
printf("%d\n",yy);
}
return 0;
}