HDU1010 Tempter of the Bone DFS+剪枝
點擊打開鏈接
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 141796????Accepted Submission(s): 37833
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
Recommend
JGShining???|???We have carefully selected several similar problems for you:??1016?1242?1072?1312?1026
題意:
輸入一個n*m的迷宮,T為可以在迷宮中生存的最大時間,。S為起點,D為終點。.為路,X為障礙,每個格子只能踩一次,且只能維持一秒,然后該塊地板就會塌陷。所以你必須每秒走一步,且到D點時,所用時間為T。
題解:
從起點開始向上下左右四個方向搜索,遇到路“."則繼續向下搜索,遇到障礙”X“則結束搜索;當走到D點時且T=0時,輸出YES,否則為NO;這樣樸素的搜索會超時,所以需要剪枝
剪枝1:
如果剩余時間T小于當前點到終點的最短距離,則這條路徑不可能按時到大,停止向下搜索
最短路徑=abs(ex-x)+abs(ey-y);
剪枝2:
初始化n*m的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
下標從1開始,a【i】【j】=(i+j)% 2
在地圖上,權值相同的兩點距離為偶數
權值不同的兩點距離為奇數
若(a【x】【y】+a【ex】【ey】)為偶數,
如果剩余時間t為偶數,則可能到達
如果剩余時間t為奇數,則無法到達
若(a【x】【y】+a【ex】【ey】)為奇數,
如果剩余時間t為偶數,則無法到達
如果剩余時間t為奇數,則可能到達
綜上,
若(a【x】【y】+a【ex】【ey】+t)為偶數,
則可能到達
如果為奇數,則無法到達
?
總結
以上是生活随笔為你收集整理的HDU1010 Tempter of the Bone DFS+剪枝的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU1426 Sudoku Kille
- 下一篇: HDU1045 Fire Net 递归回