Partychen invents a new game named “Eleven Puzzle” .Just like the classic game “Eight Puzzle”,but there some difference between them:The shape of the board is different and there are two empty tiles. 

The tile in black means it’s empty

Each step you can move only one tile. 
Here comes the problem.How many steps at least it required to done the game. 

 

Input

The first line of input contains one integer specifying the number of test cases to follow. 
Every case contains five lines to describe the initial status of the board. 0 means empty.

It’s confirmed that the board is legal.

 

Output

Output one line for each testcase.Contain an integer denotes the minimum step(s) it need to complete the game.Or “No solution!” if it’s impossible to complete the game within 20 steps.
 

Sample Input

3
2
1 0 3
4 5 6 7 8
9 0 11
10
0
1 2 3
4 5 6 7 8
9 10 11
0
0
11 10 9
8 7 6 5 4
3 2 1
0
 

Sample Output

2
0
No solution!
 
题意:给出上图的11数码,有2个空格,每次可以移一步,问能否在20步以内达到目标状态,如果能输出步数,否则输出No solution!
 
解析:题目给了20步的限制,而且前后搜效果是一样的,所以很容易想到双向bfs,但是状态怎么保存呢,有11个数外加2个空格,可以考虑用康拓展开,但不能用数组保存值,因为值太大了,所以可以考虑用map,我用的方法是哈希,给每个数乘上一个权值,然后加起来哈希成一个值,用数组模拟链表保存状态,但可能出现两种不同的状态哈希成同一个值,所以在相同的情况下还要整个进行对比。
 
源代码
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iterator>
#include<stack>
using namespace std;
const int INF=1e9+;
const double eps=1e-;
const int mod=;
const int maxn=;
int f[],r[]; //双向bfs的两个队列的队首队尾指针
int dx[]={-,,,},dy[]={,-,,}; //方向数组
bool in(int x,int y){ return x>=&&x<&&y>=&&y<; } //是否越界
struct node
{
int px[],py[]; //保存2个空格的位置和整个数组
int A[][];
}nod[][maxn]; //0是前面搜,1是后面搜
int B[][]={ //最终状态
-,-,,-,-,
-,,,,-,
,,,,,
-,,,,-,
-,-,,-,-
};
bool Same(int A[][]) //判断该状态是否与最终的状态相等
{
for(int i=;i<;i++)
for(int j=;j<;j++)
if(A[i][j]!=B[i][j]) return false;
return true;
}
int F[]; //保存2^i
void GetF()
{
F[]=;
for(int i=;i<;i++) F[i]=F[i-]*;
}
int Get(int A[][]) //得到哈希值
{
int ret=,k=;
for(int i=;i<;i++)
for(int j=;j<;j++) if(A[i][j]>) ret+=F[k++]*A[i][j]; //乘上一个权值
return ret;
}
struct Hash
{
int v,next,nid,k; //是哈希值,next指向下一个节点,nid和k分别保存是下标和0或1
}ha[mod+maxn];
int hash_id;
bool check(int a,int k1,int b,int k2) //判断是否完全相等
{
for(int i=;i<;i++)
for(int j=;j<;j++)
if(nod[k1][a].A[i][j]!=nod[k2][b].A[i][j]) return false;
return true;
}
int Insert_Hash(int v,int nid,int k) //插入
{
int a=v%mod;
int p=ha[a].next;
while(p!=-)
{
if(ha[p].v==v&&check(ha[p].nid,ha[p].k,nid,k)) return ha[p].k;//有相同的状态
p=ha[p].next;
}
p=++hash_id; //没有则增加新节点,前插法
ha[p].v=v; ha[p].nid=nid; ha[p].k=k;
ha[p].next=ha[a].next; ha[a].next=p;
return -; //-1代表插入了新节点
}
bool AddNode(node& t,int i,int j,int k)
{
int x=t.px[i],y=t.py[i];
int nx=x+dx[j],ny=y+dy[j];
if(!in(nx,ny)||t.A[nx][ny]<=) return false;
node& tt=nod[k][r[k]];
tt=t;
swap(tt.A[x][y],tt.A[nx][ny]); //交换
tt.px[i]=nx; tt.py[i]=ny;
int a=Insert_Hash(Get(tt.A),r[k],k);
if(a==-){ r[k]++; return false; } //队尾指加1
else if(a==k) return false; //自己原来访问过的状态
else return true; //相遇了,找到了解
}
void Print(node& t)
{
for(int i=;i<;i++)
{
for(int j=;j<;j++) printf("%d ",t.A[i][j]);
puts("");
}
puts("=========");
getchar();
}
bool bfs(int k)
{
int& be=f[k];
int en=r[k];
while(be<en)
{
node& t=nod[k][be++];
//Print(t);
for(int i=;i<;i++)
for(int j=;j<;j++) if(AddNode(t,i,j,k)) return true;
}
return false;
}
int solve()
{
if(Same(nod[][].A)) return ;
for(int i=;i<;i++)
for(int j=;j<;j++) nod[][].A[i][j]=B[i][j]; //最终状态
nod[][].px[]=; nod[][].py[]=;
nod[][].px[]=; nod[][].py[]=;
int step=;
f[]=f[]=,r[]=r[]=;
for(int i=;i<mod;i++) ha[i].next=-;
hash_id=mod-;
while(f[]<r[]||f[]<r[]) //双向搜
{
step++;
if(bfs()) return step;
step++;
if(bfs()) return step;
if(step>=) return -;
}
return -;
}
int main()
{
int T;
GetF();
scanf("%d",&T);
while(T--)
{
int k=;
for(int i=;i<;i++)
for(int j=;j<;j++)//得到初始状态
{
if(B[i][j]==-) { nod[][].A[i][j]=-; continue; }
scanf("%d",&nod[][].A[i][j]);
if(nod[][].A[i][j]==)
{ nod[][].px[k]=i; nod[][].py[k++]=j; }
}
int ans=solve();
if(ans==-) printf("No solution!\n");
else printf("%d\n",ans);
}
return ;
}

