hdu 1043 Eight(BFS经典)
2015-01-25 21:32
337 查看
题意:。。。
思路:
小白上有讲解。
编码:速度快,但适用范围小...
哈希:拼人品。。
map:简单,但速度慢
HDU上单向BFS无数TLE。。代码先放这。。
思路:
小白上有讲解。
编码:速度快,但适用范围小...
哈希:拼人品。。
map:简单,但速度慢
HDU上单向BFS无数TLE。。代码先放这。。
//#include<bits/stdc++.h> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <queue> #include <stack> #include <cassert> #include <algorithm> #include <cmath> #include <climits> #include <set> #include <map> using namespace std; #define SPEED_UP iostream::sync_with_stdio(false); #define FIXED_FLOAT cout.setf(ios::fixed, ios::floatfield); #define rep(i, s, t) for(int (i)=(s);(i)<=(t);++(i)) #define urep(i, s, t) for(int (i)=(s);(i)>=(t);--(i)) typedef long long LL; const int Maxn = 3; const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; int n=3, m=3; char mm[9]; int tab[11], last[362880+100], sav[362880+100], spos; struct st{ char s[9]; int t, h, pos; st(){}; st(const char *m, int t, int h, int pos):t(t), h(h), pos(pos) { memcpy(s, m, 9); } }; st q[362880+100]; bool init() { memset(last, -1, sizeof(last)); char ch; rep(i, 0, 8) { if (scanf("%c", &ch) == 1) { if (ch == 'x') mm[i] = '9', spos = i; else mm[i] = ch; } else return false; getchar(); } return true; } int Hash(const char* a, int len) { int ret = 0; rep(i, 0, len-1) { int t = 0; rep(j, i+1, len-1) if (a[j] < a[i]) ++t; ret += tab[len-1-i]*t; } return ret; } void print_state(const st &x) { cout << endl; rep(i, 0, 2) { rep(j, 0, 2) cout << x.s[i*3+j]; cout << endl; } cout << "t: " << x.t << " pos: " << x.pos << endl; } void print(int x) { if (last[x] == -1 || last[x] == -2) return; print(last[x]); switch(sav[x]) { case 0:putchar('u');break; case 1:putchar('d');break; case 2:putchar('l');break; case 3:putchar('r');break; } } int check(const char* p) { rep(i, '1', '9') if (i != (*p++)) return 0; return 1; } void solve() { int x, y, nx, ny, npos; int hcode = Hash(mm, 9); q[0] = st(mm, 0, hcode, spos); last[hcode] = -2; int head = 0, tail = 1; while (head < tail) { const st &fr = q[head++]; int h = fr.h; //print_state(fr); if (check(fr.s)) { //cout << fr.t << endl; print(h); cout << endl; return; } x = fr.pos/3, y = fr.pos%3; rep(i, 0, 3) { nx = x + dx[i]; ny = y + dy[i]; npos = nx*3+ny; if (0 <= nx && nx < 3 && 0 <= ny && ny < 3) { st &tmp = q[tail]; tmp = fr; swap(tmp.s[fr.pos], tmp.s[npos]); int _h = Hash(tmp.s, 9); if (last[_h] != -1) continue; tmp.t += 1; tmp.h = _h; tmp.pos = npos; ++tail; sav[_h] = i; last[_h] = h; } } } puts("unsolvable"); } int main() { #ifndef ONLINE_JUDGE freopen("input.in", "r", stdin); #endif //SPEED_UP tab[0] = 1;rep(i, 1, 9) tab[i] = tab[i-1]*i; while (init()) { solve(); } return 0; }
相关文章推荐
- HDU 1043 Eight(经典八数码)(BFS+STL)
- HDU 1043 Eight (经典八数码问题,BFS+状态枚举+伪哈希)
- HDU 1043 Eight(经典八数码问题)对比POJ 1077
- 【BFS 康拓 A*】HDU - 1043 Eight
- HDU 1043 Eight (BFS·八数码·康托展开)
- hdu 1043 Eight(bfs+康托)
- HDU-1043:Eight(八数码+bfs(反向或A*))
- hdu 1043-Eight(经典八数码问题)(单向广搜 A* 状态压缩)
- hdu1043 Eight(A*/双向BFS/单项BFS打表+康托展开)
- HDU 1043 Eight(反向BFS打表+康托展开)
- hdu 1043/poj 1077 Eight (八数码 经典搜索题 bfs + 康托展开)
- HDU 1043 && POJ 1077 Eight bfs || 双向bfs || A*搜索
- poj 1077 & hdu 1043 Eight ( 多种解法:预处理、bfs、dbfs、IDA*、A*)
- HDU 1043 Eight(BFS打表+状态去重)
- HDU1043 Eight[bfs]
- hdu 1043 /poj 1077 Eight(经典八数码问题,BFS+康托展开)
- HDU-1043 Eight(BFS)
- HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)
- hdu 1043 Eight 经典八数码问题
- hdu1043 Eight 康拓展开+bfs打表