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. POJ 2482 Stars in Your Window

    线段树+离散化+扫描线 AC之后,又认真读了一遍题目,好文章. #include<cstdio> #include<map> #include<algorithm> ...

  2. 设计 无状态的类,而不是 stateful

    0down votefavorite   I have created a Database Abstraction Layer over PDO to refrain from creating m ...

  3. 深度学习框架Caffe的编译安装

    深度学习框架caffe特点,富有表达性.快速.模块化.下面介绍caffe如何在Ubuntu上编译安装. 1. 前提条件 安装依赖的软件包: CUDA 用来使用GPU模式计算. 建议使用 7.0 以上最 ...

  4. 有趣的keil MDK细节(转)

    源:有趣的keil MDK细节 1.MDK中的char类型的取值范围是? 在MDK中,默认情况下,char 类型的数据项是无符号的,所以它的取值范围是0-255.它们可以显式地声明为signed ch ...

  5. iOS 主动抛出异常

    http://blog.csdn.net/jymn_chen/article/details/38096749 http://blog.sina.com.cn/s/blog_7270a06c0101b ...

  6. Ural Vol1(dif>=900)

    目前已AC:  2 1040.Airline Company(构造) 题目要求与每个顶点相连的所有边编号最大公约数为1,其实只要其中的两条边编号互质,所有边编号的最大公约数一定为1.我们知道相邻的数字 ...

  7. P4语言编程快速开始 实践二

    参考:P4语言编程快速开始 上一篇系列博客:P4语言编程快速开始 实践二 Demo 2 本Demo所做的修改及实现的功能: 为simple_router添加一个计数器(counter),该计数器附加( ...

  8. 使用Tomcat搭建图片服务器,使图片能够用链接访问

    在后台和前端交互时,遇到了后台存储的图片,前端根据地址无法访问,使用Tomcat搭建图片服务器 1.找到tomcat下的server.xml文件 2.配置文件下加入service节点 <!--为 ...

  9. NSString总结

    [from]http://www.jianshu.com/p/7994b0ad6b88 问题:NSString到底是不是字符串? NSString 是 OC中专门处理字符串的对象!提供了转换大小写,拼 ...

  10. C#中BASE64和图片相互转换

    //图片 转为    base64编码的文本        private void button1_Click(object sender, EventArgs e)        {        ...