Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5932 Accepted Submission(s): 2564
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.
#include<stdio.h>
#include<string.h>
int maps[400][400],m,n;
int p[13][3][3]={
{
0,1,0,
1,1,0,
0,0,0
},
{
0,1,0,
0,1,1,
0,0,0,
},
{
0,0,0,
1,1,0,
0,1,0,
},
{
0,0,0,
0,1,1,
0,1,0,
},
{
0,1,0,
0,1,0,
0,1,0,
},
{
0,0,0,
1,1,1,
0,0,0,
},
{
0,1,0,
1,1,1,
0,0,0,
},
{
0,1,0,
1,1,0,
0,1,0,
},
{
0,0,0,
1,1,1,
0,1,0,
},
{
0,1,0,
0,1,1,
0,1,0,
},
{
0,1,0,
1,1,1,
0,1,0,
}
};
void getbuld(char c,int h,int l);
void dfs(int i,int j);
int main()
{
int i,j,l,sum;
char c;
while(scanf("%d%d",&n,&m)&&n!=-1&&m!=-1)
{
sum=0;
memset(maps,0,sizeof maps);
getchar();
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%c",&c);
getbuld(c,i,j);
}
getchar();
}
for(i=0;i<3*n;i++)
{
for(j=0;j<3*m;j++)
if(maps[i][j]==1)
{
dfs(i,j);
sum++;
}
}
printf("%d\n",sum);
}
return 0;
}
void getbuld(char c,int h,int l)
{
int k=c-'A';
int i,j,h1=0,l1=0;
for(i=3*h;i<3*h+3;i++,h1++)
{
for(j=3*l;j<3*l+3;j++,l1++)
{
maps[i][j]=p[k][h1][l1];
}
l1=0;
}
}
void dfs(int i,int j)
{
if(maps[i][j]==0)
return ;
else
maps[i][j]=0;
if(i-1>=0&&maps[i-1][j]==1)
{
dfs(i-1,j);
}
if(j-1>=0&&maps[i][j-1]==1)
{
dfs(i,j-1);
}
if(i+1<3*n&&maps[i+1][j]==1)
{
dfs(i+1,j);
}
if(j+1<3*m&&maps[i][j+1]==1)
{
dfs(i,j+1);
}
}