现在的位置: 首页 > 综合 > 正文

HDU-1035 Robot Motion DFS

2012年11月01日 ⁄ 综合 ⁄ 共 797字 ⁄ 字号 评论关闭

  可恶的模拟题,刚进入的那一步竟然不算,贡献了多次WA啊,只要注意这点应该就木有问题了。

#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;

int N, M, sx, sy;

char map[15][15], hash[15][15];

bool out( int x, int y )
{
    if( x< 1|| x> N|| y< 1|| y> M )
    {
        return true;
    }
    return false;
}

bool DFS( int &step, int &loop, int x, int y, int s )
{
	hash[x][y]= s;
    if( map[x][y]== 'W' ) y-= 1;
    else if( map[x][y]== 'E' ) y+= 1;
    else if( map[x][y]== 'N' ) x-= 1;
    else if( map[x][y]== 'S' ) x+= 1;
    if( out( x, y ) )
    {
        step= s+ 1;
        return true;
    }
    else
    {
        if( hash[x][y]== -1 )
        {
            return DFS( step, loop, x, y, s+ 1 );
        }
        else
        {
            step= hash[x][y];
            loop= s- hash[x][y]+ 1;
            return false;
        }
    }
}

int main(  )
{
    while( scanf( "%d %d", &N, &M ), N| M )
    {
        scanf( "%d", &sy );
        int step, loop;
        memset( hash, -1, sizeof( hash ) );
        for( int i= 1; i<= N; ++i )
        {
            scanf( "%s", map[i]+ 1 );
        }
        if( DFS( step, loop, 1, sy, 0 ) )
        {
            printf( "%d step(s) to exit\n", step );
        }
        else
        {
            printf( "%d step(s) before a loop of %d step(s)\n", step, loop );
        }
    }
    return 0;
}

抱歉!评论已关闭.