HDU1043BFS 康托展开 八数码
来源:互联网 发布:apk软件编写 编辑:程序博客网 时间:2024/06/04 18:41
经典的八数码问题
至于康托展开,百度知道。。。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;int jc[11] = {1,1,2,6,24,120,720,5040,40320,362880};bool vis[444444];string path[444444];int aim;int mx[4] = {0,0,1,-1};int my[4] = {1,-1,0,0};char direction[4] = {'l','r','u','d'};struct State{ int x,y; int s[11]; int state; string path;};char mp[4][4];bool in(State a){ if(a.x >= 0 && a.x < 3 && a.y >= 0 && a.y < 3) return true; else return false;}int contor(int s[]){ int sum = 0; for(int i = 0 ; i < 9 ; i ++){ int num = 0; for(int j = i + 1 ; j < 9 ; j ++){ if(s[j] < s[i]){ num++; } } sum += num*jc[9-i-1]; } return sum + 1;}queue<State> q;void BFS(){ memset(vis,0,sizeof(vis)); State s; for(int i = 0 ; i < 8 ; i ++){ s.s[i] = i+1; } s.s[8] = 0; s.state = contor(s.s); s.x = 2; s.y = 2; s.path = ""; path[s.state] = ""; vis[s.state] = 1; q.push(s); while(!q.empty()){ State cnt = q.front(); q.pop(); for(int i = 0 ; i < 4 ; i ++){ State nt = cnt; nt.x = cnt.x + mx[i]; nt.y = cnt.y + my[i]; if(in(nt) == 0) continue; nt.s[cnt.x*3+cnt.y] = nt.s[nt.x*3+nt.y]; nt.s[nt.x*3+nt.y] = 0; nt.state = contor(nt.s); if(vis[nt.state] == 0){ path[nt.state] = direction[i]+nt.path; nt.path = path[nt.state]; vis[nt.state] = 1; q.push(nt); } } }}int main(){ char str; State now; BFS(); while(cin>>str){ if(str == 'x'){ now.x = 0; now.y = 0; now.s[0] = 0; } else now.s[0] = str - '0'; for(int i = 1 ; i < 9 ; i ++){ cin>>str; if(str == 'x'){ now.x = i/3; now.y = i%3; now.s[i] = 0; } else now.s[i] = str-'0'; } now.state = contor(now.s); if(vis[now.state] == 1) cout<<path[now.state]<<endl; else cout<<"unsolvable"<<endl; } return 0;}
- HDU1043BFS 康托展开 八数码
- 康托展开求八数码问题
- 八数码(康托展开)
- 康托展开(八数码问题)
- [POJ]1077 Eight 八数码:康托展开+BFS
- hdu1043Eight (经典的八数码)(康托展开+BFS)
- hdu1043Eight (经典的八数码)(康托展开+BFS)
- codevs1225 八数码难题(A*搜索+康托展开)
- 【康托展开+状压BFS】poj1077 Eight(八数码问题)
- POJ1077 HDU1043 Eight 八数码 (A*+康托展开)
- POJ 1077 Eight 八数码问题[康托展开 + BFS]
- BFS+康托展开(洛谷1379 八数码难题)
- POJ 1077 八数码(康托展开+暴力bfs)
- 借助八数码问题,双向广搜,康托展开,逆序数奇偶性
- 蓝桥杯 历届试题 九宫重排 经典八数码问题 A*算法+康托展开
- HDU 1043 Eight (BFS·八数码·康托展开)
- HDU 1043 双向广搜 八数码 康托展开 逆序数
- hdu 1043 /poj 1077 Eight(经典八数码问题,BFS+康托展开)
- [Java]EasyXls - 操作excel很简单
- rqnoj-74
- VSS下载地址及安装使用
- 使用Eclipse构建Maven的SpringMVC项目
- UVa 10192 - Vacation
- HDU1043BFS 康托展开 八数码
- Memcached真的过时了吗?Redis与Memcached的比较
- C++项目中的extern "C" {}
- vsprintf
- 网络协议01 邮差与邮局 (网络协议概观)
- 写了个最简单的jQuery插件
- 第六次作业
- 如何修改ubuntu 的Grub来修改启动项
- 复制不完全和自我赋值问题