|
@@ -1,4 +1,6 @@
|
|
|
#include <cstdio>
|
|
|
+#include <cstring>
|
|
|
+#include <queue>
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
@@ -6,30 +8,77 @@ const int N = 100;
|
|
|
const int M = 9;
|
|
|
const int S = 5;
|
|
|
|
|
|
-char map[N][N + 1];
|
|
|
+const char dx[4] = {1, 0, -1, 0};
|
|
|
+const char dy[4] = {0, 1, 0, -1};
|
|
|
|
|
|
-bool flag[N][N][1 << M][S];
|
|
|
+char map[N][N + 1];
|
|
|
+char snake[N][N];
|
|
|
+bool flag[N][N][M + 1][1 << S];
|
|
|
|
|
|
struct Pos {
|
|
|
- short r;
|
|
|
- short c;
|
|
|
- short key;
|
|
|
+ char r;
|
|
|
+ char c;
|
|
|
+ char key;
|
|
|
bool kill;
|
|
|
+ char died;
|
|
|
int t;
|
|
|
- Pos(short r, short c, short key, bool kill, int t)
|
|
|
- : r(r), c(c), key(key), kill(kill), t(t) {}
|
|
|
+ Pos(char r = 0, char c = 0, char key = 0, bool kill = false, char died = 0,
|
|
|
+ int t = 0)
|
|
|
+ : r(r), c(c), key(key), kill(kill), died(died), t(t) {}
|
|
|
bool operator==(const Pos &that) const {
|
|
|
return this->r == that.r && this->c == that.c && this->key == that.key;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+char n, m;
|
|
|
+
|
|
|
+bool is_expandable(char x, char y, char key, char died) {
|
|
|
+ if (x < 0 || n <= x || y < 0 || n <= y) return false;
|
|
|
+ if (map[x][y] == '#' || flag[x][y][key][died]) return false;
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
int main() {
|
|
|
- int n, m;
|
|
|
while (scanf("%d %d", &n, &m) != EOF) {
|
|
|
if (n == 0 && m == 0) break;
|
|
|
- for (int i = 0; i < n; i++) {
|
|
|
+ Pos sun, tang;
|
|
|
+ int s = 0;
|
|
|
+ for (char i = 0; i < n; i++) {
|
|
|
scanf("%s", map[i]);
|
|
|
+ for (char j = 0; j < n; j++) {
|
|
|
+ if (map[i][j] == 'T') {
|
|
|
+ tang.r = i;
|
|
|
+ tang.c = j;
|
|
|
+ tang.key = m;
|
|
|
+ tang.t = -1;
|
|
|
+ } else if (map[i][j] == 'K') {
|
|
|
+ sun.r = i;
|
|
|
+ sun.c = j;
|
|
|
+ } else if (map[i][j] == 'S') {
|
|
|
+ snake[i][j] = s++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ queue<Pos> q;
|
|
|
+ q.push(sun);
|
|
|
+ memset(flag, 0, sizeof(flag));
|
|
|
+ flag[sun.r][sun.c][0][0] = true;
|
|
|
+ while (!q.empty()) {
|
|
|
+ Pos &cur = q.front();
|
|
|
+ q.pop();
|
|
|
+ if (cur == tang) {
|
|
|
+ tang.t = cur.t;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < 4; i++) {
|
|
|
+ int nx = cur.r + dx[i];
|
|
|
+ int ny = cur.c + dy[i];
|
|
|
+ }
|
|
|
}
|
|
|
+ if (tang.t == -1)
|
|
|
+ printf("impossible\n");
|
|
|
+ else
|
|
|
+ printf("%d\n", tang.t);
|
|
|
}
|
|
|
return 0;
|
|
|
}
|