Eight

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30127   Accepted: 13108   Special Judge

Description

The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let's call the missing tile 'x'; the object of the puzzle is to arrange the tiles so that they are ordered as:

 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

You will receive a description of a configuration of the 8 puzzle. The description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus 'x'. For example, this puzzle

 1  2  3

x 4 6

7 5 8

is described by this list:

 1 2 3 x 4 6 7 5 8 

Output

You will print to standard output either the word ``unsolvable'', if the puzzle has no solution, or a string consisting entirely of the letters 'r', 'l', 'u' and 'd' that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning of the line.

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*+曼哈顿距离)的更多相关文章

  1. HDU - 3567 IDA* + 曼哈顿距离 + 康托 [kuangbin带你飞]专题二

    这题难度颇大啊,TLE一天了,测试数据组数太多了.双向广度优先搜索不能得到字典序最小的,一直WA. 思路:利用IDA*算法,当前状态到达目标状态的可能最小步数就是曼哈顿距离,用于搜索中的剪枝.下次搜索 ...

  2. HDU1043 八数码(BFS + 打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 , 康托展开 + BFS + 打表. 经典八数码问题,传说此题不做人生不完整,关于八数码的八境界 ...

  3. HDU 1043 八数码(八境界)

    看了这篇博客的讲解,挺不错的.http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html 判断无解的情况(写完七种境界才发现有直接判 ...

  4. 八数码(IDA*算法)

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

  5. Eight(经典题,八数码)

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  6. ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)

    八数码问题也称为九宫问题.(本想查查历史,结果发现居然没有词条= =,所谓的历史也就不了了之了) 在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同.棋盘上还有一个 ...

  7. HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)

    题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...

  8. 【双向广搜+逆序数优化】【HDU1043】【八数码】

    HDU上的八数码 数据强的一B 首先:双向广搜 先处理正向搜索,再处理反向搜索,直至中途相遇 visit 和 队列都是独立的. 可以用一个过程来完成这2个操作,减少代码量.(一般还要个深度数组) 优化 ...

  9. 15 Puzzle (4乘4谜题) IDA*(DFS策略与曼哈顿距离启发) 的C语言实现

    大家好!这是我的第一篇博客,由于之前没有撰写博客的经验,并且也是初入计算机和人工智能领域,可能有些表述或者理解不当,还请大家多多指教. 一.撰写目的 由于这个学期在上算法与数据结构课程的时候,其中一个 ...

随机推荐

  1. js中判断输入框是否为空(判断是一串空的字符串)

    function ltrim(str) {  if(str.length==0)    return(str);   else {    var idx=0;    while(str.charAt( ...

  2. tooltip 鼠标移动上去出现图片或文字与title大同小异

    代码如下: <script type="text/javascript" src="jquery-1.3.2.min.js"></script ...

  3. HttpServletRequest.getServletContext()一直提示找不到,而引出的问题

    开发j2ee项目的时候,需要用到servlet-api,如果使用了maven,web项目可以在pom.xml中手动加入所需jar包,达到与依赖j2ee libarary同样的功能.可问题来了: 1. ...

  4. openstack controller ha测试环境搭建记录(十二)——配置neutron(计算节点)

    在计算节点配置内核参数:vi /etc/sysctl.confnet.ipv4.conf.all.rp_filter=0net.ipv4.conf.default.rp_filter=0 在计算节点使 ...

  5. 子序列和问题 acm

    题目描述 给定一个序列 {a1,a2,…,an},定义从a[l]到a[r]的连续子序列的和为sum[l,r],即sum[l,r]=sigma{ai},l<=i<=r.(1<=l< ...

  6. 安装php扩展后,执行时找不到扩展 class xxx no found

    当编译安装一个新的php扩展,例如安装redis, 安装后 执行 new Redis(), 如果发现找不到class Redis,可以先检查redis.so文件是否在php的扩展目录下, 如果发现是, ...

  7. Spring 工作原理

    1.spring原理 内部最核心的就是IOC了,动态注入,让一个对象的创建不用new了,可以自动的生产,这其实就是利用java里的反射,反射其实就是在运行时动态的去创建.调用对象,Spring就是在运 ...

  8. VR元年,VR虚拟现实这只风口上的猪有怎样的变化?

    走过了2016年,无论我们承认不承认,这一年到底是不是VR元年,我们都很难否定,在这一年,VR虚拟现实生态圈有很大的变化,那么,这一年VR虚拟现实到底有怎样的改变呢?我们的VR虚拟现实生态圈,发生了什 ...

  9. Mac下配置node.js环境(Mac 10.12)

    有安装就有卸载,卸载教程参考:http://www.cnblogs.com/EasonJim/p/6287141.html 一.官方下载pkg安装包 1.安装 到官网https://nodejs.or ...

  10. centos 上网问题

    前言:由于Linux下很多软件安装必须网络环境下进行,因此,对于如何在VMware下进行上网,我折腾了至少三天,今天上午,也即五一劳动节,终于搜到一遍技术文章,经过自己实践,VMware下Linux的 ...