pku1979

#include < stdio.h >
#include
< stdlib.h >
#include
< string .h >
int  visited[ 100 ][ 100 ];
char  str[ 100 ][ 100 ];
int  i,j,n,m,move[ 4 ][ 2 ] = {{ 0 , 1 },{ 0 , - 1 },{ 1 , 0 },{ - 1 , 0 }},count,max;
int  f( int  x, int  y)
{
    
if (x >= 0 && y >= 0 && x < n && y < m)
        
return   1 ;
    
else  
        
return   0 ;
}
void  dfs( int  i, int  j)
{
    
int  k,x = i,y = j;

    
if (str[x][y] == ' . ' && f(x,y))
    {
        
++ count;
        

    visited[x][y]
= 1 ;

        
for (k = 0 ;k < 4 ;k ++ )
        {   

         x
= i + move[k][ 0 ];y = j + move[k][ 1 ];
           
if (str[x][y] == ' . ' && f(x,y) && ( ! visited[x][y]))
            
            dfs(x,y);
        }
    }
}
int  main()
{
    
int  x = 0 ,y = 0 ,i,j;
    
    
while (scanf( " %d %d " , & m, & n))
    {
        
if (n == 0 && m == 0 )
            
break ;
        memset(str,
0 , sizeof (str));
        
for (i = 0 ;i < n;i ++ )
            scanf(
" %s " ,str[i]);
        count
= 0 ;
        
for (i = 0 ;i < n;i ++ )
            
for (j = 0 ;j < m;j ++ )
            {
                
if (str[i][j] == ' @ ' )
                {
                    x
= i;y = j;
            
                }
                visited[i][j]
= 0 ;
            }
            str[x][y]
= ' . ' ;
         
            dfs(x,y);
            printf(
" %d\n " ,count);
            memset(visited,
0 , sizeof (visited));


    }
    
// system("pause");
    
    
return   0 ;
        
}

你可能感兴趣的:(pku)