博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1010 Tempter of the Bone(DFS+剪枝)
阅读量:7136 次
发布时间:2019-06-28

本文共 2666 字,大约阅读时间需要 8 分钟。

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 5S.X...X...XD....3 4 5S.X...X....D0 0 0

Sample Output

NOYES 题意:   题意可以理解为从起点到终点恰好只经过T步路。 题解:      刚开始想到用BFS来做,代码写了写,改了改,然后答案就是不对。BFS适合用来求解最短路,但是对于这种题,规定只经过T步路到达终点,这时候BFS就无能为力了。 DFS搜索,注意回溯。还要剪枝,根据步数的奇偶性。起点到终点的步数和时间T同为奇数或偶数时,代表可能有解,否则,一定不可能在T时间准确到达终点。(具体可以手画一下坐标系)
#include
#include
#include
#include
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
<=ny&&ny
>n>>m>>t,n||m||t) { cnt=0; int sx,sy; for(int i=0;i
>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

 

 

转载于:https://www.cnblogs.com/orion7/p/7326133.html

你可能感兴趣的文章
C#基础:多态:基类可以定义并实现虚(virtual)方法,派生类可以重写(override)这些方法...
查看>>
Visifire图表
查看>>
python常用模块之paramiko与ssh
查看>>
AES算法在Python中的使用
查看>>
动手动脑3
查看>>
HDU 3397 Sequence operation(线段树区间染色加区间合并)
查看>>
【随笔】写下现在所想的,开始写博客
查看>>
linux命令之vi文本编辑器
查看>>
WPF-WrapPanel
查看>>
大家好
查看>>
iOS 疑难杂症 — — UITableView 添加 tableFooterView 旋转屏幕后收不到点击事件!!!...
查看>>
asp.net 8 Request,Response,Server
查看>>
Windows API 技巧集
查看>>
Binary Tree Level Order Traversal [LEETCODE]
查看>>
nginx代理天地图做缓存解决跨域问题
查看>>
HDU_1542_线段树【扫描线】
查看>>
[转]Oracle数据库ASH和AWR的简单介绍
查看>>
客户单操作Cookie
查看>>
Swift -- enum 继承 protocol
查看>>
Java基础 - 流程控制语句
查看>>