【数据结构•hash表】重排九宫
Time Limit:10000MS
Total Submit:47 Accepted:15
Description
我先来说几句:
====================================================================================================
下面大家即将见到的是一道经典的广度搜索(bfs)题,bfs算法的运行耗时主要集中在状态扩展和状态判重上,大部分情况下,状态判重的耗时是远大于状态扩展的(假设可能扩展的状态数量为n,则其运算量分别为:o(n)和o(n^2)),如果在bfs算法中使用hash表去判重,则可以极大的提高运行效率。因此,bfs+hash,会是解决相当多搜索题的杀手锏!
就下面例题而言,注意到每种状态构成了一个排列,我们可以给它分配一个编号,总共的状态数不会超过9!=362880种。(关于排列直接换算分配hash地址,可以见P1511理论基础知识,因此题状态数比较少,你可以使用简化的散列(hash)函数解决此题)。
====================================================================================================
问题来了,重排九宫:
将数字1~8按照人依次序排在3*3的方格阵列中,留下一个空位供数字方块移动,游戏的最终目标是通过合法移动将数字按行排好序。
输入举例:
目标:
输出达到目标状态的最少移动次数。
Input
3*3的输入方阵,格式见样例。
Output
一个整数,达到目标状态的最少移动次数。
Sample Input
1 2 3 4 0 6 7 5 8
Sample Output
2
Hint
本题测试数据贡献者:肖遥
Source
哈希表+广搜
哈希函数:先把二维转为一维,在用“康托展开式”,把一维转换成一个数。
康托展开式:一个排列在全排列中的位置。具体见http://baike.baidu.com/view/437641.htm
var
procedure hash;
var
begin
end;
procedure init;
var
begin
end;
procedure change(i:longint);
begin
end;
procedure bfs;
var
begin
end;
begin
end.