1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| #include <bits/stdc++.h> using namespace std; const int maxn = 25, inf = 0x3f3f3f3f; char s[maxn][maxn];
const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
const char A[4] = {'N', 'S', 'W', 'E'}, a[4] = {'n', 's', 'w', 'e'}; int r, c; string path;
struct node { int bx_, by_, px_, py_; string path_; } bg;
void init() { for (int i = 1; i <= r; ++i) { for (int j = 1; j <= c; ++j) { if (s[i][j] == 'B') { bg.bx_ = i, bg.by_ = j, s[i][j] = '.'; } else if (s[i][j] == 'S') { bg.px_ = i, bg.py_ = j, s[i][j] = '.'; } } } }
bool valid(int x, int y) { return x >= 1 && y >= 1 && x <= r && y <= c && s[x][y] != '#'; }
int calc(string &str) { int res = 0; for (int i = 0; i < str.length(); ++i) { if (isupper(str[i])) ++res; } return res; }
bool bfs2(node from, node to) { queue<node> q; bool vis[maxn][maxn]; memset(vis, 0, sizeof(vis)); vis[from.bx_][from.by_] = 1; q.push(from); while (!q.empty()) { node u = q.front(); q.pop(); if (u.px_ == to.px_ && u.py_ == to.py_) { path = u.path_; return 1; } for (int i = 0; i < 4; ++i) { int x = u.px_ + dx[i], y = u.py_ + dy[i]; if (!valid(x, y)) continue; if (vis[x][y]) continue; node nxt; nxt.px_ = x, nxt.py_ = y, nxt.path_ = u.path_ + a[i]; vis[x][y] = 1; q.push(nxt); } } return 0; }
string bfs1() { init(); queue<node> q; string ans = "Impossible."; int path_len = inf, box_len = inf; int box[maxn][maxn][4], people[maxn][maxn][4]; memset(box, 0x3f, sizeof(box)), memset(people, 0x3f, sizeof(people)); q.push(bg); while (!q.empty()) { node u = q.front(); q.pop(); if (s[u.bx_][u.by_] == 'T') { int pl = u.path_.length(), bl = calc(u.path_); if (bl < box_len || (bl == box_len && pl < path_len)) { ans = u.path_, path_len = pl, box_len = bl; } continue; } for (int i = 0; i < 4; ++i) { int x = u.bx_ + dx[i], y = u.by_ + dy[i]; if (!valid(x, y)) continue; node people_from = u, people_to; people_from.path_ = ""; if (i == 0) { people_to.px_ = u.bx_ + 1, people_to.py_ = u.by_; } else if (i == 1) { people_to.px_ = u.bx_ - 1, people_to.py_ = u.by_; } else if (i == 2) { people_to.py_ = u.by_ + 1, people_to.px_ = u.bx_; } else { people_to.py_ = u.by_ - 1, people_to.px_ = u.bx_; } if (!valid(people_to.px_, people_to.py_)) continue; if (!bfs2(people_from, people_to)) continue; node nxt; nxt.bx_ = x, nxt.by_ = y, nxt.px_ = u.bx_, nxt.py_ = u.by_; nxt.path_ = u.path_ + path + A[i]; int bl = calc(nxt.path_), pl = nxt.path_.length() - bl; if (box[u.bx_][u.by_][i] < bl || people[u.bx_][u.by_][i] < pl) continue; box[u.bx_][u.by_][i] = bl, people[u.bx_][u.by_][i] = pl; q.push(nxt); } } return ans; }
int main() { int num = 0; while (cin >> r >> c && r && c) { for (int i = 1; i <= r; ++i) cin >> (s[i] + 1); cout << "Maze #" << ++num << endl << bfs1() << endl << endl; } return 0; }
|