【hiho】2018ICPC北京赛区网络赛A Saving Tang Monk II(bfs+优先队列)

题目链接

#1828 : Saving Tang Monk II

时间限制:1000ms

单点时限:1000ms

内存限制:256MB

描述

《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

【题意】

孙悟空救师父,起点为S终点为T,#为毒室,B为氧气,P为加速器,孙悟空只能携带5个氧气,孙悟空可以朝上下左右四个方向移动,每移动一个位置为1s,进入毒室时会消耗1个氧气,并会多停留1s,拾取加速器后可以减少1s,求孙悟空救到师父最少所用的时间。

【解题思路】

建立一个优先队列,按照上述要求进行bfs搜索,并做好标记,减少不必要的步数。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int maxn=105;
const int INF=0x3f3f3f3f;
int disx[]={1,0,0,-1};
int disy[]={0,1,-1,0};
int n,m,si,sj,ti,tj,ans;
char s[maxn][maxn];
int vis[maxn][maxn][6];
struct Node
{
    int x,y,b,step;
    bool operator <(const Node &c)const
    {
        return step>c.step;
    }
};
void bfs()
{
    memset(vis,0,sizeof(vis));
    Node node;
    node.x=si;node.y=sj;node.step=0;node.b=0;
    priority_queue<Node>q;
    q.push(node);
    while(!q.empty())
    {
        Node t=q.top();
        q.pop();
        for(int i=0;i<4;i++)
        {
            Node p=t;
            p.x+=disx[i];
            p.y+=disy[i];
            p.step++;
            if(p.x==ti && p.y==tj)
            {
                ans=p.step;
                return;
            }
            if(p.b==0 && s[p.x][p.y]=='#')continue;//当没有氧气时遇到#
            if(p.x>=0 && p.x<n && p.y>=0 && p.y<m)
            {
                if(s[p.x][p.y]=='B')
                {
                    if(p.b<5)p.b++;
                }
                else if(s[p.x][p.y]=='P')
                    p.step--;
                else if(s[p.x][p.y]=='#')
                {
                    if(p.b>0)
                    {
                        p.b--;
                        p.step++;
                    }
                }
                if(vis[p.x][p.y][p.b])continue;
                vis[p.x][p.y][p.b]=1;
                q.push(p);
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m) && n||m)
    {
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(s[i][j]=='S')
                    si=i,sj=j;
                else if(s[i][j]=='T')
                    ti=i,tj=j;
            }
        }
        ans=INF;
        bfs();
        if(ans==INF)printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/82821541