123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #include <cstdio>
- #include <cstring>
- #include <queue>
- using namespace std;
- const int N = 100;
- const int M = 9;
- const int S = 5;
- const char dx[4] = {1, 0, -1, 0};
- const char dy[4] = {0, 1, 0, -1};
- char map[N][N + 1];
- char snake[N][N];
- bool flag[N][N][M + 1][1 << S];
- struct Pos {
- char r;
- char c;
- char key;
- bool kill;
- char died;
- int 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() {
- while (scanf("%d %d", &n, &m) != EOF) {
- if (n == 0 && m == 0) break;
- 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;
- }
|