(已解决)hdu 1043 Eight (逆向bfs+打表+康托压缩)
来源:互联网 发布:双系统怎么重装ubuntu 编辑:程序博客网 时间:2024/05/22 06:04
又是玄学,感觉跟别人写的一模一样,怎么也过不了
不知道哪里有问题,先放上来好了
=============================
原来是忘了换行符。。。gg
#include <stdio.h>#include <string.h>#include <iostream>#include <queue>#include <string>using namespace std;#define maxn 370000int fac[10] = {1, 1, 2, 6, 24, 120, 720, 720 * 7, 720 * 7 * 8, 720 * 7 * 8 * 9};int dir[4][2] = {0, 1, 0, -1, -1, 0, 1, 0}; // r, l, u, dint vis[maxn];string path[maxn];char step[5] = "lrdu";struct node { int loc; // x所在位置 int ct; //康托值 int s[10];};int cantor(int a[]) { int sum = 0; for (int i = 0; i < 9; i++) { int m = 0; for (int j = i + 1; j < 9; j++) { if (a[i] > a[j]) m++; } sum += m * (fac[9 - 1 - i]); } return sum + 1;}void BFS() { queue<node> Q; node cur; for (int i = 0; i < 8; i++) { cur.s[i] = i + 1; } cur.s[8] = 0; cur.ct = cantor(cur.s); cur.loc = 8; vis[cur.ct] = 1; path[cur.ct] = ""; Q.push(cur); node next; while (!Q.empty()) { node cur = Q.front(); Q.pop(); for (int i = 0; i < 4; i++) { int x = cur.loc / 3 + dir[i][0]; int y = cur.loc % 3 + dir[i][1]; if (x > 2 || x < 0 || y > 2 || y < 0) continue; next = cur; next.loc = 3 * x + y; next.s[cur.loc] = next.s[next.loc]; next.s[next.loc] = 0; next.ct = cantor(next.s); if (!vis[next.ct]) { vis[next.ct] = 1; Q.push(next); path[next.ct] = step[i] + path[cur.ct]; } } }}int main(void) { char s[2]; int a[10]; int m; memset(vis, 0, sizeof(vis)); BFS(); while (scanf("%s", s) != EOF) { if (s[0] == 'x') a[0] = 0; else a[0] = s[0] - '0'; for (int i = 1; i < 9; i++) { scanf("%s", s); if (s[0] == 'x') a[i] = 0; else a[i] = s[0] - '0'; } m = cantor(a); if (vis[m]) cout << path[m] << '\n'; else cout << "unsolvable"; } return 0;}
阅读全文
0 0
- (已解决)hdu 1043 Eight (逆向bfs+打表+康托压缩)
- HDU 1043 Eight(反向BFS打表+康托展开)
- HDU 1043 Eight ((八数码问题)逆向BFS + 康托定理判重)
- HDU 1043 Eight(bfs+康托展开)
- hdu1043 Eight(A*/双向BFS/单项BFS打表+康托展开)
- POJ 1077 Eight & HDU 1043 Eight(康托展开+BFS)
- HDU 1043 Eight(广搜+哈希+康托展开+打表)
- hdu 1043 Eight(bfs+康托)
- [BFS+康托展开]Hdu 1043 Eight
- (多回顾)hdu 3567 Eight2 (bfs逆处理+打表+映射+康托压缩~)
- HDU 1043 Eight(BFS打表+状态去重)
- hdu 3567 Eight II (bfs+康托展开+预处理)
- HDU Eight II(BFS+康托展开)
- HDU 1043 Eight(康托展开)
- hdu 1043 /poj 1077 Eight(经典八数码问题,BFS+康托展开)
- hdu 1043/poj 1077 Eight (八数码 经典搜索题 bfs + 康托展开)
- HDU 1043 Eight(BFS)
- HDU ACM 1043 Eight->广度优先搜索(BFS)+康托展开(全排列hash)实践
- 5.0转场动画入门
- 搜索框
- ‘’足球论之网上商城项目知识点整合一‘’
- pwntools安装使用方法
- 解析Fat32文件系统
- (已解决)hdu 1043 Eight (逆向bfs+打表+康托压缩)
- 公路(最小生成树)
- 选择排序法
- spring+mybatis
- sql语句学习之having用法
- 《java编程思想》第三章之equals()方法
- 51nod_循环数组最大子段和
- PHP接收JSON格式的数据
- 第一性原理:戳中问题本质的人是怎么思考的?