POJ1077 Eight —— IDA*算法
主页面: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*算法的更多相关文章
- HDU3567 Eight II —— IDA*算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3567 Eight II Time Limit: 4000/2000 MS (Java/Others) ...
- 【学时总结】 ◆学时·II◆ IDA*算法
[学时·II] IDA*算法 ■基本策略■ 如果状态数量太多了,优先队列也难以承受:不妨再回头看DFS-- A*算法是BFS的升级,那么IDA*算法是对A*算法的再优化,同时也是对迭代加深搜索(IDF ...
- HUD 1043 Eight 八数码问题 A*算法 1667 The Rotation Game IDA*算法
先是这周是搜索的题,网站:http://acm.hdu.edu.cn/webcontest/contest_show.php?cid=6041 主要内容是BFS,A*,IDA*,还有一道K短路的,.. ...
- 八数码(IDA*算法)
八数码 IDA*就是迭代加深和A*估价的结合 在迭代加深的过程中,用估计函数剪枝优化 并以比较优秀的顺序进行扩展,保证最早搜到最优解 需要空间比较小,有时跑得比A*还要快 #include<io ...
- HDU1560 DNA sequence —— IDA*算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 DNA sequence Time Limit: 15000/5000 MS (Java/Oth ...
- POJ1077 Eight —— A*算法
主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html 关于A*算法:g(n)表示从起点到任意节点n的路径花费,h(n)表示从节点n到目标节点路径花费 ...
- IDA*算法——骑士精神
例题 骑士精神 Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者 ...
- UVA - 11212 Editing a Book(IDA*算法+状态空间搜索)
题意:通过剪切粘贴操作,将n个自然段组成的文章,排列成1,2,……,n.剪贴板只有一个,问需要完成多少次剪切粘贴操作可以使文章自然段有序排列. 分析: 1.IDA*搜索:maxn是dfs的层数上限,若 ...
- 还不会ida*算法?看完这篇或许能理解点。
IDA* 算法分析 IDA* 本质上就是带有估价函数和迭代加深优化的dfs与,A * 相似A *的本质便是带 有估价函数的bfs,估价函数是什么呢?估价函数顾名思义,就是估计由目前状态达 到目标状态的 ...
随机推荐
- Codeforces961F. k-substrings
$n \leq 1000000$的字符串,对每一个子串$i$~$n-i+1$,求他最长的一个既是前缀又是后缀的子串. 这题要求的东西具有“对称性”,不充分利用难以解决.这里的“对称性”不仅指询问是对称 ...
- Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创)
Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创) 由于java interface中声明的字段在编译时会自动加上static final的修饰符,即声明为常量.因而inter ...
- vi 和vim 的区别以及用法
具体用法参考:http://blog.csdn.net/xuesnowce/article/details/53117352 它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所 ...
- 标准C程序设计七---07
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- Scrapy学习-7-数据存储至数据库
使用MySQL数据库存储 安装mysql模块包 pip install mysqlclient 相关库文件 sudo apt-get install libmysqlclient-devel sudo ...
- Linux 系统的常用命令之 rm ,rm -rf , rm -f 以及rm 命令的其他参数命令
1.rm -rf * 删除当前目录下的所有文件,这个命令很危险,应避免使用. 所删除的文件,一般都不能恢复! 2.rm -f 其中的,f参数 (f --force ) 忽略不存在的文件,不显示任何信息 ...
- 树莓派LED指示灯说明
原文:http://shumeipai.nxez.com/2014/09/30/raspberry-pi-led-status-detail.html?variant=zh-cn LED亮灯状态 LE ...
- flask的run的运行参数含义
直接阅读源代码吧: 在flask的app.py里,查看run函数的定义 def run(self, host=None, port=None, debug=None, **options): &quo ...
- Android 系统广播机制
一.Android应用程序注冊广播接收器(registerReceiver)的过程分析 參考Android应用程序注冊广播接收器(registerReceiver)的过程分析http://blog.c ...
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
sql server 关于表中只增标识问题 由于我们系统时间用的过长,数据量大,设计是采用自增ID 我们插入数据的时候把ID也写进去,我们可以采用 关闭和开启自增标识 没有关闭的时候 ,提示一下错 ...