时间限制: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;
}