ACM-ICPC 2018北京赛区网络预赛A(三维记录状态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点还能继续走,然后就是需要三维记录状态,三维记录状态可以包含了走到毒气区域发现氧气不够再多拿氧气的情况,因为队列里面已经有多拿氧气的情况了,还有就是从队列里取出来的时候再标记这个状态,因为取出来才是走过了,而放进去不是,最后就是判断唐僧的点也是取出来再判定,道理相同。

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int xs,ys,n,m;
int vis[110][110][10];
char map[110][110];
int xd[5]={1,-1,0,0},yd[5]={0,0,-1,1};
struct node{
	int x,y;
	int o2,step;
	bool operator <(const node &x)const{
		return step>x.step;	
	}
};
priority_queue<node>q;
void bfs(){
	while(!q.empty()) q.pop();
	memset(vis,0,sizeof(vis));
	q.push({xs,ys,0,0});
	map[xs][ys]='.';
	while(!q.empty()){
		node l=q.top();
		q.pop();
		if(map[l.x][l.y]=='T'){
			cout<<l.step<<endl;
			return;
		}
		if(vis[l.x][l.y][l.o2])continue;
		vis[l.x][l.y][l.o2]=1;
		for(int i=0;i<4;i++){
			int xx=l.x+xd[i],yy=l.y+yd[i];
			if(xx<=0||xx>n||yy<=0||yy>m)
				continue;
			if(map[xx][yy]=='#'){
				if(l.o2>0&&!vis[xx][yy][l.o2-1]){
//					vis[xx][yy][l.o2-1]=1;
					q.push({xx,yy,l.o2-1,l.step+2});
				}
			}
			else if(map[xx][yy]=='B'){
				if(l.o2+1<=5&&!vis[xx][yy][l.o2+1]){
//					vis[xx][yy][l.o2+1]=1;
					q.push({xx,yy,l.o2+1,l.step+1});
				}
			}
			else if(map[xx][yy]=='P'){
//				vis[xx][yy][l.o2]=1;
				q.push({xx,yy,l.o2,l.step});
			}
			else if(map[xx][yy]=='T'||map[xx][yy]=='.'){
//				vis[xx][yy][l.o2]=1;
				q.push({xx,yy,l.o2,l.step+1});
			}
		}
	}
	cout<<-1<<endl;
}
int main(){
	while(cin>>n>>m && m+n){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cin>>map[i][j];
				if(map[i][j]=='S'){
					xs=i;ys=j;
				}
			}
		}
		bfs();
	}
}

猜你喜欢

转载自blog.csdn.net/samscream/article/details/82910917