HDU 1312 Red and Black

简单搜索题:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
class Node
{
public:
int x,y;
};
Node queue[424];
int d[4][2]={0,-1,1,0,0,1,-1,0};
char map[24][24];
int BFS( int x, int y )
{
int end =0 ,first = 0;
Node t;
t.x = x;
t.y = y;
queue[end]=t;
end++;
while( first < end )
{
for( int i=0; i<4 ; i++ )
{
int dx = queue[first].x + d[i][0];
int dy = queue[first].y + d[i][1];
if( map[dx][dy] == '.' )
{
t.x = dx;
t.y = dy;
map[dx][dy]='#';
queue[end] = t;
end++;
}
}
first++;
}
return end;
}
int main( )
{
int n,m;
while( scanf( "%d%d",&n,&m ),n )
{
memset( map , 0, sizeof( map ) );
int x,y;
for( int i = 1 ; i<= m ;i++ )
{
scanf( "%s",map[i]+1 );
for( int j = 1; j<= n ;j++ )
{
if( map[i][j]=='@' )
{
x = i ;
y = j;
break;
}
}
}
map[x][y] = '#';
printf( "%d\n",BFS( x , y ) );
}
return 0;
}

 

posted @ 2012-02-28 22:20  wutaoKeen  阅读(140)  评论(0编辑  收藏  举报