Farm Irrigation

/***********需要多少水源***********************/

Farm Irrigation Farm Irrigation

3 3  ADC   FJK        IHE

代码
   
     
#include < stdio.h >
#define N 51
bool con[ 11 ][ 4 ] = {{ 1 , 0 , 1 , 0 },{ 0 , 1 , 1 , 0 },{ 1 , 0 , 0 , 1 },{ 0 , 1 , 0 , 1 },{ 0 , 0 , 1 , 1 },
{
1 , 1 , 0 , 0 },{ 1 , 1 , 1 , 0 },{ 1 , 0 , 1 , 1 },{ 1 , 1 , 0 , 1 },{ 0 , 1 , 1 , 1 },{ 1 , 1 , 1 , 1 }}; // w,e,s,n
int set [N * N];
int R,C;
void Make_set( int set [])
{
int i;
for (i = 0 ;i < R * C;i ++ )
set [i] = i;
}
int Find_set( int n)
{
if ( set [n] != n)
return Find_set( set [n]);
return n;
}
void Union( int a, int b)
{
set [a] = set [b];
}
int EJudge( char a, char b)
{
if (con[a - ' A ' ][ 1 ] == 1 && con[b - ' A ' ][ 0 ] == 1 )
return 1 ;
return 0 ;
}
int NJudge( char a, char b)
{
if (con[a - ' A ' ][ 3 ] == 1 && con[b - ' A ' ][ 2 ] == 1 )
return 1 ;
return 0 ;
}
int main()
{
int i,j,s1,s2,need;
char map[N][N];
while (scanf( " %d%d " , & R, & C),R !=- 1 || C !=- 1 ){
for (i = 0 ;i < R;i ++ )
scanf(
" %s " ,map[i]);
Make_set(
set );
for (i = 0 ;i < R;i ++ ){
for (j = 0 ;j < C;j ++ ){
if (j + 1 < C && EJudge(map[i][j],map[i][j + 1 ])){
s1
= Find_set( set [i * C + j]);s2 = Find_set( set [i * C + j + 1 ]);
set [i * C + j] = s1; set [i * C + j + 1 ] = s2;
if (s1 != s2){
Union(s1,s2);
}
}
if (i + 1 < R && NJudge(map[i][j],map[i + 1 ][j])){
s1
= Find_set( set [i * C + j]);s2 = Find_set( set [(i + 1 ) * C + j]);
set [i * C + j] = s1; set [(i + 1 ) * C + j] = s2;
if (s1 != s2){
Union(s1,s2);
}
}
}
}
need
= 0 ;
for (i = 0 ;i < R * C;i ++ ){
if ( set [i] == i)need ++ ;
}
printf(
" %d\n " ,need);
}
return 0 ;
}

你可能感兴趣的:(ARM)