Saving Tang Monk II HihoCoder - 1828(2018北京网络赛三维标记+bfs)
《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng’en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujing, escorted Tang Monk to India to get sacred Buddhism texts.
During the journey, Tang Monk was often captured by demons. Most of demons wanted to eat Tang Monk to achieve immortality, but some female demons just wanted to marry him because he was handsome. So, fighting demons and saving Monk Tang is the major job for Sun Wukong to do.
Once, Tang Monk was captured by the demon White Bones. White Bones lived in a palace and she cuffed Tang Monk in a room. Sun Wukong managed to get into the palace, and he wanted to reach Tang Monk and rescue him.
The palace can be described as a matrix of characters. Different characters stand for different rooms as below:
‘S’ : The original position of Sun Wukong
‘T’ : The location of Tang Monk
‘.’ : An empty room
‘#’ : A deadly gas room.
‘B’ : A room with unlimited number of oxygen bottles. Every time Sun Wukong entered a ‘B’ room from other rooms, he would get an oxygen bottle. But staying there would not get Sun Wukong more oxygen bottles. Sun Wukong could carry at most 5 oxygen bottles at the same time.
‘P’ : A room with unlimited number of speed-up pills. Every time Sun Wukong entered a ‘P’ room from other rooms, he would get a speed-up pill. But staying there would not get Sun Wukong more speed-up pills. Sun Wukong could bring unlimited number of speed-up pills with him.
Sun Wukong could move in the palace. For each move, Sun Wukong might go to the adjacent rooms in 4 directions(north, west,south and east). But Sun Wukong couldn’t get into a ‘#’ room(deadly gas room) without an oxygen bottle. Entering a ‘#’ room each time would cost Sun Wukong one oxygen bottle.
Each move took Sun Wukong one minute. But if Sun Wukong ate a speed-up pill, he could make next move without spending any time. In other words, each speed-up pill could save Sun Wukong one minute. And if Sun Wukong went into a ‘#’ room, he had to stay there for one extra minute to recover his health.
Since Sun Wukong was an impatient monkey, he wanted to save Tang Monk as soon as possible. Please figure out the minimum time Sun Wukong needed to reach Tang Monk.
Input
There are no more than 25 test cases.
For each case, the first line includes two integers N and M(0 < N,M ≤ 100), meaning that the palace is a N × M matrix.
Then the N×M matrix follows.
The input ends with N = 0 and M = 0.
Output
For each test case, print the minimum time (in minute) Sun Wukong needed to save Tang Monk. If it’s impossible for Sun Wukong to complete the mission, print -1
Sample Input
2 2
S#
#T
2 5
SB###
##P#T
4 7
SP…
P#…
…#
B…##T
0 0
Sample Output
-1
8
11
題意:孫悟空救唐僧,#代表著毒氣室,只有有氧氣管的時候才能進入,出了毒氣室要休息一分鐘。B代表著氧氣室,最多可以帶5瓶氧氣。P代表著可以加速一分鐘。. 代表著空閑地方。問孫悟空從s到達t的最少時間是多少。
之前還沒做到過這樣的題目,之前的廣搜就是判斷這個點有沒有走到過,走到就不能走了。但是這個題要是這樣做的話就不對了,也非常麻煩。我們用vis[x][y][num]代表著走到(x,y)還剩num瓶氧氣。如果到達一個點它的氧氣瓶的數量之前出現過,那么這種狀態之前也出現過。廣搜暴力去找所有的狀態,直到找到了唐僧。以為在出了毒氣室的時候,需要休息一分鐘,就代表著進毒氣室需要2分鐘,這樣的話就需要用到優先隊列來維護了。
代碼如下:
努力加油a啊,(o)/~
總結
以上是生活随笔為你收集整理的Saving Tang Monk II HihoCoder - 1828(2018北京网络赛三维标记+bfs)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Greedy Sequence(2019
- 下一篇: Distance on the tree