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

ACM/ICPC 2018亞洲區預選賽北京賽站網路賽 A. Saving Tang Monk II(bfs+dp)

時間限制: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的最短時間,如果不能到達輸出-1,規則是:'.'表示空地,'B'表示氧氣室,進去可以得到一個氧氣,最多攜帶五個,'P'表示加速,進去可以得到一個加速的道具,使用可以使得下一步不花費時間,可以攜帶無數個,'#'表示毒氣室,沒有氧氣無法進入,進入消耗一瓶氧氣並且需要多停留一個單位時間。

思路:用bfs是很明確的,加速早用晚用都一樣,比較好處理,要處理的就是氧氣的問題,一直沒處理好,看了題解說是用dp,dp[i][j][k]代表到達i,j這個地方,攜帶k個氧氣的最短時間

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#define N 105
#define inf 99999999
using namespace std;
 
struct node{
	int x,y;
	int b;
};
 
int n,m;
char mp[N][N];
int ex,ey;
int sx,sy;
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int dp[N][N][10];//從起點出發直到在(i,j)拿著k個氧氣罐所需最少時間
 
int check(int x,int y){
	if(x<0||x>=n||y<0||y>=m)return 1;
	return 0;
}
void bfs(){
	int i;
	queue<node>Q;
	node a,next;
	a.x=sx;
	a.y=sy;
	a.b=0;
	Q.push(a);
	while(!Q.empty()){
		a=Q.front();
	 	Q.pop();
	 	for(int i=0;i<4;i++){
	 		next=a;
	 		next.x+=dir[i][0];
	 		next.y+=dir[i][1];
	 		if(check(next.x,next.y))continue;
		 	if(mp[next.x][next.y]=='T'){
	 			dp[next.x][next.y][next.b]=min(dp[next.x][next.y][next.b],dp[a.x][a.y][a.b]+1);
	 		}
	 		if(mp[next.x][next.y]=='#'){
		 		if(next.b==0)continue;
		 		else{
		 			if(dp[next.x][next.y][next.b-1]>dp[a.x][a.y][a.b]+2){
		 				dp[next.x][next.y][next.b-1]=dp[a.x][a.y][a.b]+2;
		 				next.b--;
				 		Q.push(next);
				 	}
		 		}
		 	}
		 	if(mp[next.x][next.y]=='.'||mp[next.x][next.y]=='S'){
	 			if(dp[next.x][next.y][next.b]>dp[a.x][a.y][a.b]+1){
	 				dp[next.x][next.y][next.b]=dp[a.x][a.y][a.b]+1;
			 		Q.push(next);
			 	}
	 		}
	 		if(mp[next.x][next.y]=='P'){
	 			if(dp[next.x][next.y][next.b]>dp[a.x][a.y][a.b]){
	 				dp[next.x][next.y][next.b]=dp[a.x][a.y][a.b];
			 		Q.push(next);
			 	}
		 	}
		 	if(mp[next.x][next.y]=='B'){
		 		if(next.b<5){
			 		if(dp[next.x][next.y][next.b+1]>dp[a.x][a.y][a.b]+1){
		 				dp[next.x][next.y][next.b+1]=dp[a.x][a.y][a.b]+1;
		 				next.b++;
				 		Q.push(next);
				 	}	
		 		}
	 			else{
			 		if(dp[next.x][next.y][next.b]>dp[a.x][a.y][a.b]+1){
		 				dp[next.x][next.y][next.b]=dp[a.x][a.y][a.b]+1;
				 		Q.push(next);
				 	}
			 	}
	 		}
	 	}
	}
}
int main(){
	while(scanf("%d%d",&n,&m)!=EOF){
		if(n==0&&m==0)break;
		for(int i=0;i<n;i++){
			scanf("%s",mp[i]);
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(mp[i][j]=='S'){
					sx=i;sy=j;
				}
				if(mp[i][j]=='T'){
					ex=i;ey=j;
				}
				for(int k=0;k<=5;k++)dp[i][j][k]=inf;
			}
		}
		dp[sx][sy][0]=0;
		bfs();
		int ans=inf;
		for(int i=0;i<=5;i++){
			ans=min(ans,dp[ex][ey][i]);
		}
		if(ans==inf)printf("-1\n");
		else printf("%d\n",ans);
 	}
 	return 0;
}