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的处理有点浪费内存。
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cmath> 6 #include <algorithm> 7 #include <set> 8 #include <map> 9 #include <queue> 10 #include <string> 11 #include <vector> 12 #define inf 0x3fffffff 13 #define esp 1e-10 14 using namespace std; 15 struct node 16 { 17 int x, y, time, key; 18 bool v[5]; 19 bool operator < (const node &a) const 20 { 21 return time > a.time; 22 } 23 }; 24 char Map[105][105]; 25 int visit[105][105][15], n, m; 26 int sn[105][105], now; 27 int bfs (int ix, int iy) 28 { 29 node f; 30 f.x = ix; f.y = iy; 31 f.time = 0; 32 f.key = 0; 33 memset (f.v, 0, sizeof (f.v)); 34 priority_queue < node > q; 35 q.push(f); 36 while (!q.empty()) 37 { 38 node k = q.top(); 39 q.pop(); 40 if (Map[k.x][k.y] == 'T' && k.key == m) 41 { 42 return k.time; 43 } 44 for (int xx = -1; xx <= 1; ++xx) 45 { 46 for (int yy = -1; yy <= 1; ++yy) 47 { 48 if (xx != 0 && yy != 0) 49 continue; 50 if (xx == 0 && yy == 0) 51 continue; 52 if (k.x + xx < 0 || k.x + xx >= n) 53 continue; 54 if (k.y + yy < 0 || k.y + yy >= n) 55 continue; 56 if (Map[k.x + xx][k.y + yy] == '#') 57 continue; 58 int dt = 1; 59 if (Map[k.x + xx][k.y + yy] == 'S' && k.v[sn[k.x + xx][k.y + yy]] == 0) 60 dt = 2; 61 int kk = k.key; 62 if (Map[k.x + xx][k.y + yy] - '0' == k.key + 1) 63 kk = k.key + 1; 64 if (visit[k.x+xx][k.y+yy][kk] == 0 || visit[k.x+xx][k.y+yy][kk] > k.time+dt) 65 { 66 visit[k.x + xx][k.y + yy][kk] = k.time + dt; 67 f = k; 68 f.x = k.x + xx; f.y = k.y + yy; 69 f.time = k.time + dt; 70 f.key = kk; 71 if (Map[k.x + xx][k.y + yy] == 'S') 72 { 73 f.v[sn[k.x + xx][k.y + yy]] = 1; 74 } 75 q.push(f); 76 } 77 } 78 } 79 } 80 return -1; 81 } 82 int main() 83 { 84 freopen ("test.txt", "r", stdin); 85 while (scanf ("%d%d", &n, &m) != EOF && (m+n) != 0) 86 { 87 now = 0; 88 memset (sn, -1, sizeof(sn)); 89 getchar(); 90 int ix, iy; 91 for (int i = 0; i < n; ++i) 92 { 93 for (int j = 0; j < n; ++j) 94 { 95 Map[i][j] = getchar(); 96 if (Map[i][j] == 'K') 97 { 98 ix = i; 99 iy = j; 100 } 101 if (Map[i][j] == 'S') 102 { 103 sn[i][j] = now++; 104 } 105 } 106 getchar(); 107 } 108 memset (visit, 0, sizeof (visit)); 109 int ans = bfs(ix, iy); 110 if (ans != -1) 111 printf ("%d\n", ans); 112 else 113 printf ("impossible\n"); 114 } 115 return 0; 116 }