zoukankan      html  css  js  c++  java
  • DFS(4)——hdu1010Tempter 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;
    '.': 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
     
    题意:小狗从点'S'出发,每秒走一步,且每个点只能经过一次,请问它能否恰好在第T秒达到点'D' 。
     
    二、解题思路
    • DFS
    • 剪枝

    注意,这道题目是要恰好t时间到达,并不是在t时间内到达......

    第一个剪枝我们可以想到,当剩下的步数大于剩下的时间的时候,狗是不能走到的;
     
    接下来我们来第二个剪枝
    我们把map的奇偶性以01编号:
    0 1 0 1 0 1 
    1 0 1 0 1 0 
    0 1 0 1 0 1 
    1 0 1 0 1 0 
    0 1 0 1 0 1 
    我们发现从0走一步一定走到1,从1走一步一定走到0。
    也就是说,如果当前的狗所在的坐标与D的坐标奇偶性不一样,那么狗需要走奇数步。
    同理,如果狗所在坐标与D的坐标奇偶性一样,那么狗需要走偶数步数。
     
    也就是说,狗的横坐标x与纵坐标y的和对2取余是它的奇偶性,D的横坐标x与纵坐标y的和对2取余是D的奇偶性。
    两个奇偶性相加再对2取余,拿这个余数去与剩下时间对2取余的余数作比较即可(不同即不可能)。
     
    附图:
     
    三、代码
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    
    int n,m,t;                            
    int sx,sy,dx,dy;
    int flag;                        
    char a[10][10];                        
    bool vis[10][10];                                //标记某个点是否已经过 
    int to[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    
    void dfs(int x,int y,int k)                        //小狗当前的坐标(x,y) 及 第 k 秒 
    {
        if(k>t)    return;                                //超过规定时间 
        if(k==t && !(x==dx&&y==dy))    return;            //任务失败 
        if(k==t && x==dx && y==dy){                    //任务成功 
            flag = 1;                                //此标记为了让调用回到主函数 
            return;                                    
        }
        for(int i=0;i<4;i++){            //小狗在点(x,y)向四个方向走 
            int mx = x+to[i][0];
            int my = y+to[i][1];
            if(a[mx][my]!='X' && !vis[mx][my]){
                if(mx<1 || mx>n || my<1 || my>m)    continue;    //越界 
                vis[mx][my] = 1;
                dfs(mx,my,k+1);
                if(flag)    return;
                vis[mx][my] = 0;
            }
        }
    } 
    
    int main()
    {
        while(scanf("%d%d%d",&n,&m,&t) && !(n==0&&m==0&&t==0)){
            for(int i=1;i<=n;i++){
                getchar();
                for(int j=1;j<=m;j++){
                    scanf("%c",&a[i][j]);
                    if(a[i][j] == 'S'){
                        sx = i;
                        sy = j;
                    }
                    if(a[i][j] == 'D'){
                        dx = i;
                        dy = j;
                    }
                }
            }
            getchar();
            
            if(t<abs(dx+dy-sx-sy)){                    //剪枝一 
                printf("NO
    ");    continue;
            }
            int a = (sx+sy)%2, b = (dx+dy)%2;        //剪枝二 
            int c = (a+b)%2, d = t%2;
            if(c != d){
                printf("NO
    ");    continue;
            }
            
            flag = 0;
            memset(vis,0,sizeof(vis));
            vis[sx][sy] = 1;
            dfs(sx,sy,0);
            if(flag == 1)    printf("YES
    ");
            else    printf("NO
    ");
        }
        return 0;
    } 
    View Code
     
     
  • 相关阅读:
    matlab函数集锦
    视觉(1)[导入]EStereo
    我为啥申请这里的博客?
    [导入]给定一个英文原文,统计文件里面一共有多少个不同的英文单词
    视觉(2)[导入]opengl贴图程序
    C++ Builder 控件的卸载<转载>
    [转]<不知道能否解决先转下来再说>不显示删除回复显示所有回复显示星级回复显示得分回复 没有找到MSVR90D.dll因此这个应用程序未能启动
    终于把《windows程序设计》上册读完了,hoho
    乱思。。。。。。。、、、、、
    测试windows live writer
  • 原文地址:https://www.cnblogs.com/xzxl/p/7301761.html
Copyright © 2011-2022 走看看