• HDU1035 Robot Motion DFS


      可恶的模拟题,刚进入的那一步竟然不算,贡献了多次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;
    }
    
  • 相关阅读:
    好题记录
    「同余数论」学习笔记
    「网络流」学习笔记
    物理知识相关内容总结
    「多项式」学习笔记
    「数论函数」学习笔记
    「点分治」学习笔记
    「线性基」学习笔记
    「后缀自动机」学习笔记
    「后缀数组」学习笔记
  • 原文地址:https://www.cnblogs.com/Lyush/p/2136140.html
Copyright © 2020-2023  润新知