主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html

代码一:像BFS那样,把棋盘数组放在结构体中。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+;
#define AIM 1 //123456789的哈希值为1 struct node
{
int s[];
int loc;
}; int fac[] = { , , , , , , , , };
int dir[][] = { -,, ,, ,-, , };
char op[] = {'u', 'd', 'l', 'r' }; int cantor(int s[]) //获得哈希函数值
{
int sum = ;
for(int i = ; i<; i++)
{
int num = ;
for(int j = i+; j<; j++)
if(s[j]<s[i]) num++;
sum += num*fac[-i];
}
return sum+;
} int dis_h(int s[]) //获得曼哈顿距离
{
int dis = ;
for(int i = ; i<; i++)
if(s[i]!=)
{
int x = i/, y = i%;
int xx = (s[i]-)/, yy = (s[i]-)%;
dis += abs(x-xx) + abs(y-yy);
}
return dis;
} char path[];
bool IDAstar(node now, int depth, int pre, int limit)
{
if(dis_h(now.s)==) //搜到123456789
{
path[depth] = '\0';
puts(path);
return true;
} int x = now.loc/;
int y = now.loc%;
for(int i = ; i<; i++)
{
if(i+pre== || i+pre==) continue; //方向与上一步相反, 剪枝
int xx = x + dir[i][];
int yy = y + dir[i][];
if(xx>= && xx<= && yy>= && yy<=)
{
node tmp = now;
tmp.s[x*+y] = tmp.s[xx*+yy];
tmp.s[xx*+yy] = ;
tmp.loc = xx*+yy;
path[depth] = op[i];
if(depth++dis_h(tmp.s)<=limit) //在限制内
if(IDAstar(tmp, depth+, i, limit))
return true;
}
}
return false;
} int main()
{
char str[];
while(gets(str))
{
node now;
int cnt = ;
for(int i = ; str[i]; i++)
{
if(str[i]==' ') continue;
if(str[i]=='x') now.s[cnt] = , now.loc = cnt++;
else now.s[cnt++] = str[i]-'';
} int limit;
for(limit = ; limit<=; limit++) //迭代加深搜
if(IDAstar(now, , INF, limit))
break;
if(limit>)
puts("unsolvable");
}
}

代码二:根据DFS的特点,由于棋盘每次只交换一对,所以可以只开一个棋盘数组。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+; int M[];
int fac[] = { , , , , , , , , };
int dir[][] = { -,, ,, ,-, , };
char op[] = {'u', 'd', 'l', 'r' }; int cantor(int s[]) //获得哈希函数值
{
int sum = ;
for(int i = ; i<; i++)
{
int num = ;
for(int j = i+; j<; j++)
if(s[j]<s[i]) num++;
sum += num*fac[-i];
}
return sum+;
} int dis_h(int s[]) //获得曼哈顿距离
{
int dis = ;
for(int i = ; i<; i++)
if(s[i]!=)
{
int x = i/, y = i%;
int xx = (s[i]-)/, yy = (s[i]-)%;
dis += abs(x-xx) + abs(y-yy);
}
return dis;
} char path[];
bool IDAstar(int loc, int depth, int pre, int limit)
{
int h = dis_h(M);
if(depth+h>limit)
return false; if(h==) //搜到123456789
{
path[depth] = '\0';
puts(path);
return true;
} int x = loc/;
int y = loc%;
for(int i = ; i<; i++)
{
if(i+pre== || i+pre==) continue; //方向与上一步相反, 剪枝
int xx = x + dir[i][];
int yy = y + dir[i][];
if(xx>= && xx<= && yy>= && yy<=)
{
swap(M[loc], M[xx*+yy]);
path[depth] = op[i];
if(IDAstar(xx*+yy, depth+, i, limit))
return true;
swap(M[loc], M[xx*+yy]);
}
}
return false;
} int main()
{
char str[];
while(gets(str))
{
int cnt = , loc;
for(int i = ; str[i]; i++)
{
if(str[i]==' ') continue;
if(str[i]=='x') M[cnt] = , loc = cnt++;
else M[cnt++] = str[i]-'';
} int limit;
for(limit = ; limit<=; limit++) //迭代加深搜
if(IDAstar(loc, , INF, limit))
break;
if(limit>)
puts("unsolvable");
}
}

