题意:经典的八数码=3=
3*3的格子,里面有1~8这8个数字,还有一个空格x,移动空格的位置,直到移到1~8按顺序排好,输出移动的序列。
解法:看到题果断写了个广搜……然后T了……百度了一下说广搜虽然慢了点但是也是可以过的嘛……默默看了眼自己代码……唔……好像他们都不是用string路径的……
实在懒得改了,学个新做法吧,哦吼吼吼这个A*看起来很神奇啊……学一下学一下
600ms+过了……嗯……跟别人广搜一个时间啊……_(:з」∠)_实在不想改记路径的方法啊……
A*我觉得就是一个更聪明的广搜……每个状态的权值为每个数到最终状态的曼哈顿距离之和加上已走过的步长,用优先队列维护……
还有就是每个状态序列可以转换成一个排列的id……涨姿势了0v0
代码:
#include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<string.h> #include<math.h> #include<limits.h> #include<time.h> #include<stdlib.h> #include<map> #include<queue> #include<set> #include<stack> #include<vector> #define LL long long using namespace std; int fac[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 }; int order(vector <int> v) { int i, j, temp, num; num = 0; for (i = 0; i < 8; i++) { temp = 0; for (j = i + 1; j < 9; j++) { if (v[j] < v[i]) temp++; } num += fac[v[i] -1] * temp; } return num; } bool vis[400000]; int dir1[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1}; char dir2[] = "udlr"; struct node { vector <int> v; string path; node(vector <int> tv, string tpath) { v = tv; path = tpath; } node() {} bool operator < (const node &tmp) const { int sum1 = 0, sum2 = 0; for(int i = 0; i < 9; i++) sum1 += abs((v[i] - 1) / 3 - i / 3) + abs((v[i] - 1) % 3 - i % 3); for(int i = 0; i < 9; i++) sum2 += abs((tmp.v[i] - 1) / 3 - i / 3) + abs((tmp.v[i] - 1) % 3 - i % 3); return sum1 + path.size() > sum2 + tmp.path.size(); } }; string bfs(vector <int> st) { memset(vis, 0, sizeof vis); vis[order(st)] = 1; priority_queue <node> q; q.push(node(st, "")); while(!q.empty()) { node tmp = q.top(); if(order(tmp.v) == 0) return tmp.path; q.pop(); int sx, sy; for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) if(tmp.v[i * 3 + j] == 9) { sx = i; sy = j; } for(int i = 0; i < 4; i++) { int tx = sx + dir1[i][0], ty = sy + dir1[i][1]; if(tx < 0 || tx > 2 || ty < 0 || ty > 2) continue; swap(tmp.v[tx * 3 + ty], tmp.v[sx * 3 + sy]); int id = order(tmp.v); if(!vis[id]) { vis[id] = 1; q.push(node(tmp.v, tmp.path + dir2[i])); } swap(tmp.v[tx * 3 + ty], tmp.v[sx * 3 + sy]); } } return "unsolvable"; } int main() { char input[2]; while(~scanf("%s", input)) { vector <int> v; if(input[0] == 'x') v.push_back(9); else v.push_back(input[0] - '0'); for(int i = 0; i < 8; i++) { scanf("%s", input); if(input[0] == 'x') v.push_back(9); else v.push_back(input[0] - '0'); } int sum = 0; for(int i = 1; i < 9; i++) for(int j = 0; j < i; j++) if(v[i] != 9 && v[j] != 9 && v[i] < v[j]) sum++; if((sum & 1) == 0) cout << bfs(v) << endl; else puts("unsolvable1"); } return 0; }