HDU 1035 Robot Motion

简单的搜索题:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int flag,Count,step;
char map[15][15];
int STEP[15][15];
bool hash[15][15];
void DFS( int x, int y, int cnt )
{

if( hash[x][y] )
{
// flag =2;
Count = cnt - STEP[x][y];
step = STEP[x][y];
return;
}
if( flag||map[x][y]==0 )
{
flag = 1;
Count = cnt;
return;
}
STEP[x][y] = cnt;
hash[x][y] = 1;
if( map[x][y]=='E' )
{
DFS( x , y +1 ,cnt+1 );
}
else
{
if( map[x][y]=='S' )
{
DFS( x+1 , y ,cnt+1 );
}
else
{
if( map[x][y]=='W' )
{
DFS( x , y -1 ,cnt+1 );
}
else
{
DFS( x-1 , y ,cnt+1 );
}
}
}
return;
}
int main( )
{
int n,m,start;
while( scanf( "%d%d",&n,&m ),n||m )
{
flag = 0;
memset( map , 0 ,sizeof( map ) );
memset( STEP , 0 , sizeof( STEP ) );
memset( hash , 0 , sizeof( hash ) );
scanf( "%d",&start );
for( int i = 1 ;i<= n ; i++ )
scanf( "%s",map[i]+1 );
DFS( 1 , start,0 );
if( flag ) printf( "%d step(s) to exit\n",Count );
else printf( "%d step(s) before a loop of %d step(s)\n",step, Count );
}
return 0;
}

 

posted @ 2012-03-01 13:07  wutaoKeen  阅读(120)  评论(0编辑  收藏  举报