题目1 : 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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; int n,m;
char s[][];
int b[][][];
int t[][]={,,,,-,,,-};
struct Node{
int x,y,B,s;
friend bool operator<(Node a,Node b)
{
return a.s>b.s;
}
}node;
priority_queue<Node> q; void bfs(int x,int y){
node.x=x;node.y=y;
node.B=;node.s=;
q.push(node);
b[x][y][]=;
int f=-;
while(q.size()){ for(int i=;i<;i++){
int tx=q.top().x+t[i][];
int ty=q.top().y+t[i][];
if(tx<||ty<||tx>n||ty>m) continue;
int tB=q.top().B;
int ts=q.top().s+;
if(s[tx][ty]=='#'&&tB<) continue;
if(s[tx][ty]=='#'){
tB--;
ts++;
}
if(s[tx][ty]=='B'&&tB<) tB++;
if(s[tx][ty]=='P') ts--;
if(s[tx][ty]=='T'){
f=ts;
break;
}
if(b[tx][ty][tB]==) continue;
b[tx][ty][tB]=;
node.x=tx;node.y=ty;
node.B=tB;node.s=ts;
q.push(node);
}
if(f>-) break;
q.pop();
}
printf("%d\n",f);
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)&&n+m){
for(i=;i<=n;i++){
scanf(" %s",s[i]+);
}
while(q.size()){
q.pop();
}
memset(b,,sizeof(b));
int f=;
for(i=;i<=n;i++){
for(j=;j<=m;j++){
if(s[i][j]=='S'){
bfs(i,j);
f=;
break;
}
}
if(f==) break;
}
}
return ;
}

最新文章

  1. c++局域网多播
  2. What does it mean to “delegate to a sister class” via virtual inheritance?
  3. Spring bean的作用域和生命周期
  4. wtforms 使用
  5. Daily Scrum 10.28
  6. C#中 ToString 和 override ToString 的区别
  7. Varchar2 size how to decide?
  8. mybatis系列-10-一对一查询
  9. httpd配置Gzip压缩
  10. 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
  11. MySQL数据库导入外部*.sql文件具体步骤
  12. 【转】理解 Android Build 系统----不错
  13. [置顶] JQuery实战总结三 标签页效果图实现
  14. springmvc 接收对象 滴灌摘要
  15. java操作redis redis连接池
  16. linux下实现自动部署tomcat的脚本
  17. CRL快速开发框架4.4版发布,支持主从读写分离
  18. SpriteBuilder中同父节点的显示顺序
  19. mvn test报错
  20. 安装kubelet报错 : Depends: init-system-helpers (&gt;= 1.18~) but 1.14ubuntu1 is to be installed

热门文章

  1. (1)Web 应用是一个状态机,视图与状态是一一对应的。 (2)所有的状态,保存在一个对象里面。
  2. php总结5——常量、文件上传
  3. PHP加Nginx实现动态裁剪图片方案
  4. gon
  5. Form表单插件
  6. Linux (ubuntu和redhat) 常用命令及细节
  7. freemaker开发
  8. zkeacms源码解读一
  9. TYOI 1015 Game:博弈 结论【步数之和的奇偶性】
  10. Oracle学习笔记_05_ 一个创建表空间、创建用户、授权的完整过程