main.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;
  5. const int N = 100;
  6. const int M = 9;
  7. const int S = 5;
  8. const char dx[4] = {1, 0, -1, 0};
  9. const char dy[4] = {0, 1, 0, -1};
  10. char map[N][N + 1];
  11. char snake[N][N];
  12. bool flag[N][N][M + 1][1 << S];
  13. struct Pos {
  14. char r;
  15. char c;
  16. char key;
  17. bool kill;
  18. char died;
  19. int t;
  20. Pos(char r = 0, char c = 0, char key = 0, bool kill = false, char died = 0,
  21. int t = 0)
  22. : r(r), c(c), key(key), kill(kill), died(died), t(t) {}
  23. bool operator==(const Pos &that) const {
  24. return this->r == that.r && this->c == that.c && this->key == that.key;
  25. }
  26. };
  27. char n, m;
  28. bool is_expandable(char x, char y, char key, char died) {
  29. if (x < 0 || n <= x || y < 0 || n <= y) return false;
  30. if (map[x][y] == '#' || flag[x][y][key][died]) return false;
  31. return true;
  32. }
  33. int main() {
  34. while (scanf("%d %d", &n, &m) != EOF) {
  35. if (n == 0 && m == 0) break;
  36. Pos sun, tang;
  37. int s = 0;
  38. for (char i = 0; i < n; i++) {
  39. scanf("%s", map[i]);
  40. for (char j = 0; j < n; j++) {
  41. if (map[i][j] == 'T') {
  42. tang.r = i;
  43. tang.c = j;
  44. tang.key = m;
  45. tang.t = -1;
  46. } else if (map[i][j] == 'K') {
  47. sun.r = i;
  48. sun.c = j;
  49. } else if (map[i][j] == 'S') {
  50. snake[i][j] = s++;
  51. }
  52. }
  53. }
  54. queue<Pos> q;
  55. q.push(sun);
  56. memset(flag, 0, sizeof(flag));
  57. flag[sun.r][sun.c][0][0] = true;
  58. while (!q.empty()) {
  59. Pos &cur = q.front();
  60. q.pop();
  61. if (cur == tang) {
  62. tang.t = cur.t;
  63. break;
  64. }
  65. for (int i = 0; i < 4; i++) {
  66. int nx = cur.r + dx[i];
  67. int ny = cur.c + dy[i];
  68. }
  69. }
  70. if (tang.t == -1)
  71. printf("impossible\n");
  72. else
  73. printf("%d\n", tang.t);
  74. }
  75. return 0;
  76. }