我A*然后错了! 其实我不知道我的A*哪里错了…… 但是不管了,以后再练A*好了。
题目思想: 任何一个全排列数字,可以用康拓展开的知识,来映射到一个数字上去。 http://zh.wikipedia.org/wiki/%E5%BA%B7%E6%89%98%E5%B1%95%E5%BC%80
WIKI有详细的讲解。
当然,用散列函数也可以让全排列数字能有映射效果,MAP什么都可以解决这个问题……
其实就是BFS啦! 核心问题就是,判断这个状态是否进过队列。 把这个8位全排列,用康拓展开映射成数字,或者直接散列函数,或者扔进平衡树,都可以解决问题……
我的程序因为原来想写A*,然后写挂了……所以很丑很长……而且也不快
Compiling... Compile: OK Executing... Test 1: TEST OK [0.005 secs, 4024 KB] Test 2: TEST OK [0.003 secs, 4024 KB] Test 3: TEST OK [0.003 secs, 4024 KB] Test 4: TEST OK [0.003 secs, 4024 KB] Test 5: TEST OK [0.022 secs, 4024 KB] Test 6: TEST OK [0.035 secs, 4024 KB] Test 7: TEST OK [0.051 secs, 4024 KB] Test 8: TEST OK [0.073 secs, 4024 KB] All tests OK.
/* TASK:msquare LANG:C++ */ #include <iostream> #include <queue> #include <cstdio> #include <cstring> using namespace std; int factorial[10]={1,1,2,6,24,120,720,5040,40320,362880}; bool vis[10]; int pnext[10], ppre[10]; inline void clean() { for (int i = 1; i <= 8; ++ i) { ppre[i] = i - 1; pnext[i] = i + 1; } pnext[0] = 1; ppre[9] = 9; } inline void del(int k) { pnext[ppre[k]] = pnext[k]; ppre[pnext[k]] = ppre[k]; } inline int cantor_expand(int *r, int n = 8)//r数组(含0下标) 有n位,转化为一个数字 { int ans = 0; clean(); for (int i = 0; i != n; ++ i) { int tmp = -1; for (int j = 0; j < r[i]; j = pnext[j], ++ tmp); del(r[i]); //cout<<tmp<<" " << n - i - 1 << endl; ans += tmp * factorial[n - i - 1]; } return ans; } inline void cantor_expand(int k, int *r, int n = 8)//k这个数字,有n位,转化进r数组 { clean(); for (int i = 0; i != n - 1; ++ i) { int tmp = k / factorial[n - i - 1], count = 0, j; for (j = pnext[0]; ; j = pnext[j]) { ++ count; if (count == tmp + 1) break; } r[i] = j; del(j); k %= factorial[n - i - 1]; } r[n - 1] = pnext[0]; } //init 函数:处理最短路径问题 int g[9][9];//保存每个数字到每个地方的距离 void init() { int num[2][4],t=0; for (int i = 0; i <= 1; ++ i) for (int j = 0; j != 4; ++ j) num[i][j] = t ++ ; memset(g, 40, sizeof(g)); int inf = g[0][0]; for (int i = 0; i != 9; ++ i) g[i][i] = 0; g[1][8] = g[2][7] = g[3][6] = g[4][5] = 1; g[8][1] = g[7][2] = g[6][3] = g[5][4] = 1; g[1][2] = g[2][3] = g[3][4] = g[4][1] = 1; g[8][7] = g[7][6] = g[6][5] = g[5][8] = 1; g[6][7] = 1; for (int k = 0; k != 9; ++ k) for (int i = 0; i != 9; ++ i) for (int j = 0; j != 9; ++ j) if (g[i][k] + g[k][j] < g[i][j]) g[i][j] = g[i][k] + g[k][j]; } int cantor[10]; struct node { int way; int did, willdid; int num; node(int W, int D, int NUM):way(W),did(D),num(NUM) { cantor_expand(num, cantor); //for (int i = 0; i != 8; ++ i) cout<<cantor[i]<<" ";cout<<endl; willdid = 0; for (int i = 0; i != 8; ++ i) { int tmp = g[cantor[i]][i + 1]; willdid = max(willdid, tmp); } willdid = 0; } }; inline bool operator < (node A, node B) { if (A.did + A.willdid == B.did + B.willdid) return A.way > B.way; //估价步数相同,使用转移 return A.did + A.willdid > B.did + B.willdid; } bool v[41000] = {0}; int pre[41000]={0}, plan[41000]; int cantor_tmp[10]; //priority_queue<node>q; queue<node>q; inline int one(int now, int did) { for (int i = 0; i != 8; ++ i) cantor[i] = cantor_tmp[i]; swap(cantor[0], cantor[7]); swap(cantor[1], cantor[6]); swap(cantor[2], cantor[5]); swap(cantor[3], cantor[4]); int tmp = cantor_expand(cantor); if (!v[tmp]) { v[tmp] = 1; pre[tmp] = now; plan[tmp] = 1; q.push(node(1, did, tmp)); } return tmp; } inline int two(int now, int did) { for (int i = 0; i != 8; ++ i) cantor[i] = cantor_tmp[i]; int tmp = cantor[3]; for (int i = 3; i >= 1; -- i) cantor[i] = cantor[i - 1]; cantor[0] = tmp; tmp = cantor[4]; for (int i = 4; i <= 7; ++ i) cantor[i] = cantor[i + 1]; cantor[7] = tmp; tmp = cantor_expand(cantor); if (!v[tmp]) { v[tmp] = 1; pre[tmp] = now; plan[tmp] = 2; q.push(node(2, did, tmp)); } return tmp; } inline int three(int now, int did) { for (int i = 0; i != 8; ++ i) cantor[i] = cantor_tmp[i]; int tmp = cantor[6]; cantor[6] = cantor[5]; cantor[5] = cantor[2]; cantor[2] = cantor[1]; cantor[1] = tmp; tmp = cantor_expand(cantor); if (!v[tmp]) { v[tmp] = 1; pre[tmp] = now; plan[tmp] = 3; q.push(node(3, did, tmp)); } return tmp; } int output[41000],ot=0; void doit() { int aim, tmp, begin(0), a[10]; for (int i = 0; i != 8; ++ i) cin >> a[i]; aim = cantor_expand(a); q.push(node(0, 0, begin)); v[begin] = 1; while (1) { int now = q.front().num; int did = q.front().did; q.pop(); cantor_expand(now, cantor_tmp); if (one(now, did + 1) == aim) break; if (two(now, did + 1) == aim) break; if (three(now, did + 1) == aim) break; } int now = aim; while (now != begin) { output[ot++] = plan[now]; now = pre[now]; } cout<<ot<<endl; for (int i = ot - 1; i >= 0; -- i) cout<<(char)(output[i]+'A' - 1); cout<<endl; } int main() { freopen("msquare.in","r",stdin); freopen("msquare.out","w",stdout); init(); doit(); return 0; }