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
| #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <queue>
using namespace std;
const int MAX = 1 << 17; const int x[32] = {0, 1, 3, 11, 10, 8, 10, 2, 8, 9, 9, 0, 0, 1, 3, 11, 10, 8, 9, 2, 12, 13, 13, 4, 14, 6, 12, 4, 5, 7, 15, 14}; const int y[32] = {1, 3, 11, 10, 8, 0, 2, 3, 9, 1, 11, 2, 4, 5, 7, 15, 14, 12, 13, 6, 13, 5, 15, 6, 6, 7, 4, 5, 7, 15, 14, 12}; const int aim = ((1 << 8) - 1); int dist[MAX];
struct Node { int st; int depth; };
int hash(const int& st, const int& op) { return st ^ ((1 << (15 - x[op])) | (1 << (15 - y[op]))); }
void bfs() { Node node, next; queue<Node> q; node.st = aim; node.depth = 0; dist[aim] = 0; q.push(node); while (!q.empty()) { node = q.front(); q.pop(); if (node.depth > 3) break; next.depth = node.depth + 1; for (int i = 0; i < 32; i++) { if ((node.st & (1 << (15 - x[i]))) != (node.st & (1 << (15 - y[i])))) { next.st = hash(node.st, i); if (dist[next.st] == 1000) { dist[next.st] = next.depth; q.push(next); } } } } }
int main() { int t, cnt = 0; int a; int st; for (int i = 0; i < MAX; i++) { dist[i] = 1000; } bfs(); scanf("%d", &t); while (t--) { cnt++; st = 0; for (int i = 0; i < 16; i++) { scanf("%d", &a); if (a == 1) st |= (1 << (15 - i)); } a = dist[st]; if (a > 3) { printf("Case #%d: more\n", cnt); } else { printf("Case #%d: %d\n", cnt, a); } } return 0; }
|