POJ1077 Eight —— IDA*算法的更多相关文章

  1. HDU3567 Eight II —— IDA*算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3567 Eight II Time Limit: 4000/2000 MS (Java/Others)  ...

  2. 【学时总结】 ◆学时·II◆ IDA*算法

    [学时·II] IDA*算法 ■基本策略■ 如果状态数量太多了,优先队列也难以承受:不妨再回头看DFS-- A*算法是BFS的升级,那么IDA*算法是对A*算法的再优化,同时也是对迭代加深搜索(IDF ...

  3. HUD 1043 Eight 八数码问题 A*算法 1667 The Rotation Game IDA*算法

    先是这周是搜索的题,网站:http://acm.hdu.edu.cn/webcontest/contest_show.php?cid=6041 主要内容是BFS,A*,IDA*,还有一道K短路的,.. ...

  4. 八数码(IDA*算法)

    八数码 IDA*就是迭代加深和A*估价的结合 在迭代加深的过程中,用估计函数剪枝优化 并以比较优秀的顺序进行扩展,保证最早搜到最优解 需要空间比较小,有时跑得比A*还要快 #include<io ...

  5. HDU1560 DNA sequence —— IDA*算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 DNA sequence Time Limit: 15000/5000 MS (Java/Oth ...

  6. POJ1077 Eight —— A*算法

    主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html 关于A*算法:g(n)表示从起点到任意节点n的路径花费,h(n)表示从节点n到目标节点路径花费 ...

  7. IDA*算法——骑士精神

    例题 骑士精神 Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者 ...

  8. UVA - 11212 Editing a Book(IDA*算法+状态空间搜索)

    题意:通过剪切粘贴操作,将n个自然段组成的文章,排列成1,2,……,n.剪贴板只有一个,问需要完成多少次剪切粘贴操作可以使文章自然段有序排列. 分析: 1.IDA*搜索:maxn是dfs的层数上限,若 ...

  9. 还不会ida*算法?看完这篇或许能理解点。

    IDA* 算法分析 IDA* 本质上就是带有估价函数和迭代加深优化的dfs与,A * 相似A *的本质便是带 有估价函数的bfs,估价函数是什么呢?估价函数顾名思义,就是估计由目前状态达 到目标状态的 ...

随机推荐

  1. Spoj-NPC2015A Eefun Guessing Words

    Eefun Guessing Words Eefun is currently learning to read. His way of learning  is unique, by trying ...

  2. response.setHeader参数、用法的介绍

    response.setHeader 是用来设置返回页面的头 meta 信息, 使用时 response.setHeader( name, contect ); meta是用来在HTML文档中模拟HT ...

  3. 云计算与 OpenStack

    “云计算” 算是近年来最热的词了.现在 IT 行业见面不说这三个字您都不好意思跟人家打招呼. 对于云计算,学术界有各种定义,大家有兴趣可以百度一下. CloudMan 这里主要想从技术的角度谈谈对云计 ...

  4. linux下面MySQL变量修改及生效

    今天在访问mysql项目的时候突然报500错误,没有找到连接,因此想到mysql的连接时间. mysql> show global variables; 主要就是连接时间是28800(8小时), ...

  5. [MFC] CFile读写文件实现(高效)

    1.文件写入 void CMFCApplication1Dlg::Write() { CFile file; CString FileName = "D:\\100w.txt"; ...

  6. hdu1671 Phone List [字典树 hash]

    传送门 Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. Python入门--13--爬虫一

    URL的格式一般为(带方括号的是可选的): protocol://hostname[:port]/path/[;parameters][?query]#fragment URL由三部分组成: 第一部分 ...

  8. Python入门--7--元祖:列表的顽固亲戚

    一.创建和访问一个元祖 zheshiyige_yuanzu=(1,2,3,4,5,6) #创建一个元祖 zheshiyige_yuanzu[1] #打印第二个元素 zheshiyige_yuanzu[ ...

  9. ajax 分页(jquery分页插件pagination) 小例1

    <link rel="stylesheet" href="/plugins/jQuery/page/pagination.css"/> <sc ...

  10. CodeForces - 813C The Tag Game (树的dfs遍历)

    [传送门]http://codeforces.com/problemset/problem/813/C [题目大意]两个人玩游戏,一个人跑一个人追,轮流决策,可以走也可以不走.给你一棵树,想要从某个结 ...