1. 程式人生 > >ACM/ICPC 2018亞洲區預選賽北京賽站網路賽 Saving Tang Monk II

ACM/ICPC 2018亞洲區預選賽北京賽站網路賽 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處可以拿氧氣,最多可以攜帶5瓶氧氣,#是由毒氣的地方,通過#處需耗時2分鐘,P處可以減少一分鐘(意思是從其他處走到p處不耗時),走到其他處時都是耗時 1分鐘,求S到T最少時間

思路:因為最多可以攜帶5瓶氧氣,所以一共的狀態數為 6*100*100;直接bfs(),就行了;

程式碼:

#include<bits/stdc++.h>
using namespace std;
#define Max 105
#define INF 0x3f3f3f3f
int dp[6][Max][Max];    // 一共的狀態數; 
char str[Max][Max];
int a[4][2] = {0,-1,1,0,0,1,-1,0};
int n,m,x,y;
struct node
{
	int x,y,step,kk;
	bool operator < (node a) const   // 優先佇列 
	{
		return a.step < step;
	}
	
};
int bfs()
{
	priority_queue<node> q;
	dp[0][y][x] = 0;
	node tt;
	tt.y = y;
	tt.x = x;
	tt.step = 0;
	tt.kk = 0;
	q.push(tt);
	while(!q.empty())
	{
		node star,end;
		star = q.top(); 
		q.pop();
		for(int i = 0; i < 4;i++)
		{
			int tx = star.x + a[i][0];
			int ty = star.y + a[i][1];
			if(tx>=0&&ty>=0&tx<n&&ty<m)
			{
				end.x = tx;
				end.y = ty;
				if( str[ty][tx]=='#' && star.kk > 0 && star.step + 2 < dp[star.kk-1][ty][tx])
				{
					end.step = star.step + 2;
					end.kk = star.kk - 1;
					dp[end.kk][ty][tx] = star.step + 2;
					q.push(end);
				}
				else if(str[ty][tx]=='B' && star.kk <= 4 && star.step + 1 < dp[star.kk+1][ty][tx])
				{
					end.step = star.step + 1;
					end.kk = star.kk + 1;
					dp[end.kk][ty][tx] = star.step + 1;
					q.push(end);
				}
				else if(str[ty][tx]=='P'&& star.step < dp[star.kk][ty][tx])
				{
					end.step = star.step;
					end.kk = star.kk;
					dp[end.kk][ty][tx] = end.step;
					q.push(end);
				}
				else if((str[ty][tx] == '.'||str[ty][tx]=='S')&&star.step + 1 < dp[star.kk][ty][tx])
				{
					end.step = star.step + 1;
					end.kk = star.kk;
					dp[end.kk][ty][tx] = end.step;
					q.push(end);
				}
				else if(str[ty][tx] == 'T')
				{
					end.step = star.step + 1;
					end.kk = star.kk;
					dp[end.kk][ty][tx] = end.step;
					q.push(end);
					return end.step;
				}
			}
		}
	}
	return -1;
}
int main()
{
	while(~scanf("%d%d",&m,&n)&&(m+n))
	{
		for(int i = 0;i<m;i++)
		{
			scanf("%s",str[i]);
			for(int j = 0;j<n;j++)
			{
				if(str[i][j]=='S')
				{
					y = i;
					x = j;	
				}
			}
		}
		memset(dp,INF,sizeof(dp));
		printf("%d\n",bfs()); 
	}
	return 0;
}