• 杭电1010Tempter of the Bone


    Tempter of the Bone
    Problem Description

    The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.
    The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

    Input

    The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
    'X': a block of wall, which the doggie cannot enter;
    'S': the start point of the doggie;
    'D': the Door; or
    '.': an empty block.
    The input is terminated with three 0's. This test case is not to be processed.

    Output

    For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.

    Sample Input

    4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0

    Sample Output

    NO YES

    第一次做深搜索题目,这一道应该算简单的吧,参考了别人的代码写的,不过收获挺多的,下面的一句话,应该可以大概的概括搜索的一般解法吧:

    所有的搜索算法从其最终的算法实现上来看,都可以划分成两个部分:控制结构产生系统,也应该就是穷举着,并且筛选着。

    奇偶剪枝:t-[abs(ex-sx)+abs(ey-sy)] 结果为非偶数(奇数),则无法在t步恰好到达;

    参考百度百科:http://baike.baidu.com/link?url=vS7Q7XC5UXvuJomzh-HLq8LJV2-ql0RTxfFEtQPSOIQDS3bnF48bHCkfYMH57hsIv_suGQYN8PaQBPNrFDvDr_#2

    //N行迷宫的长度,T是开门的时间,M是每行的字符
    //(1 < N, M < 7; 0 < T < 50), 
    
    #include<iostream>
    #include<cmath>
    using namespace std;
    int n,m,ex,ey,t;
    bool success;
    char maze[10][10];       
    void dfs(int stx,int sty,int dt)
    {
        if(stx<=0 || stx >n || sty<=0 ||sty > m)
            return ;
    
        if(stx==ex && sty == ey && dt==t)       //成功逃脱
        {    success = true;return ;}
    
        int temp = (t-dt) - abs(ex-stx) - abs(ey-sty);    
        
        if(temp < 0 || temp & 1)    //奇偶剪枝 ,一个奇数&1是0
            return;
    
        //然后是对上下左右进行搜索
        if(maze[stx][sty+1]!='X')           //向上搜索
        {
            maze[stx][sty+1]='X';        //把block堵上后在进行dfs;
                dfs(stx,sty+1,dt+1);
            maze[stx][sty+1]= '.';      //恢复自由
        }
    
        if(maze[stx+1][sty]!='X')           //向右搜索
        {
            maze[stx+1][sty]='X';        
                dfs(stx+1,sty,dt+1);
            maze[stx+1][sty]= '.';
        }
        
        if(maze[stx][sty-1]!='X')           //向下搜索
        {
            maze[stx][sty-1]='X';      
                dfs(stx,sty-1,dt+1);
            maze[stx][sty-1]= '.';
        }
    
        if(maze[stx-1][sty]!='X')           //向左搜索
        {
            maze[stx-1][sty]='X';      
                dfs(stx-1,sty,dt+1);
            maze[stx-1][sty]= '.';
        }
        return;
    }
    
    
    int main()
    {
        int i,j;
        int stx,sty,wall;
        while(cin>>n>>m>>t,n+m+t)
        {        
            wall = 0;
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=m;j++)
                {
                    cin>>maze[i][j];
                
                    if(maze[i][j]=='S')     //判定起点
                    {    stx = i;sty = j;}
                    
                    if(maze[i][j]=='D')     //判定终点
                    {    ex = i;ey = j;}
                    
                    if(maze[i][j]=='X')     //判断墙壁数量                
                        wall++;
                
                }
            }
            success = false;             //其实逃脱的成功率是渺茫的吧!
            maze[stx][sty] = 'X';             //堵住入口
            if(n*m-wall<=t)                   //当可以走的block大于时间才可能到达door
                cout<<"NO"<<endl;
            else
            {
                dfs(stx,sty,0);
                if(success)
                    cout<<"YES"<<endl;
                else
                    cout<<"NO"<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    值类型和引用类型
    0513二分查找练习
    0512随机4位验证码
    0511java 随机6个不同的彩票数
    随机数的产生机制
    0510Java 练习
    0509java练习题
    java循环作业
    字符集的由来及发展
    hdu2577_键入字母
  • 原文地址:https://www.cnblogs.com/LZYY/p/3448482.html
Copyright © 2020-2023  润新知