LeeBlog

导航

HDU 1312 Red and Black

这题忒easy了。。。。直接搜吧。。 都不用回溯的

#include<stdio.h>
#include<string.h>
int map[25][25],des[25][25],n,m,sx,sy;
void DFS( int y,int x )
{
     if( map[y][x] == '#'||des[y][x]||y > n || x > m )
         return ;
     des[y][x] = 1;
     DFS( y - 1,x );
     DFS( y + 1,x );
     DFS( y,x - 1 );
     DFS( y,x + 1 );
 }
int main( )
{
    while( scanf( "%d%d%*c",&m,&n ),n||m )
    {//n跟m别弄错了。。。 
           for( int i = 0; i < 25; ++i )
                for( int j = 0; j < 25; ++j )
                     map[i][j] = '#',des[i][j] = 0;
           for( int i = 1; i <= n; ++i )
           {
                for( int j = 1; j <= m; ++j )
                {
                     scanf( "%c",&map[i][j] );
                     if( map[i][j] == '@' )
                         sy = i,sx = j;
                     }
                getchar();
           }
           DFS( sy,sx );
           int c = 0,i,j;
           for( i = 1; i <= n; ++i )
                for( j = 1; j <= m; ++j )
                     if( des[i][j] )
                         ++c;
           printf( "%d\n",c );
           }
    return 0;
}

posted on 2011-05-11 10:29  LeeBlog  阅读(174)  评论(0编辑  收藏  举报