2018 北京賽區網路預選賽 A. Saving Tang Monk II(BFS+優先佇列)

大白QQly成長日記發表於2018-09-23

時間限制: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為加速。每走一格時間花費1,入一個#花費一個氧氣瓶,無氧氣不能入,入B能獲得一個氧氣,身上最多能攜帶5個氧氣,入P不花費時間,求起點到終點的最小花費時間。

思路:呵呵,想多了。典型的搜尋題嘛,之前的想法是搜尋每條路,統計路上的#與B,判斷在B額外獲得1個氧氣的花費,最後減去相除取時間,再取最值。感覺應該能行吧?但是死磕在BB與BP的情況,效率不同可能涉及到組合問題,這就很傷腦筋了.........

然而....大佬們告訴我直接搜就可以了.....

參考了網上大佬的做法:www.bubuko.com/infodetail-2777883.html

因為最多能帶5個氧氣,所以每次入毒都只有那幾種可能,所以可將二維圖擴充套件為三維的狀態。每個地方相同的狀態進入結果是相同的,所以標記的vis陣列擴充套件為3維即可。

程式碼如下:

#include<set>
#include<map>
#include<list>
#include<deque>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<stdio.h>
#include<sstream>
#include<stdlib.h>
#include<string.h>
//#include<ext/rope>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
#define per(i,a,b) for(int i=a;i<=b;++i)
#define LL long long 
#define swap(a,b) {int t=a;a=b;b=t} 
using namespace std;
//using namespace __gnu_cxx;
char mp[205][205];
int vis[205][205][10]; 
struct node{
    int x;
	int y;
	int t;
	int b;
    inline bool operator<(const node &a) const{
         return t>a.t;    
		}
}p,p2;
int n,m,x1,y1;
int f[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int bfs()
{
    memset(vis,0,sizeof(vis));
    priority_queue<node>q;
    q.push(node{x1,y1,0,0});
    vis[x1][y1][0]=1;
    while(!q.empty())
	{
        p=q.top();
        q.pop();
        if(mp[p.x][p.y]=='T') return p.t;
        per(i,0,3)
		{
            p2=p;
            int nx=p.x+f[i][0];
			int ny=p.y+f[i][1];
            p2.x=nx;
			p2.y=ny;
            if(nx<0||nx>=n||ny<0||ny>=m) continue;
            if(mp[nx][ny]=='B')
			{
                p2.t++;
                p2.b=min(p2.b+1,5);
                if(vis[nx][ny][p2.b]==0)
				{
                    vis[nx][ny][p2.b]=1;
                    q.push(p2);
                }
            } 
			else if(mp[nx][ny]=='P')
			{
                if(vis[nx][ny][p2.b]==0)
				{
                    vis[nx][ny][p2.b]=1;
                    q.push(p2);
                }
            } 
			else if(mp[nx][ny]=='#')
			{
                if(p2.b==0) continue;
                p2.b--;
                p2.t+=2;
                if(vis[nx][ny][p2.b]==0)
				{
                    vis[nx][ny][p2.b]=1;
                    q.push(p2);
                }
            } 
			else {
                p2.t++;
                if(vis[nx][ny][p2.b]==0)
				{
                    vis[nx][ny][p2.b]=1;
                    q.push(p2);
                }
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF&&n!=0&m!=0)
	{
		getchar();
        per(i,0,n-1)
		{
			per(j,0,m-1)
			{
				scanf("%c",&mp[i][j]);
				if(mp[i][j]=='S')
				{
					x1=i;
					y1=j;
				}
			}
			getchar();
        }
        printf("%d\n",bfs());
    }
    return 0;
}

 

相關文章