深度优先搜索——HDU1010

在一张地图上,给定起点和终点,问能否恰好在t时刻到达终点?
-来源HDU1010
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

Author
ZHANG, Zheng

Source
ZJCPC2004

题意:输入一个n*m的迷宫,和一个T:可以在迷宫中生存的最大时间。S为起点,D为终点。并且,每个格子只能踩一次,且只能维持一秒,然后该块地板就会塌陷。所以你必须每秒走一步,且到D点时,所用时间为T

方法:
深搜+奇偶减枝
奇偶剪枝是数据结构的搜索中,剪枝的一种特殊小技巧。
奇偶剪枝百度百科

    #include<iostream>  
    using namespace std;  
    int sx,sy,ex,ey;  
    int n,m;  
    char map[10][10];    
    int flag,step;  
    int d[4][2]={0,1,1,0,0,-1,-1,0};  
    void dfs(int x,int y,int t)  
    {   
        if(flag==1) return ;                                           //若找到所需解,则不用再找其余路径   
        if(t<abs(ex-x)+abs(ey-y)||(t-abs(ex-x)+abs(ey-y))%2) return ;  //剪枝。若剩余时间-当前点到终点的横纵坐标之和,为偶数  
                                                                       //则,该路,可走。(若无障碍,差值为0,若有障碍则绕道  
                                                                       //即差值-1+(n*2+3)为偶数)                                                                 
        else if(t==0)                //t代表剩余时间,每走一步t--,这里若t=0,则迷宫坍塌   
        {  
             if(x==ex&&y==ey)  {flag=1; return ;}    //若此时,t=0且刚好到达终点,则标记找到所需解。   
             else { return ;  }    
        }     
        else  
        for(int i=0;i<4;i++)                  //每次向4个方向探。   
        {  
                int nx=x+d[i][0],ny=y+d[i][1];  
                if (nx>0&&nx<=n&&ny>0&&ny<=m&&(map[nx]
                [ny]=='.'||map[nx][ny]=='D')) //若该点不为墙,则为可走   
                {  
                       map[nx][ny]='X';                    //标记走过   
                       dfs( nx,ny,t-1) ;  
                       map[nx][ny]='.';                   //回溯   
                }  

        }  
        return ;  
    }  
    int main()  
    {  


        char str[10];    
        int t;  
        while (scanf("%d%d%d",&n,&m,&t)!=EOF)  
        {     
              if(n==0&&m==0&&t==0) return 0;  
              for (int i=1;i<=n;i++)  
              {  
                  scanf("%s",str);  
                  for (int j=1;j<=m;j++)  
                  {  
                      map[i][j]=str[j-1];     
                      if(map[i][j]=='S')  sx=i,sy=j;  
                      else if(map[i][j]=='D') ex=i,ey=j;  
                  }  
              }           
              flag=0;  
              dfs(sx,sy,t);  
              if(flag==0) printf("NO\n");  
              else printf("YES\n");   
        }  
        return 0;  
    }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值