Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22207   Accepted: 9846   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

题意:这是一个8数码问题,听着好高端的样子;就是给你一个3*3的矩阵,包括1~8和x;例
1  2  3

x 4 6

7 5 8 问最少需要变换x几步成为 1 2 3
4 5 6
7 8 x 的形式; 这题的关键是找到一个哈希函数,使得矩阵形成的排列与一个自然数一一对应,这里采用的是全排列的哈希函数,另一个就是BFS加打印路径了;
 #include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std; const int maxn = ;//根据全排列的哈希函数,n+1个数的排列可以对应n个数的多进制形式,这里九个数对应多进制的最大值为9!-1;
int factorial[] = {,,,,,,,,};
int pow[] = {,,,,,,,,};
int head,tail;
bool vis[maxn]; struct node
{
char status;
int id,num,pre;
}que[maxn]; int hash(int num)//全排列的哈希函数
{
int a[],key,i,j,c;
for(i = ; i < ; i++)
{
a[i] = num%;//a数组倒着存的num,所以求逆序数的时候条件是a[j]<a[i];
num = num/;
}
key = ;
for(i = ; i < ; i++)
{
for(j = ,c = ; j < i; j++)
{
if(a[j] < a[i])
c++;
}
key += c*factorial[i];
}
return key;
} void change(int num,int a,int b,char status)//a位置上的数和b位置上的数互换;
{
int n1,n2;
n1 = num/pow[a]%;
n2 = num/pow[b]%;
num = num - (n1-n2)*pow[a] + (n1-n2)*pow[b];
int key = hash(num);
if(!vis[key])
{
vis[key] = true;
que[tail].num = num;
que[tail].id = b;
que[tail].status = status;
que[tail++].pre = head;
}
} //打印路径
void print(int head)
{
char s[];
int c = ;
while(que[head].status != 'k')
{
s[c++] = que[head].status;
head = que[head].pre;
}
s[c] = '\0';
for(int i = c-; i >= ; i--)
{
printf("%c",s[i]);
}
printf("\n");
} int main()
{
char c;
int num,id,t; num = ;
for(int i = ; i < ; i++)
{
cin>>c;
if(c == 'x')
{
t = ;
id = i;
}
else t = c-'';
num = *num+t;
}
bool flag = false;
memset(vis,false,sizeof(vis)); head = ;
tail = ;
que[].id = id;
que[].num = num;
que[].status = 'k'; while(head < tail)
{
num = que[head].num;
id = que[head].id;
if(num == )
{
flag = true;
break;
}
if(id > )
change(num,id,id-,'u'); if(id < )
change(num,id,id+,'d'); if(id% != )
change(num,id,id-,'l');
if(id% != )
change(num,id,id+,'r');
head++; }
if(flag) print(head);
else printf("unsolvable\n");
return ;
}

Eight(bfs+全排列的哈希函数)的更多相关文章

  1. 字符串哈希函数(String Hash Functions)

    哈希函数举例 http://www.cse.yorku.ca/~oz/hash.html Node.js使用的哈希函数 https://www.npmjs.org/package/string-has ...

  2. lintcode:哈希函数

    题目: 哈希函数 在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数.一个好的哈希函数可以尽可能少地产生冲突.一种广泛使用的哈希函数算法是使用数值33,假 ...

  3. 算法初级面试题05——哈希函数/表、生成多个哈希函数、哈希扩容、利用哈希分流找出大文件的重复内容、设计RandomPool结构、布隆过滤器、一致性哈希、并查集、岛问题

    今天主要讨论:哈希函数.哈希表.布隆过滤器.一致性哈希.并查集的介绍和应用. 题目一 认识哈希函数和哈希表 1.输入无限大 2.输出有限的S集合 3.输入什么就输出什么 4.会发生哈希碰撞 5.会均匀 ...

  4. lintcode-->哈希函数

    在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数.一个好的哈希函数可以尽可能少地产生冲突.一种广泛使用的哈希函数算法是使用数值33,假设任何字符串都是基 ...

  5. php的哈希函数

    哈希函数: echo password_hash("rasmuslerdorf", PASSWORD_DEFAULT)."\n"; 验证函数: boolean  ...

  6. 经常使用哈希函数的比較及其C语言实现

    基本概念 所谓完美哈希函数.就是指没有冲突的哈希函数.即对随意的 key1 != key2 有h(key1) != h(key2). 设定义域为X,值域为Y, n=|X|,m=|Y|.那么肯定有m&g ...

  7. djb2:一个产生简单的随机分布的哈希函数

    目录 LCG算法 示例代码 djb2 示例代码 为什么选择参数33和 33 was chosen because: 5381 was chosen because 哈希选择参考 LCG算法 djb2与 ...

  8. lintcode-128-哈希函数

    128-哈希函数 在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数.一个好的哈希函数可以尽可能少地产生冲突.一种广泛使用的哈希函数算法是使用数值33,假 ...

  9. Java集合(八)哈希表及哈希函数的实现方式

    Java集合(八)哈希表及哈希函数的实现方式 一.哈希表 非哈希表的特点:关键字在表中的位置和它之间不存在一个确定的关系,查找的过程为给定值一次和各个关键字进行比较,查找的效率取决于和给定值进行比较的 ...

随机推荐

  1. iBatis 的删除一条记录

    Student.xml 设置删除参数的类型,可以是一个对象的 <delete id="delStudent" parameterClass="int" & ...

  2. mysql - 初探

    1,查询所有数据库名称: show databases; 2,查询所有表: use database_name; show tables; 3,查询表中的所有字段: desc table_name;

  3. Oracle利用dbms_metadata.get_ddl查看DDL语句

    当我们想要查看某个表或者是表空间的DDL的时候,可以利用dbms_metadata.get_ddl这个包来查看. dbms_metadata包中的get_ddl函数详细参数 GET_DDL函数返回创建 ...

  4. iOS sizeWithFont 过期 is deprecated

    原文: http://www.cnblogs.com/A--G/p/4819189.html iOS 2.0之后 sizeWithFont就被弃用了: //计算textview 高度 - (float ...

  5. jQuery - 获取内容和属性

    jQuery 拥有可操作 HTML 元素和属性的强大方法. jQuery DOM 操作 jQuery 中非常重要的部分,就是操作 DOM 的能力. jQuery 提供一系列与 DOM 相关的方法,这使 ...

  6. javascript中常用的DOM事件

    //常用事件 onclick 点击事件 onmousedown 鼠标按下 onmousemove 鼠标移动 onmouseup 鼠标抬起 onmouseover 鼠标放上 onmouseout 鼠标放 ...

  7. AbstractFactory 模式

    ///////////////////////Product.h////////////// #ifndef _PRODUCT_H_ #define _PRODUCT_H_ class Abstrac ...

  8. C#编程连接数据库,通过更改配置文件切换数据库功能。

           该实例主要应用情景:假如某公司用mysql当做数据库服务器,由于发现mysql数据库在运行中出现不稳定情况,针对这情况,厂家要求更换连接数据库方式,改用SQL server数据库,来满足 ...

  9. spring 构造注入 异常 Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments

    你可能在做项目的时候,需要在项目启动时初始化一个自定义的类,这个类中包含着一个有参的构造方法,这个构造方法中需要传入一些参数. spring提供的这个功能叫“构造注入”, applicationCon ...

  10. iOS 多线程详解

    iOS开发 多线程 概览 机器码是按顺序执行的,一个复杂的多步操作只能一步步按顺序逐个执行.改变这种状况可以从两个角度出发: 对于单核处理器,可以将多个步骤放到不同的线程,这样一来用户完成UI操作后其 ...