Eight HDU - 1043(八数码+搜索)
题意:
就是还原八数码。输出操作。
题目:
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 45 6 7 89 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 45 6 7 8 5 6 7 8 5 6 7 8 5 6 7 89 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 xr-> 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, several descriptions of configuration of the 8 puzzle. One 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. Do not print a blank line between cases.
Sample Input
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr
分析:
用广搜打表,根据广搜的性质,第一次出现某种状态,即是最短路
注意,方向问题。最终结果倒着输出
(因为此题用搜索即可做出,网上有康拓展开(利用康拓展开判重。通过康拓展开知一共有362879种状态。用逆序数判断可行解见八数码可行解。然后宽搜。)和A*(A*是一种启发式搜索,g为已花代价,h为估计的剩余代价,而A*是根据f=g+h作为估价函数进行排列,也就是优先选择可能最优的节点进行扩展。)做法,自行去看,我不会QAQ)orz
康拓展开:https://www.xuebuyuan.com/3261380.html
A*:https://blog.csdn.net/acm_cxlove/article/details/7745323
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<map>
#include<string>
using namespace std;
map<int,int>book;
map<int,string>ans;
int c[4][2]= {0,1,1,0,0,-1,-1,0};
struct node
{int x,y;string s;int k[3][3];
};
void dfs()
{queue<node>q;node u,v;u.x=2,u.y=2;int xx=1;for(int i=0; i<3; i++)for(int j=0; j<3; j++)u.k[i][j]=xx++;u.k[2][2]=0;book[123456780]=1;q.push(u);while(!q.empty()){v=q.front();q.pop();for(int i=0; i<4; i++){int yy=1;int dx=v.x+c[i][0];int dy=v.y+c[i][1];if(dx<0||dy<0||dx>=3||dy>=3)continue;for(int o=0; o<3; o++)for(int j=0; j<3; j++){u.k[o][j]=v.k[o][j];}swap(u.k[dx][dy],u.k[v.x][v.y]);for(int o=0; o<3; o++)for(int j=0; j<3; j++){yy=yy*10+u.k[o][j];}if(book[yy])continue;book[yy]=1;u.x=dx;u.y=dy;if(i==0)u.s=v.s+'l';else if(i==1)u.s=v.s+'u';else if(i==2)u.s=v.s+'r';elseu.s=v.s+'d';q.push(u);ans[yy]=u.s;}}
}
int main()
{char w[50];dfs();while(gets(w)){int ant=1;int len=strlen(w);for(int i=0; i<len; i++){if(w[i]>='1'&&w[i]<='8')ant=ant*10+w[i]-'0';else if(w[i]=='x')ant*=10;}if(book[ant]){string m=ans[ant];int l=m.size();for(int i=l-1; i>=0; i--)cout<<m[i];cout<<endl;}elsecout<<"unsolvable"<<endl;}return 0;
}
Eight HDU - 1043(八数码+搜索)相关推荐
- HDU - 1043 Eight (A*搜索)
A*模板 可以发现无论怎么动x逆序对奇偶性不变,可用这个性质判unsolve 状态判重可用康托展开 估价函数h(n)的求法为计算图上每一个点到目标的曼哈顿距离之和 1 #include<cmat ...
- hdu 1043 Eight 经典八数码问题
hdu 1043 Eight 经典八数码问题 题意描述:给出一个3×3的矩阵(包含1-8数字和一个字母x),经过一些移动格子上的数后得到连续的1-8,最后一格是x,要求最小移动步数. 算法分析:经典的 ...
- hdu 1043 ,pku 1077 Eight ,八数码问题
某位神牛曾说过,此题是涉及到人生完不完整的一道题.. Goodness大牛曾总结了 八数码的八重境界 : http://www.cnblogs.com/goodness/archive/2010/05 ...
- HDU 1043 Eight(八数码)
HDU 1043 Eight(八数码) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- HDU 1043 Eight(八数码第七境界|A*+哈希+曼哈顿距离)
题意:八数码. 思路:将上一篇博客的估计函数h更改为求当前状态到目标状态的曼哈顿距离,因为每次都是和空格交换,所以计算的时候不计算空格的曼哈顿距离就可以满足估计函数的两条性质: 1.h(n)>h ...
- HUD 1043 Eight 八数码问题 A*算法 1667 The Rotation Game IDA*算法
先是这周是搜索的题,网站:http://acm.hdu.edu.cn/webcontest/contest_show.php?cid=6041 主要内容是BFS,A*,IDA*,还有一道K短路的,.. ...
- 算法提高课-搜索-A*(A star)算法-AcWing 179. 八数码:A星算法求解
题目分析 来源:acwing 分析: A*算法是什么呢? A算法是一种bfs的变式,需要用到一个"估价函数",用来计算任意状态到目标状态所需代价的估计值.然后在搜索中,维护一个堆, ...
- 八数码问题——双向广度优先搜索解决
八数码问题:在3×3的方格棋盘上,摆放着1到8这八个数码,有1个方格是空的,其初始状态如图1所看到的,要求对空格运行空格左移.空格右移.空格上移和空格下移这四个操作使得棋盘从初始状态到目标状态. 搜索 ...
- 题目2:隐式图的搜索问题(A*算法解决八数码)
数据结构课程实践系列 题目1:学生成绩档案管理系统(实验准备) 题目2:隐式图的搜索问题(A*算法解决八数码) 题目3:文本文件单词的检索与计数(实验准备) 文章目录 数据结构课程实践系列 题目1:学 ...
最新文章
- 全新开源,《Pytorch常用函数函数手册》开放下载!内含200余个函数!
- 计算机桌面文件夹删除如何找回,电脑删除文件如何恢复 误操作的一剂后悔药...
- 'CUDA driver version is insufficient for CUDA runtime version
- BACKUP PENDING状态的解除
- pycharm 调试(debug)模式时界面上的 mute breakpoint 是什么意思?(239)
- Apache Cassandra和Java入门(第一部分)
- fn映射 mac 键盘_【新鲜评测】高颜值、低延迟、多模式跨平台办公神器-米物蓝牙键盘...
- php输入安全验证漏洞,PHP 输入验证错误漏洞
- abb机器人建立工件坐标系_abb机器人坐标系说明介绍
- 95-170-046-源码-Time-Flink时间系统系列之ProcessFunction使用分析
- AndroidOpenCV摄像头预览全屏问题
- SSD网络及代码理解
- 多媒体系统导论 实验一:基于Photoshop的图像处理
- HDU 3533 简单bfs 主要是MLE问题
- 信息安全从业者书单推荐(2020.6.28更新)
- outlook 签名 设置
- D200和D2X区别
- 什么是android SDK和API
- 如何将PDF压缩突破限制大小
- vue导出excel加一个进度条_vue项目中如何把数据导出成excel文件
热门文章
- Android之在Java socket作为服务器里面返回数据头部怎么写入浏览器需要下载文件的文件名
- 服务器之Apache和Tomcat和Nginx的理解和对比
- LeetCode之Hamming Distance
- python任务调度平台 界面_分布式任务调度平台XXL-JOB
- 需要多快的速度,才能在抽走桌布之后保持桌面物体不掉?
- 一个浪漫又悲情的爱情故事...
- 老师看完都吐血的五道题
- 编程语言的“别样”编年史
- Lanchester战争模型:用可分离变量的微分方程占卜战事
- vue 一个页面有点请求需要同时发送_前端性能优化,这些你都需要知道