hdu3095-Eleven puzzle(双向搜索+哈希)的更多相关文章

  1. Eleven puzzle_hdu_3095(双向广搜).java

    Eleven puzzle Time Limit: 20000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  2. Eight(bfs+全排列的哈希函数)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22207   Accepted: 9846   Special Judge ...

  3. poj1200Crazy Search (哈希)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Crazy Search Time Limit: 1000MS   Memory ...

  4. POJ 1200:Crazy Search(哈希)

    Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32483   Accepted: 8947 Des ...

  5. POJ-3131-Cubic Eight-Puzzle(双向BFS+哈希)

    Description Let's play a puzzle using eight cubes placed on a 3 × 3 board leaving one empty square. ...

  6. Crazy Search POJ - 1200 (字符串哈希hash)

    Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could ...

  7. [PHP内核探索]PHP中的哈希表

    在PHP内核中,其中一个很重要的数据结构就是HashTable.我们常用的数组,在内核中就是用HashTable来实现.那么,PHP的HashTable是怎么实现的呢?最近在看HashTable的数据 ...

  8. java单向加密算法小结(2)--MD5哈希算法

    上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇要说的MD5,其实也不算是加密算法,而是一种哈希算法,即将目标文本转化为固定长度,不可逆的字符 ...

  9. Java 哈希表运用-LeetCode 1 Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

随机推荐

  1. 用Update Select批量更新某一字段的值[可以跨库]

    SQL:UPDATE test1  SET name = (SELECT y.name  FROM  DB2.dbo.test2 y WHERE test1.id = y.id)

  2. c语言typedef运用与函数指针

    #include <stdio.h> #include <stdlib.h> #define PINT int * typedef short* PSHORT; //typed ...

  3. Android——自定义Actionbar左侧覆盖不全的解决方案

    今天遇到一个很蛋疼的问题,就是在自定义Actionbar的时候,setCustomView中,自定义的view怎么也覆盖不了整个视图,左侧一直留有一个空白,看下图: 所写的部分代码如下: protec ...

  4. 第32讲 UI组件之 时间日期控件DatePicker和TimePicker

    第32讲 UI组件之 时间日期控件DatePicker和TimePicker 在Android中,时间日期控件相对来说还是比较丰富的.其中, DatePicker用来实现日期输入设置,    Time ...

  5. CSS3 div水平、垂直居中,IE9以上、Firefox、Chrome均正常

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. mysql下用户和密码生成管理

    应用上线,涉及到用户名和密码管理,随着上线应用的增加,用户名和密码的管理设置成为一个问题.还要对用户赋权,于是想着写一个脚本来管理,看到同事写的一个脚本,满足需求.思路大致是字母替换为数字,账号根据库 ...

  7. Hibernate自增列保存失败的问题

    author: hiu 更正说明:今天(2014-07-07)才发现的问题,我把@Id设置在了实体类中的id中,@Id是主键,应该设置在实体类的keyjobno中,之前发的文章可能误导了大家,如今更正 ...

  8. C# 初识Ref和Out

    首先:两者都是按地址传递的,使用后都将改变原来参数的数值. 其次:ref可以把参数的数值传递进函数,但是out是要把参数清空,就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空,所 ...

  9. jQuery制作焦点图(轮播图)

    焦点图(轮播图) 案例 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  10. (转)@@trancount解析

    在处理事务的时候,一般都用RollBack Transaction来回滚,但是如果在嵌套事务中这样使用的话,就会出现错误. 在SqlServer里,嵌套事务的层次是由@@TranCount全局变量反映 ...