Saving Tang Monk(翻译)

2022.2.11

题目网址:

http://noi.openjudge.cn/ch0205/8436/

原题:

Saving Tang Monk

总时间限制: 

1000ms

内存限制: 

65536kB

描述

《Journey to theWest》(also 《Monkey》) is one of the Four Great Classical Novelsof Chinese literature. It was writtenby 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 getsacred Buddhism texts.

During thejourney, Tang Monk was often captured by demons. Most of demons wanted to eatTang Monk to achieve immortality, but some female demons just wanted to marryhim because he was handsome. So, fighting demons and saving Monk Tang is themajor job for Sun Wukong to do.

Once, TangMonk was captured by the demon White Bones. White Bones lived in a palace andshe cuffed Tang Monk in a room. Sun Wukong managed to get into the palace. Butto rescue Tang Monk, Sun Wukong might need to get some keys and kill somesnakes in his way.

The palace can be described as a matrix of characters. Each characterstands for a room. In the matrix, 'K' represents the original position of SunWukong, 'T' represents the location of Tang Monk and 'S' stands for a room witha snakein it. Please note that there are only one 'K' and one 'T', and at mostfive snakesin the palace. And, '.' means a clear room as well '#' means a deadlyroom which Sun Wukong couldn't get in.

There may be some keys of different kinds scattered in the rooms, butthere is at most one key in one room. There are at most 9 kinds of keys. A roomwith a key in it is represented by a digit(from '1' to '9'). For example, '1'means a room with a first kind key, '2' means a room with a second kind key,'3' means a room with a third kind key... etc. To save Tang Monk, Sun Wukongmust get ALL kinds of keys(in other words, at least one key for each kind).

For each step, Sun Wukong couldmove to the adjacent rooms(except deadly rooms) in 4 directions(north, west, southand east), and each step took him one minute. If he entered a room in which aliving snakestayed, he must kill the snake.Killing a snakealso took one minute. If Sun Wukong entered a room where there is a keyof kind N, Sun would get that key if and only if he had already got keys ofkind 1,kind 2 ... and kind N-1. In other words, Sun Wukong must get a key ofkind N before he could get a key of kind N+1 (N>=1). If Sun Wukong got allkeys he needed and entered the room in which Tang Monk was cuffed, the rescue mission is completed. If Sun Wukong didn't get enough keys,he still could pass through Tang Monk's room. Since Sun Wukong was a impatient monkey,he wanted to save Tang Monk as quickly as possible. Please figure out theminimum time Sun Wukong needed to rescue Tang Monk.

输入

There are several test cases.
For each case, the first line includes two integers N and M(0 < N <= 100, 0<=M<=9), meaning that the palace is a N×N matrix and Sun Wukong needed M kinds of keys(kind 1, kind 2, ... kind M).
Then the N×N matrix follows.
The input ends with N = 0 and M = 0.

输出

For each test case, print the minimum time (in minutes) Sun Wukong needed to save Tang Monk. If it's impossible for Sun Wukong to complete the mission, print "impossible"(no quotes).

翻译:

描述:

《西游记》(也叫《齐天大圣》)是中国文学的四大经典小说之一。它在明朝由吴承恩著作。在这个小说里,齐天大圣孙悟空,猪八戒,沙悟净,护送唐僧去印度取经。

在旅途期间,唐僧经常被妖魔鬼怪捕获。大多数妖魔鬼怪想吃唐僧以实现长生不老,但是一些女性妖魔鬼怪只是想要嫁给他,因为他很英俊。所以,与妖魔鬼怪搏斗以及拯救唐僧是孙悟空的主要任务。

有一次,唐僧被妖怪白骨精抓到了。白骨精住在一个宫殿里面并且把唐僧拷在一个房间里面。孙悟空设法成功进入这个宫殿。为了拯救唐僧,孙悟空可能需要拿到某些钥匙并且杀了一些在路上的蛇。

这个宫殿可以被描述为一个字符矩阵。每个字符代表一个房间。在这个矩阵中,‘K’代表孙悟空最初的位置。‘T’代表唐僧的位置,‘S’代表有一条蛇在里面。请注意这里只有一个‘K’和一个‘T’,这个宫殿里最多5条蛇。‘.’代表一个干净的房间,‘#’代表一个孙悟空不能进去的死寂的房间。

这里可能有一些钥匙散布在房间里,但是这里一个房间最多有一把钥匙。这里最多有9种钥匙。有一把钥匙的一个房间由一个数字表示(从‘1’到‘9’)。例如,‘1’意味着一个有一把第一种钥匙的房间,‘2’意味着一个有一把第二种钥匙的房间,‘3’意味着一个有一把第三种钥匙的房间...等等。为了拯救唐僧,孙悟空必须拿到所有的钥匙(换言之,每种钥匙至少拿到一把)。

对于每一步,孙悟空可以往东西南北四个方向走进房间(死寂房间除外),每一步都要花费一分钟。如果他进入了一个有蛇的房间,那么他必须杀死这条蛇。杀死一条蛇也需要花费一分钟。如果孙悟空进入了一个有钥匙的房间,并且他已经拿到了第1,2,...,n-1种钥匙,他将拿到这把钥匙。换言之,在孙悟空拿到第N+1把钥匙之前,他必须拿到N种钥匙种每种一把(N>1)。如果孙悟空拿到了他所有需要的钥匙并且进入了唐僧被拷的那个房间,那么救援任务就完成了。如果孙悟空没有拿到足够的钥匙,他仍然能通过唐僧的房间。因为孙悟空是一个没有耐心的猴子,所以他想尽可能快的拯救唐僧。请找到孙悟空需要救唐僧的最短时间。

输入:

这里有几组测试样例。

对于每一组样例,第一行包含两个整数N和M(0<N<=100,0<=M<=9),代表这个宫殿是一个N*N的矩阵,孙悟空需要M种钥匙(第一种,第二种,...第M种)。

接下来有一个N*N的矩阵。

输入以N=0和M=0结束。

输出:

对于每组测试样例,打印出孙悟空需要救出唐僧的最短时间(以分钟为单位)。如果孙悟空不可能完成这个任务,那么打印“impossible”(不带引号)。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值