POJ1077&&HDU1043(八数码,IDA*+曼哈顿距离)
Eight
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 30127 | Accepted: 13108 | Special Judge | ||
Description
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 x
where the only legal operation is to exchange 'x' with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8
9 x 10 12 9 10 x 12 9 10 11 12 9 10 11 12
13 14 11 15 13 14 11 15 13 14 x 15 13 14 15 x
r-> d-> r->
The letters in the previous row indicate which neighbor of the 'x' tile is swapped with the 'x' tile at each step; legal values are 'r','l','u' and 'd', for right, left, up, and down, respectively.
Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing 'x' tile, of course).
In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three
arrangement.
Input
1 2 3
x 4 6
7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8
Output
Sample Input
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr
//2016.8.25
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map> using namespace std; int a[][], b[], d[], sx, sy, deep;
bool ok;
map<int, bool> vis;
char dir[] = {'u', 'd', 'l', 'r'};
int dx[] = {-, , , };//分别对应上下左右四个方向
int dy[] = {, , -, }; bool solve()//求逆序对判断是否有解,偶数有解,奇数无解
{
int cnt = ;
for(int i = ; i <= ; i++)
for(int j = ; j < i; j++)
if(b[i] && b[j]>b[i])
cnt++;
return !(cnt%);
} int Astar()//启发函数,假设每个数字可以从别的数字头上跨过去,计算到达自己应该到的位置所需要的步数,即计算该数到达其正确位置的曼哈顿距离,h为各点曼哈顿距离之和
{
int h = ;
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
if(a[i][j]!=)
{
int nx = (a[i][j]-)/;
int ny = (a[i][j]-)%;
h += (abs(i-nx-)+abs(j-ny-));
}
return h;
} int toInt()//把矩阵转换为int型数字
{
int res = ;
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
res = res*+a[i][j];
return res;
} void IDAstar(int x, int y, int step)
{
if(ok)return ;
int h = Astar();
if(!h && toInt()==)//找到答案
{
for(int i = ; i < step; i++)
cout<<dir[d[i]];
cout<<endl;
ok = ;
return ;
}
if(step+h>deep)return ;//现实+理想<现状,则返回,IDA*最重要的剪枝
int now = toInt();
if(vis[now])return ;//如果状态已经搜过了,剪枝,避免重复搜索
vis[now] = true;
for(int i = ; i < ; i++)
{
int nx = x+dx[i];
int ny = y+dy[i];
if(nx>=&&nx<=&&ny>=&&ny<=)
{
d[step] = i;
swap(a[x][y], a[nx][ny]);
IDAstar(nx, ny, step+);
swap(a[x][y], a[nx][ny]);
d[step] = ;
}
}
return;
} int main()
{
char ch;
while(cin >> ch)
{
ok = false;
deep = ;
int cnt = ;
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; j++)
{
if(i==&&j==);
else cin >> ch;
if(ch == 'x')
{
a[i][j] = ;
sx = i;
sy = j;
}else
a[i][j] = ch - '';
b[cnt++] = a[i][j];
}
}
if(!solve())
{
cout<<"unsolvable"<<endl;
continue;
}
while(!ok)
{
vis.clear();
IDAstar(sx, sy, );
deep++;//一层一层增加搜索的深度
}
} return ;
}
POJ1077&&HDU1043(八数码,IDA*+曼哈顿距离)的更多相关文章
- HDU - 3567 IDA* + 曼哈顿距离 + 康托 [kuangbin带你飞]专题二
这题难度颇大啊,TLE一天了,测试数据组数太多了.双向广度优先搜索不能得到字典序最小的,一直WA. 思路:利用IDA*算法,当前状态到达目标状态的可能最小步数就是曼哈顿距离,用于搜索中的剪枝.下次搜索 ...
- HDU1043 八数码(BFS + 打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 , 康托展开 + BFS + 打表. 经典八数码问题,传说此题不做人生不完整,关于八数码的八境界 ...
- HDU 1043 八数码(八境界)
看了这篇博客的讲解,挺不错的.http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html 判断无解的情况(写完七种境界才发现有直接判 ...
- 八数码(IDA*算法)
八数码 IDA*就是迭代加深和A*估价的结合 在迭代加深的过程中,用估计函数剪枝优化 并以比较优秀的顺序进行扩展,保证最早搜到最优解 需要空间比较小,有时跑得比A*还要快 #include<io ...
- Eight(经典题,八数码)
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)
八数码问题也称为九宫问题.(本想查查历史,结果发现居然没有词条= =,所谓的历史也就不了了之了) 在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同.棋盘上还有一个 ...
- HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)
题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...
- 【双向广搜+逆序数优化】【HDU1043】【八数码】
HDU上的八数码 数据强的一B 首先:双向广搜 先处理正向搜索,再处理反向搜索,直至中途相遇 visit 和 队列都是独立的. 可以用一个过程来完成这2个操作,减少代码量.(一般还要个深度数组) 优化 ...
- 15 Puzzle (4乘4谜题) IDA*(DFS策略与曼哈顿距离启发) 的C语言实现
大家好!这是我的第一篇博客,由于之前没有撰写博客的经验,并且也是初入计算机和人工智能领域,可能有些表述或者理解不当,还请大家多多指教. 一.撰写目的 由于这个学期在上算法与数据结构课程的时候,其中一个 ...
随机推荐
- 解决IntelliJ IDEA 创建Maven项目速度慢问题 DarchetypeCatalog
原因 IDEA根据maven archetype的本质,其实是执行mvn archetype:generate命令,该命令执行时,需要指定一个archetype-catalog.xml文件. 该命令的 ...
- Varnish Cache
1 Varnish简介 Varnish是高性能且开源的反向代理服务器和HTTP加速器(cache server).其开发者Poul-Henning Kamp是FreeBSD核心的开发人员之一.Varn ...
- 用java.util.Observable实现Observer模式
http://blog.csdn.net/dada360778512/article/details/6977758 http://blog.csdn.net/luoweifu/article/det ...
- [转] Eclipse中已安装的插件如何卸载
转自 : http://blog.csdn.net/macong01/article/details/7631105 最近在Eclipse中安装了一个插件,导致Eclipse使用的时候有些问题,就找了 ...
- Linux建立信任主机
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1.先在本机上面装一个sshpass 的安装包 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2.ssh-ke ...
- 用OpenSSL生成自签名证书在IIS上搭建Https站点(用于iOS的https访问)
前提: 先安装openssl,安装有两种方式,第一种直接下载安装包,装上就可运行:第二种可以自己下载源码,自己编译.这里推荐第一种. 安装包:http://slproweb.com/products/ ...
- C#数码管控件(转)
源:一个简单Led控件 Led控件,可能是非常经典和常用的了,但是很遗憾的是,这个名称至少涵盖了三种控件:1.是7段式的有发光二极管构成的Led,通常用来显示数字.2.是指示灯,通常用来闪烁,指示电源 ...
- iOS调试-LLDB学习总结
from:http://www.jianshu.com/p/d6a0a5e39b0e LLDB阐述 LLDB 是一个有着 REPL 的特性和 C++ ,Python 插件的开源调试器.LLDB 绑定在 ...
- [转]centos 6.5安装caffe
centos 6.5安装caffe 原文地址:http://blog.csdn.net/wqzghost/article/details/47447377 总结:在安装protobuf,hdf5等 ...
- ModelDriven动作(转)
所谓ModelDriven ,意思是直接把实体类当成页面数据的收集对象.比如,有实体类User 如下: package cn.com.leadfar.struts2.actions; public c ...