热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

HDU1043:Eigth(康托展开)

ProblemDescription The15-puzzlehasbeenaroundforover100years;evenifyoudon'tknowitby

 

Problem 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, 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

 

8数码问题网上解说很多,我是用康托展开做的

 

#include
#include
#include
#include
#include
using namespace std;
int s[20],pos[20],end[20];
int hash[20],vis[1000000];
string ans[1000000];
struct node
{
string step;
int num[10],val;
int pp;
};
int solve(int s[])
{
int sum=0;
for(int i=0; i<9; i++)
{
int num=0;
for(int j=i+1; j<9; j++)
if(s[j] sum+=(num*hash[9-i-1]);
}
return sum+1;
}
int to[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
char path[10] = "durl";
void bfs()
{
memset(vis,0,sizeof(vis));
int i,j;
node a,next;
queue Q;
for(i = 0; i<8; i++)
a.num[i] = i+1;
a.num[8] = 0;
a.pp = 8;
a.step = "";
a.val = 46234;
ans[a.val] = "";
Q.push(a);
while(!Q.empty())
{
a = Q.front();
Q.pop();
int x = a.pp/3;
int y = a.pp%3;
for(i = 0; i<4; i++)
{
int tx = x+to[i][0];
int ty = y+to[i][1];
if(tx<0 || ty<0 || tx>2 || ty>2)
continue;
next = a;
next.pp = 3*tx+ty;
next.num[a.pp] = next.num[next.pp];
next.num[next.pp] = 0;
next.val = solve(next.num);
if(!vis[next.val])
{
vis[next.val]=true;
next.step=path[i]+next.step;
Q.push(next);
ans[next.val]=next.step;
}
}
}
}
int main()
{
int i;
char ch;
node a;
int c[10];
hash[0] = 1;
for(i = 1; i<=10; i++)
hash[i] = hash[i-1]*i;
bfs();
while(cin >> ch)
{
if(ch == ‘x‘)
c[0] = 0;
else c[0]=ch-‘0‘;
for(int i=1; i<9; i++)
{
cin>>ch;
if(ch==‘x‘)
c[i]=0;
else c[i]=ch-‘0‘;
}
int k;
k = solve(c);
if(vis[k])
cout < else
cout <<"unsolvable" < }
return 0;
}


HDU1043:Eigth(康托展开),布布扣,bubuko.com


推荐阅读
  • AsyncDisplayKit2.0教程(下)
    AsyncDisplayKit2.0Tutorial:AutomaticLayout原文:AsyncDisplayKit2.0Tutorial:Automatic ... [详细]
  • 题目:Givenanintegerarray,youneedtofindone continuoussubarray thatifyouonlysortthissubarrayin ... [详细]
  • Spark 贝叶斯分类算法
    一、贝叶斯定理数学基础我们都知道条件概率的数学公式形式为即B发生的条件下A发生的概率等于A和B同时发生的概率除以B发生的概率。根据此公式变换,得到贝叶斯公式:即贝叶斯定律是关于随机 ... [详细]
  • 安全3AAuthentication:认证Authorzation:授权Accouting|Audition:审计用户管理用户:UID:0,不一定是root,root的uid非0时 ... [详细]
  • ARToolKitunity
    ARToolKit为开源的AR库,相对于高通和easyAr有几点特点:1)开源2)识别项目可以动态添加(详细在后)3)识别文件可以本地生成4)目前只能识别图片(目前为.jpg格式) ... [详细]
  • 【实践】基于RTThread的智慧路灯案例实验分享
    之前分享了基于LiteOS的智慧农业案例实验分享基于LiteOS的智慧农业案例实验分享,阅读量挺不错,看样子大家都挺喜欢这种实验。那咱们就再来一个类似的实验:基于RT-Thread ... [详细]
  • 步骤一:明确主打的核心目标用户群(对应产品侧的定位)这个核心目标用户群体是该产品成功挤进市场的切入点,甚至是撬动市场的支点和撬杠。市面上几乎很少有产品是专门给一个群体用而对其他群体 ... [详细]
  • UDP协议开发
    UDP是用户数据报协议(UserDatagramProtocol,UDP)的简称,其主要作用是将网络数据流量压缩成数据报形式,提供面向事务的简单信息传送服务。与TCP协议不同,UD ... [详细]
  • webpack 配置IP 和端口号
    最近在用webpack搭建本地服务器的时候,因为不想总是用localhost来跑,所以对webpack.config.js进行了配置,如下devServer:{publicPath ... [详细]
  • #includestdafx.h#includeiostream#includesstream#includemap#includestring ... [详细]
  • 第38天:Python decimal 模块
    by程序员野客在我们开发工作中浮点类型的使用还是比较普遍的,对于一些涉及资金金额的计算更是不能有丝毫误差,Python的decimal模块为浮点型精确计算提供了支持。1简介deci ... [详细]
  • 利用ipv6技术,废旧笔记本变成server
    如果你家的路由器已经get到了ipv6地址,并且你家的电脑也获取了有效的ipv6地址,在广域网的设备可以访问到。那恭喜你,再配合我这个dd ... [详细]
  • SortalinkedlistinO(nlogn)timeusingconstantspacecomplexity.这道题属于人生中第一次对链表进行操作,首先,不同于C++中的st ... [详细]
  • Redis 外部访问设置
    1、错误原因Redis搭建好后一般都是使用编程语言进行连接调用,默认Redis的设置是不允许外界访问的,连接Redis只能通过本地(127.0.0.1)来连接,而不能使用网络IP( ... [详细]
  • 法国人家喻户晓的一首歌,很老的一首了。旋律轻盈,歌词温馨会把你带回到小时候的回忆中去。Ilrevientàmamémoire一切都回到我脑海中Dessouvenirsfamilie ... [详细]
author-avatar
我和你2602883283
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有