HihoCoder 1828-Saving Tang Monk II-广搜+记忆化+剪枝

Description

《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.

Input

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.

Output

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

Sample Input

2 2
S#
#T
2 5
SB###
##P#T
4 7
SP.....
P#.....
......#
B...##T
0 0

Sample Output

-1
8
11

题目大意:

迷宫中,位于S位置的孙悟空要去救位于T位置的唐僧。
每个位置的字符可能为以下六种的任意一种

  • “S”:孙悟空的位置
  • “T”:唐僧的位置
  • “.”:空地
  • “#”:毒气室
  • “B”:氧气室,室内有无限瓶氧气。每当孙悟空进入此房间时会获得一瓶氧气,孙悟空最多可以携带5瓶氧气。
  • “P”:药丸室,室内有无限颗药丸。每当孙悟空进入此房间时会获得一颗药丸,孙悟空可携带无限颗药丸。

另外注意:
1、孙悟空可以向上下左右四个方向移动,进入毒气室需要消耗一瓶氧气。
2、孙悟空每次移动花费一分钟,但是药丸可以使下一次位移不花费时间。
每经过一次毒气室,孙悟空都需要额外花费一分钟修养

求孙悟空从S至T的最短时间。

核心思想:

广度优先搜索+记忆化+剪枝

  • 搜索的队列的单位是结构体:
    x,y,z,s。
    表示走过(x,y)后携带z瓶氧气并且花费了s时间。
  • dp[x][y][z] 记忆 走过(x,y)后携带z瓶氧气 所需要的最短时间。

假设从(x,y)移动至(tx,ty),氧气数量从z瓶改变至(可能不变)tz瓶,花费的时间从s改变至(可能不变)ts:
如果ts<dp[tx][ty][tz],则将以下结构体入队:
tx,ty,tz,ts。
否则此状态不必再搜,不入队,剪枝。

注意:由于有减时药丸的存在,搜索是按位置而不是按时间,所以第一次搜到T的时间可能不是最短时间,要将搜索进行到底,比较所有到达时间,取最小值。

代码如下:

#include<cstdio>
#include<iostream>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=120,Q=5e5+20;
char G[N][N];
int dp[N][N][6];
int ans,n,m,sx,sy,b[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct node{
	int x,y,z,s;
	node()
	{
	}
	node(int xx,int yy,int zz,int ss)
	{
		x=xx;
		y=yy;
		z=zz;
		s=ss;
	}
}q[Q];
int bfs()
{
	//初始化 
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			for(int k=0;k<6;k++)
				dp[i][j][k]=inf;
	ans=inf;
	int h=1,t=0;
	q[0]=node(sx,sy,0,0);
	dp[sx][sy][0]=0;
	//广搜 
	while(t<h)
	{
		int x=q[t].x;
		int y=q[t].y;
		int z=q[t].z;
		int s=q[t].s;
		//四个方向可以走 
		for(int i=0;i<4;i++)
		{
			int tx=x+b[i][0];
			int ty=y+b[i][1];
			if(tx<0||tx>=n||ty<0||ty>=m)
				continue;
			if(G[tx][ty]=='.'||G[tx][ty]=='S')
			{
				if(dp[tx][ty][z]<=s+1)
					continue;
				dp[tx][ty][z]=s+1;
				q[h++]=node(tx,ty,z,s+1);
			}
			else if(G[tx][ty]=='#')
			{
				if(!z||dp[tx][ty][z-1]<=s+2)
					continue;
				dp[tx][ty][z-1]=s+2;
				q[h++]=node(tx,ty,z-1,s+2);
			}
			else if(G[tx][ty]=='B')
			{
				int tz=min(5,z+1);
				if(dp[tx][ty][tz]<=s+1)
					continue;
				dp[tx][ty][tz]=s+1;
				q[h++]=node(tx,ty,tz,s+1);
			}
			else if(G[tx][ty]=='P')
			{
				if(dp[tx][ty][z]<=s)
					continue;
				dp[tx][ty][z]=s;
				q[h++]=node(tx,ty,z,s);
			}
			else//到达T,取最小值 
				ans=min(ans,s+1);
		}
		t++;
	}
	if(ans<1e8)
		return ans;
	return -1;
}
int main()
{
	while(1)
	{
		scanf("%d%d",&n,&m);
		if(!n&&!m)
			break;
		sx=-1;
		for(int i=0;i<n;i++)
		{
			scanf("%s",G[i]);
			if(sx==-1)
			{
				for(int j=0;j<m;j++)
					if(G[i][j]=='S')
					{
						sx=i;
						sy=j;
						break;
					}
			}
		}
		printf("%d\n",bfs());
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值