Eight(bfs+全排列的哈希函数)
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 22207 | Accepted: 9846 | Special Judge | ||
Description
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
1 2 3
x 4 6
7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8
Output
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+全排列的哈希函数)的更多相关文章
- 字符串哈希函数(String Hash Functions)
哈希函数举例 http://www.cse.yorku.ca/~oz/hash.html Node.js使用的哈希函数 https://www.npmjs.org/package/string-has ...
- lintcode:哈希函数
题目: 哈希函数 在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数.一个好的哈希函数可以尽可能少地产生冲突.一种广泛使用的哈希函数算法是使用数值33,假 ...
- 算法初级面试题05——哈希函数/表、生成多个哈希函数、哈希扩容、利用哈希分流找出大文件的重复内容、设计RandomPool结构、布隆过滤器、一致性哈希、并查集、岛问题
今天主要讨论:哈希函数.哈希表.布隆过滤器.一致性哈希.并查集的介绍和应用. 题目一 认识哈希函数和哈希表 1.输入无限大 2.输出有限的S集合 3.输入什么就输出什么 4.会发生哈希碰撞 5.会均匀 ...
- lintcode-->哈希函数
在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数.一个好的哈希函数可以尽可能少地产生冲突.一种广泛使用的哈希函数算法是使用数值33,假设任何字符串都是基 ...
- php的哈希函数
哈希函数: echo password_hash("rasmuslerdorf", PASSWORD_DEFAULT)."\n"; 验证函数: boolean ...
- 经常使用哈希函数的比較及其C语言实现
基本概念 所谓完美哈希函数.就是指没有冲突的哈希函数.即对随意的 key1 != key2 有h(key1) != h(key2). 设定义域为X,值域为Y, n=|X|,m=|Y|.那么肯定有m&g ...
- djb2:一个产生简单的随机分布的哈希函数
目录 LCG算法 示例代码 djb2 示例代码 为什么选择参数33和 33 was chosen because: 5381 was chosen because 哈希选择参考 LCG算法 djb2与 ...
- lintcode-128-哈希函数
128-哈希函数 在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数.一个好的哈希函数可以尽可能少地产生冲突.一种广泛使用的哈希函数算法是使用数值33,假 ...
- Java集合(八)哈希表及哈希函数的实现方式
Java集合(八)哈希表及哈希函数的实现方式 一.哈希表 非哈希表的特点:关键字在表中的位置和它之间不存在一个确定的关系,查找的过程为给定值一次和各个关键字进行比较,查找的效率取决于和给定值进行比较的 ...
随机推荐
- FastJson解析对象及对象数组--项目经验
第一次使用json,解析工具为FastJson,使用语言为java 常见的json解析实例,以map为例: Map<String,String> map=new HashMap<St ...
- Realm Configuration HOW-TO--官方
来源:https://secure.gettinglegaldone.com/docs/realm-howto.html Quick Start This document describes how ...
- iOS-CALayer中position与anchorPoint详解
iOS-CALayer中position与anchorPoint详解 属性介绍 CALayer通过四个属性来确定大小和位置, 分别为:frame.bounds.position.anchorPoint ...
- 模拟dispatch_once
dispatch_once dispatch_once可以保证一段代码只被执行一次,因此出现之后使用最多的场景就是实现单例.本文来模拟实现dispatch_once的功能. 模拟dispatch_ ...
- 'Service' object has no attribute 'process'
在使用selenium+phantomjs时,运行总是出现错误信息: 'Service' object has no attribute 'process' 出现该错误的原因是未能找到可执行程序&qu ...
- 谁是谁的first-child
看过CSS伪类选择器之后,心想也就如此嘛,:first-child选择元素的第一个子元素,有什么难的,可一到实践中,还是到处碰壁啊. <body> <ul class="f ...
- 用GitHub Pages免费空间搭建Blog
前言 其实之前就知道可以用GitHub Pages搭建静态博客,不过之前一直忙着爬手册撸代码==,昨天终于把前端各种手册里的入门教程撸的差不多了(CSS布局撸的我要吐了好嘛),于是把代码什么的放一 ...
- 如何在安卓/data(而不是/data/data)目录下进行文件的读写操作
分析:Android默认是无法直接操作/data目录的,只能读写程序自己的私有目录,也就是/data/data/package name/下,默认只能操作这个目录下的文件,也就是我们想直接读写/dat ...
- Andoird - SQLite 数据库 基础教程
链接来源 http://www.tutorialspoint.com/android/android_sqlite_database.htm SQLite是一个开源的SQL数据库,这个数据库把数据存储 ...
- C# DataTable去除重复,极其简便、简单
其中sourceDT是获取到的一个DataTable类型的集合对象 去重复使用方式: 实例化一个DataView对象 假设为dv,直接dv.ToTable()即可,ToTable中可为(true,&q ...