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
| int n, m, w, A, B, C, D; bool operator<(vector<vector<int>> A, vector<vector<int>> B) { repp(i, n)repp(j, n)if (A[i][j] != B[i][j])return A[i][j] < B[i][j]; return 0; } bool operator==(vector<vector<int>> A, vector<vector<int>> B) { repp(i, n)repp(j, n)if (A[i][j] != B[i][j])return 0; return 1; } vector<vector<int>> a(5), b(5), x(5), y(5), cpy(5); bool f[5][5][5][5], f_[5][5][5][5], F[100]; map<vector<vector<int>>, int>vis; void rotate(int N) { while (N--) { repp(i, n)repp(j, n)cpy[i][j] = x[n + 1 - j][i]; x = cpy; repp(i, n)repp(j, n)cpy[i][j] = y[n + 1 - j][i]; y = cpy; repp(i1, n)repp(j_1, n)repp(i2, n)repp(j2, n)f_[i1][j_1][i2][j2] = f[n + 1 - j_1][i1][n + 1 - j2][i2]; repp(i1, n)repp(j_1, n)repp(i2, n)repp(j2, n)f[i1][j_1][i2][j2] = f_[i1][j_1][i2][j2]; } } bool move() { repp(i, n) { repp(j, n) { if (!x[i][j])continue; int k = j; while (k > 1 && !f[i][k - 1][i][k] && !x[i][k - 1]) { x[i][k - 1] = x[i][k]; x[i][k] = 0; k--; if (y[i][k] == x[i][k]) { x[i][k] = y[i][k] = 0; break; } if (y[i][k] && y[i][k] != x[i][k])return 0; } } } return 1; } int bfs() { queue<pair<vector<vector<int>>, int>>q; q.push(mp(a, 0)); vis[a] = T_; while (!q.empty()) { x = q.front().fi; int val = q.front().se; q.pop(); init(F, 0); bool tmp = 0; repp(i, n)repp(j, n)if (x[i][j])F[x[i][j]] = tmp = 1; if (!tmp)return val; repp(i, n)repp(j, n) { if (F[b[i][j]])y[i][j] = b[i][j]; else y[i][j] = 0; } vector<vector<int>>xx = x, yy = y; bool suc; suc = move(); if (suc && vis[x] != T_)q.push(mp(x, val + 1)), vis[x] = T_; x = xx, y = yy; rotate(1); suc = move(); rotate(3); if (suc && vis[x] != T_)q.push(mp(x, val + 1)), vis[x] = T_; x = xx, y = yy; rotate(2); suc = move(); rotate(2); if (suc && vis[x] != T_)q.push(mp(x, val + 1)), vis[x] = T_; x = xx, y = yy; rotate(3); suc = move(); rotate(1); if (suc && vis[x] != T_)q.push(mp(x, val + 1)), vis[x] = T_; } return -1; } void solve() { repp(i, n)a[i].resize(5), b[i].resize(5), x[i].resize(5), y[i].resize(5), cpy[i].resize(5); repp(i, n)repp(j, n)a[i][j] = b[i][j] = 0; repp(i, n)repp(j, n)repp(ii, n)repp(jj, n)f[i][j][ii][jj] = 0; repp(i, m) { cin >> A >> B; a[A + 1][B + 1] = i; } repp(i, m) { cin >> A >> B; b[A + 1][B + 1] = i; } repp(i, w) { cin >> A >> B >> C >> D; A++, B++, C++, D++; f[A][B][C][D] = f[C][D][A][B] = 1; } int ans = bfs(); if (ans == -1) cout << "Case " << T_ << ": impossible\n\n"; else cout << "Case " << T_ << ": " << ans << " moves\n\n"; }
|