hdu 1043 Eight(BFS经典)
来源:互联网 发布:淘宝上有好看衣服吗 编辑:程序博客网 时间:2024/05/16 01:23
题意:。。。
思路:
小白上有讲解。
编码:速度快,但适用范围小...
哈希:拼人品。。
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;}
0 0
- hdu 1043 Eight(BFS经典)
- HDU-1043 Eight(BFS)
- HDU 1043 Eight(经典八数码)(BFS+STL)
- HDU 1043 Eight 反向BFS
- HDU 1043 Eight(BFS)
- HDU 1043 Eight (经典八数码问题,BFS+状态枚举+伪哈希)
- HDU 1043 Eight(经典八数码问题)对比POJ 1077(bfs)
- hdu 1043 /poj 1077 Eight(经典八数码问题,BFS+康托展开)
- hdu 1043/poj 1077 Eight (八数码 经典搜索题 bfs + 康托展开)
- hdu 1043 Eight 双向BFS/A*算法
- HDU 1043 Eight(BFS+康拓展开)
- hdu 1043 Eight(bfs+康托)
- [BFS+康托展开]Hdu 1043 Eight
- HDU 1043 && POJ 1077 Eight bfs || 双向bfs || A*搜索
- hdu 1043 Eight 经典八数码问题
- hdu 1043 eight 八数码问题 bfs 和 A*
- HDU 1043 Eight(bfs+康托展开)
- HDU 1043 Eight(BFS打表+状态去重)
- UIButton 解析
- linux设备驱动模型一三基础结构之Kset
- Java基础:Servlet之HelloWorld
- HDU 4704 Sum (隔板原理 + 费马小定理)
- n-1位数 scanf(\n%*c%d)
- hdu 1043 Eight(BFS经典)
- 唤醒沉睡的博客
- iOS的控件--pickerview
- 在PyQt5里面玩Android 的Material Design风格
- leetrcode 3 Longest Substring Without Repeating Characters
- 浅谈HDU1201
- 谈谈对移动app测试的了解
- <csapp> pipeline lab (《深入理解计算机系统》lab7)
- 单词长度