1. 程式人生 > >Saving Tang Monk II (bfs)

Saving Tang Monk II (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.

輸入

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.

輸出

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

樣例輸入

2 2
S#
#T
2 5
SB###
##P#T
4 7
SP.....
P#.....
......#
B...##T
0 0

樣例輸出

-1
8
11

題意:在n*m的矩陣裡面,‘.’代表這點為空,可走。通過P點可以獲得一個加速包,可以在當前這條路上抵消一個時間。通過B點可以獲得一個氧氣,最多獲得5個,一直呆在B點不能累加氧氣。通過#需要消耗一個氧氣,並且還需要在原地再呆一個時間。求從S ->T 的最短時間。

思路:遇到不同點特殊處理一下即可。由於最多可以攜帶5瓶氧氣,那麼我們標記每個點時可以把氧氣的數量也當作一個狀態book[x][y][z]  (x,y)點z個氧氣的時候,處理P點時,步數不增加。然後用優先佇列bfs一波即可。

程式碼如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define ll long long
#include<algorithm>
using namespace std;
int n,m,a,b;
int book[110][110][10];
int to[4][2]= {0,1,1,0,0,-1,-1,0};
char mp[110][110];

struct node
{
    int x,y,step,b;
    bool operator < (const node &a)const //過載最小步數
    {
        return a.step<step;
    }
};
void bfs()
{
    priority_queue<node>q;
    node st,ed;
    st.x=a;
    st.y=b;
    st.step=0;
    st.b=0;
    q.push(st);
    while(!q.empty())
    {
        st=q.top();
        q.pop();
        if(mp[st.x][st.y]=='T')  //找到
        {
            printf("%d\n",st.step);
            return ;
        }
        for(int i=0; i<4; i++)  
        {
            int tx=st.x+to[i][0];
            int ty=st.y+to[i][1];
            if(tx<0||ty<0||tx>=n||ty>=m)
                continue;
            if(mp[tx][ty]=='B')  //B點
            {
                if(st.b==5)  //到達B點的時候氧氣本身就為5,那麼只增加步數
                {
                    ed.b=st.b;
                    ed.step=st.step+1;
                }
                else  //增加步數和氧氣數
                {
                    ed.b=st.b+1;
                    ed.step=st.step+1;
                }
            }
            else if(mp[tx][ty]=='P')  //經過這一點步數不增加
            {
                ed.step=st.step;
                ed.b=st.b;
            }
            else if(mp[tx][ty]=='#') 
            {
                if(st.b>0)  //有氧氣
                {
                    ed.step=st.step+2;//需要在這點再呆一秒,那麼時間+2
                    ed.b=st.b-1;  //氧氣數-1
                }
                else
                    continue;
            }
            else  //  '.'
            {
                ed.step=st.step+1;
                ed.b=st.b;
            }
            ed.x=tx;
            ed.y=ty;
            if(book[tx][ty][ed.b]==0)  //這個狀態沒有出現過
            {
                book[tx][ty][ed.b]=1;
                q.push(ed);
            }
        }
    }
    printf("-1\n");
}
int main()
{
    while(~scanf("%d%d",&n,&m)&&(n||m))
    {
        int i,j;
        memset(book,0,sizeof(book));
        for(i=0; i<n; i++)
        {
            scanf("%s",mp[i]);
            for(j=0; j<m; j++)
                if(mp[i][j]=='S')
                    a=i,b=j;
        }
        book[a][b][0]=1;
        bfs();
    }
    return 0;
}