poj1077 Eight —— 正向bfs+康拓
来源:互联网 发布:日本dvd播放软件 编辑:程序博客网 时间:2024/05/26 09:53
题目链接:http://poj.org/problem?id=1077
代码如下:
#include<iostream>//poj1077 正向bfs+康拓#include<cstring>#include<cstdio>#define MAX 400000#define AIM 46234using namespace std;int mov[4][2] = {-1,0,1,0,0,-1,0,1}, fac[9] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320};char dir[] = "udlr";bool vis[MAX];struct Node{ int s[9]; int loc; int status; int fa; char dir;};Node cur,q[MAX];int cantor(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*fac[9 - i - 1]; } return sum + 1;}bool bfs(){ Node next; int front = 0, rear = 1; q[front] = cur; while (front < rear) { cur = q[front]; int x = cur.loc / 3; int y = cur.loc % 3; for (int i = 0; i < 4; i++) { int xx = x + mov[i][0]; int yy = y + mov[i][1]; if (xx < 0 || xx>2 || yy < 0 || yy>2)continue; next = cur; next.loc = xx * 3 + yy; next.s[cur.loc] = next.s[next.loc]; next.s[next.loc] = 0; next.fa = front; next.dir = dir[i]; next.status = cantor(next.s); if (!vis[next.status]) { vis[next.status] = true; if(next.status==AIM) {cur = next; return true;} q[rear++] = next; } } front++; } return false;}void pri(){ char ch = cur.dir; if(cur.fa>0) {cur = q[cur.fa];pri();} putchar(ch);}int main(){ char ch[50]; while(gets(ch)) { for(int i = 0,m = 0; ch[i]!=0; i++) { if(ch[i]==' ') continue; if(ch[i]=='x') { cur.loc = m; cur.s[m++] = 0;} else cur.s[m++] = ch[i]-'0'; } memset(vis,false,sizeof(vis)); vis[cantor(cur.s)] = true; if(bfs()) pri(), putchar('\n'); else cout<<"unsolvable"<<endl; } return 0;}
0 0
- poj1077 Eight —— 正向bfs+康拓
- POJ1077 Eight —— 正向BFS
- POJ1077 Eight —— 反向BFS
- POJ1077 Eight —— 双向BFS
- POJ1077——Eight
- POJ1077 Eight —— A*算法
- POJ1077 Eight —— IDA*算法
- POJ1077 Eight —— 经典的搜索问题
- poj1077 Eight
- hdu1043 Eight —— 反向bfs+康拓
- POJ1077、HDU1043 Eight 八数码问题:双向BFS、A*
- 【康托展开+状压BFS】poj1077 Eight(八数码问题)
- poj1077 /hdoj 1043 Eight
- 【POJ1077】Eight【IDA*】
- POJ 1077 Eight (正向BFS + 康托展开)
- Eight poj1077 广度优先搜索
- poj1077 hdu1043 Eight 八数码问题
- 八数码 poj1077 Eight(A*、IDA*)
- 判断输入的一个整数有多少位是1,效率要高
- 利用Python数据分析:数据规整化(二)
- java中的接口和抽象
- 总结一下最近干的一个活
- 【hdoj_2152】Fruit(母函数)
- poj1077 Eight —— 正向bfs+康拓
- Localizable.strings文件一点小知识
- zookeeper大集合
- 背景学习模型 —》codebook算法
- 利用Python数据分析:数据规整化(三)
- poj 3276 Face The Right Way
- 戒除浮躁(不知作者但是超级棒)
- gitlab无法拉起代码Deploy Key
- 区间调度问题-贪心选择