poj1077八数码问题——境界六(IDA*)
来源:互联网 发布:海迅开料软件 编辑:程序博客网 时间:2024/05/24 06:15
迭代加深
#include<stdio.h>#include<string.h>#include<string>#include<algorithm>#include<iostream>#include<set>#include<map>#include<queue>#include<stack>#include<vector>#include<math.h>#include<stdlib.h>using namespace std;#define M 400000struct node {char s[20];int pos;}st;int h[4]={-1,0,1,0};int z[4]={0,1,0,-1};char fa[]={"urdl"};int path[M],limit;int geth(char *s)//取得h,为当前位置与目标位置的manhattan距离 {int i,j,h=0,x,y,x1,y1;for(i=0;i<9;i++){if(s[i]!='0'){x=i/3;y=i%3;x1=(s[i]-'0'-1)/3;y1=(s[i]-'0'-1)%3;h+=abs(x-x1)+abs(y-y1);}}return h;}bool isAns(node t){int i,j;if(strcmp(t.s,"123456780")==0)return 1;return 0;}int judge(int x,int y){ return (x>=0&&x<3&&y>=0&&y<3);}int IDAstar(node t,int len){if(len==limit){return isAns(t);}int i,j,k,x,y;for(i=0;i<4;i++){if(len>0&&abs(i-path[len-1])==2)continue;node tp=t;x=tp.pos/3;y=tp.pos%3;x+=h[i];y+=z[i];if(judge(x,y)){tp.pos=x*3+y;swap(tp.s[tp.pos],tp.s[t.pos]);path[len]=i;if(geth(tp.s)+len<=limit&&IDAstar(tp,len+1))return 1;}}return 0;}void output(){int i,j,k;for(i=0;i<limit;i++)printf("%c",fa[path[i]]);puts("");}int main(){int i,j,k; char c; for(i=0;i<9; )//读取字符串 { scanf("%c",&c); if(c == 'x') { st.s[i]='0'; st.pos=i; i++; } else if(c>='0'&&c<='9') { st.s[i] = c; i++; } else continue; }st.s[9]='\0';limit=geth(st.s);while(!IDAstar(st,0))limit++;output();return 0;}
- poj1077八数码问题——境界六(IDA*)
- poj1077八数码问题——境界五(A*)
- poj1077八数码问题——境界二
- 八数码与IDA*问题 HDU1043&&POJ1077
- POJ1077 八数码问题
- 八数码第八境界——IDA*+逆序数判无解
- POJ1077&HDU1043 Eight 八数码第八境界 IDA* hash 康托展开 奇偶剪枝
- 八数码 poj1077 Eight(A*、IDA*)
- 八数码问题多种解法比较(poj1077宽搜,双向宽搜,A*,IDA*+扩展)(持续更新)
- poj1077 hdu1043 Eight 八数码问题
- Poj1077/HDU1043(A*搜索)八数码问题
- POJ1077(经典的八数码问题)
- POJ1077八数码问题哈希,bfs
- hdu1043八数码问题——境界三
- IDA*——Luogu1379 八数码难题
- POJ 1077 Eight(八数码第八境界|IDA*+曼哈顿距离+判断是否有解)
- POJ1077(8数码问题)
- 菜鸟系列——八数码八境界
- Field 'id' doesn't have a default value
- Android OS - Processes and the Zygote!
- UTF8编码转换 CChineseCode
- MFC Windows 程序设计 第二章 在窗口中绘图
- linux命令大全
- poj1077八数码问题——境界六(IDA*)
- MFC Windows 程序设计 第三章 鼠标与键盘
- HDU 4661 Message Passing 【Tree】
- MFC Windows 程序设计 第四章 菜单
- Myeclipse项目的调试与发布
- 进程调度
- MFC Windows 程序设计 第五 MFC集合类
- linux多线程的总结(pthread用法)
- MFC Windows 程序设计 第六章 文件I/O与串行化