zoukankan      html  css  js  c++  java
  • HDU 1010 Tempter of the Bone(DFS+剪枝)

    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. 

    InputThe 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. 
    OutputFor 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步路。
    题解:
      
      刚开始想到用BFS来做,代码写了写,改了改,然后答案就是不对。BFS适合用来求解最短路,但是对于这种题,规定只经过T步路到达终点,这时候BFS就无能为力了。
    DFS搜索,注意回溯。还要剪枝,根据步数的奇偶性。起点到终点的步数和时间T同为奇数或偶数时,代表可能有解,否则,一定不可能在T时间准确到达终点。(具体可以手画一下坐标系)
    #include<iostream>
    #include<queue>
    #include<cstring>
    #include<cmath>
    using namespace std;
    char maze[10][10];
    bool flag;
    int n,m,ex,ey,cnt,t;
    int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
    void dfs(int x,int y,int s)
    {
        if(x==ex&&y==ey&&s==t)
        {
            flag=true;
            return ;
        }
        if(abs(x-ex)+abs(y-ey)+s>t)//如果目前最短到终点的距离大于T就回溯
            return ;
        if((t-s-abs(x-ex)-abs(y-ey))&1)//根据步数的奇偶性判断,差值为奇数就一定不能在T秒到达终点
            return ;
        for(int i=0;i<4;i++)
        {
            int nx=x+dx[i],ny=y+dy[i];
            if(0<=nx&&nx<n&&0<=ny&&ny<m&&maze[nx][ny]!='X')
            {
                maze[nx][ny]='X';
                dfs(nx,ny,s+1);
                if(flag)//如果能到达终点就返回
                    return ;
                maze[nx][ny]='.';//回溯,状态复原
            }
        }
    }
    int main()
    {
        while(cin>>n>>m>>t,n||m||t)
        {
            cnt=0;
            int sx,sy;
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                {
                    cin>>maze[i][j];
                    if(maze[i][j]=='S')
                    {
                        sx=i;
                        sy=j;
                    }
                    if(maze[i][j]=='D')
                    {
                        ex=i;
                        ey=j;
                    }
                    if(maze[i][j]=='X')
                        cnt++;
                }
            flag=false;
            if(n*m-cnt<t)
                cout<<"NO"<<endl;
            else
            {
                maze[sx][sy]='X';//不要忘了,标记起点,第一次WA就在这里。
                dfs(sx,sy,0);
                if(flag)
                    cout<<"YES"<<endl;
                else
                    cout<<"NO"<<endl;
            }
        }
        return 0;
    }
     
  • 相关阅读:
    洛谷P3953 逛公园
    洛谷P1247 取火柴游戏
    洛谷P2024 食物链
    洛谷P2680 运输计划
    hdu 1495 非常可乐(bfs)
    poj3984 迷宫问题(简单的输出路径的bfs)
    Codeforces 729C Road to Cinema(二分)
    Codeforces Technocup 2017
    Codeforces Technocup 2017
    poj 2251 Dungeon Master(bfs)
  • 原文地址:https://www.cnblogs.com/orion7/p/7326133.html
Copyright © 2011-2022 走看看