zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    leetcode_138复制带随机指针的链表
    minSTL
    LLVM
    STL基础_迭代器
    mysql数据库表清空后id如何从1开始自增
    explain用法和结果分析
    MySQL多表查询与子查询
    数据结构与算法笔记
    MySQL数据库的SQL语言与视图
    mysql忘记密码解决方案
  • 原文地址:https://www.cnblogs.com/Lyush/p/2136140.html
Copyright © 2011-2022 走看看