Problem 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.
But to rescue Tang Monk, Sun Wukong might need to get some keys and kill
some snakes in his way.

The palace can be described as a
matrix of characters. Each character stands for a room. In the matrix,
'K' represents the original position of Sun Wukong, 'T' represents the
location of Tang Monk and 'S' stands for a room with a snake in it.
Please note that there are only one 'K' and one 'T', and at most five
snakes in the palace. And, '.' means a clear room as well '#' means a
deadly room which Sun Wukong couldn't get in.

There may be
some keys of different kinds scattered in the rooms, but there is at
most one key in one room. There are at most 9 kinds of keys. A room with
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 Wukong must get ALL kinds of keys(in other words, at least one
key for each kind).

For each step, Sun Wukong could move to
the adjacent rooms(except deadly rooms) in 4 directions(north, west,
south and east), and each step took him one minute. If he entered a room
in which a living snake stayed, he must kill the snake. Killing a snake
also took one minute. If Sun Wukong entered a room where there is a key
of kind N, Sun would get that key if and only if he had already got
keys of kind 1,kind 2 ... and kind N-1. In other words, Sun Wukong must
get a key of kind N before he could get a key of kind N+1 (N>=1). If
Sun Wukong got all keys 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 the minimum time Sun Wukong needed to
rescue Tang Monk.

Input

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

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

Sample Input

3 1
K.S
##1
1#T
3 1
K#T
.S#
1#.
3 2
K#T
.S.
21.
0 0

Sample Output

5
impossible
8

这个题目,读完题目第一反应就是搜索。但是这个题目有几个注意点。读入需要当心,此外,此处用bfs的 话,如果搜到就return,不一定是最小的,因为走过S的第一次时候会使时间负担增加,故最好使用优先队列。此外不止一个S,而且每个S只起一次作用, 也比较棘手,最重要的是要按顺序找到钥匙才行,使得bfs的状态变得很复杂。此处使用key变量表示当前开了几把锁,而S则人为进行排序用v数组才保存访 问状态。由于比赛时写的代码,所以,对S的处理有点浪费内存。

代码:

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define inf 0x3fffffff
#define esp 1e-10
using namespace std;
struct node
{
int x, y, time, key;
bool v[];
bool operator < (const node &a) const
{
return time > a.time;
}
};
char Map[][];
int visit[][][], n, m;
int sn[][], now;
int bfs (int ix, int iy)
{
node f;
f.x = ix; f.y = iy;
f.time = ;
f.key = ;
memset (f.v, , sizeof (f.v));
priority_queue < node > q;
q.push(f);
while (!q.empty())
{
node k = q.top();
q.pop();
if (Map[k.x][k.y] == 'T' && k.key == m)
{
return k.time;
}
for (int xx = -; xx <= ; ++xx)
{
for (int yy = -; yy <= ; ++yy)
{
if (xx != && yy != )
continue;
if (xx == && yy == )
continue;
if (k.x + xx < || k.x + xx >= n)
continue;
if (k.y + yy < || k.y + yy >= n)
continue;
if (Map[k.x + xx][k.y + yy] == '#')
continue;
int dt = ;
if (Map[k.x + xx][k.y + yy] == 'S' && k.v[sn[k.x + xx][k.y + yy]] == )
dt = ;
int kk = k.key;
if (Map[k.x + xx][k.y + yy] - '' == k.key + )
kk = k.key + ;
if (visit[k.x+xx][k.y+yy][kk] == || visit[k.x+xx][k.y+yy][kk] > k.time+dt)
{
visit[k.x + xx][k.y + yy][kk] = k.time + dt;
f = k;
f.x = k.x + xx; f.y = k.y + yy;
f.time = k.time + dt;
f.key = kk;
if (Map[k.x + xx][k.y + yy] == 'S')
{
f.v[sn[k.x + xx][k.y + yy]] = ;
}
q.push(f);
}
}
}
}
return -;
}
int main()
{
freopen ("test.txt", "r", stdin);
while (scanf ("%d%d", &n, &m) != EOF && (m+n) != )
{
now = ;
memset (sn, -, sizeof(sn));
getchar();
int ix, iy;
for (int i = ; i < n; ++i)
{
for (int j = ; j < n; ++j)
{
Map[i][j] = getchar();
if (Map[i][j] == 'K')
{
ix = i;
iy = j;
}
if (Map[i][j] == 'S')
{
sn[i][j] = now++;
}
}
getchar();
}
memset (visit, , sizeof (visit));
int ans = bfs(ix, iy);
if (ans != -)
printf ("%d\n", ans);
else
printf ("impossible\n");
}
return ;
}

最新文章

  1. VS2015使用scanf报错的解决方案
  2. C#读取Excel文件:通过OleDb连接,把excel文件作为数据源来读取
  3. 66. Regular Expression Matching
  4. 关于childNodes的length的问题
  5. new_things
  6. AES加密算法
  7. Apache Spark GraphX的简介
  8. 多浏览器兼容flv视频播放HTML
  9. js中push()方法
  10. db2_errroecode
  11. hudson入门
  12. (译) JSON-RPC 2.0 规范(中文版)
  13. python之类的多态(鸭子类型 )、封装和内置函数property
  14. Python面向对象编程 -- 类和实例、访问限制
  15. Ubuntu 安装 uget
  16. 简易数据库实现 UNIX环境高级编程(APUE)第二十章 A Database Library
  17. Maven中使用本地JAR包
  18. 教你用CMD命令查询域名的DNS解析记录:A,NS,MX,CNAME,TXT
  19. CentOS 7 重装mysql编译过程报错解决方法
  20. 使用CoreData [4]

热门文章

  1. SQL SERVER 2008递归
  2. Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [32,176] milliseco
  3. 【python】-- RabbitMQ RPC模型
  4. Linux软件包分类
  5. IntelliJ IDEA 添加类注释模板
  6. k近邻算法(k-nearest neighbor,k-NN)
  7. 初步jmeter安装与使用
  8. Windows存储管理之磁盘类型简介
  9. 每天一个Linux命令(3)mkdir命令
  10. 为什么要用redux?component自身管理自己的state难道不更解耦吗?