Description

Let's play a card game called Gap.
You have cards labeled with two-digit numbers. The first digit (from to ) represents the suit of the card, and the second digit (from to ) represents the value of the card. First, you shu2e the cards and lay them face up on the table in four rows of seven cards, leaving a space of one card at the extreme left of each row. The following shows an example of initial layout.

Next, you remove all cards of value , and put them in the open space at the left end of the rows: "" to the top row, "" to the next, and so on. 

Now you have  cards and four spaces, called gaps, in four rows and eight columns. You start moving cards from this layout. 

At each move, you choose one of the four gaps and fill it with the successor of the left neighbor of the gap. The successor of a card is the next card in the same suit, when it exists. For instance the successor of "" is "", and "" has no successor. 

In the above layout, you can move "" to the gap at the right of "", or "" to the gap at the right of "". If you move "", a new gap is generated to the right of "". You cannot move any card to the right of a card of value , nor to the right of a gap. 

The goal of the game is, by choosing clever moves, to make four ascending sequences of the same suit, as follows. 

Your task is to find the minimum number of moves to reach the goal layout.

Input

The input starts with a line containing the number of initial layouts that follow. 

Each layout consists of five lines - a blank line and four lines which represent initial layouts of four rows. Each row has seven two-digit numbers which correspond to the cards. 

Output

For each initial layout, produce a line with the minimum number of moves to reach the goal layout. Note that this number should not include the initial four moves of the cards of value . If there is no move sequence from the initial layout to the goal layout, produce "-1".

Sample Input


Sample Output


-

Source

 
这题的关键在用hash来保存状态,其他的是bfs基础了。。。
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define M 1000007
#define ll long long
ll aimNum;
ll hash[M];
struct Node{
ll x[],y[];//存空格的横纵坐标
ll mp[][];//存整张地图
long long time;//存时间
}tmp;
ll flag;
ll aim[][]={
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,
};
ll base[]={}; bool inserNum(ll ans){//hash的插入,看看是否跟之前的状态相同,其实跟vis数组标记一个意思
ll val=ans%M;
while(hash[val]!=- && hash[val]!=ans){
val=(val+)%M;
}
if(hash[val]==-){
hash[val]=ans;
return true;//可以插入返回true
}
return false;//否则返回false
} bool work(Node cnt){
ll ans=;
for(ll i=;i<;i++){
for(ll j=;j<;j++){
ans=ans+cnt.mp[i][j]*base[i*+j];//ans为整张图的hash值
}
}
if(ans==aimNum){
flag=;
}
if(inserNum(ans))
return true;
return false;
} ll bfs(){
queue<Node>q;
q.push(tmp);
Node t1,t2;
while(!q.empty()){
t1=q.front();
q.pop(); for(ll k=;k<;k++){//4个空格依次遍历
t2=t1;
ll tx=t2.x[k];
ll ty=t2.y[k];
for(ll i=;i<;i++){//遍历整张图,寻找符合的数
for(ll j=;j<;j++){
if(t2.mp[i][j]==) continue;//如果要调换的还是空格,则不行
if(t2.mp[i][j]!=t2.mp[tx][ty-]+) continue;//需要填入的数为前一个+1 swap(t2.mp[i][j],t2.mp[tx][ty]);
if(work(t2)){//判断是否可以继续往下走
t2.time=t1.time+;
t2.x[k]=i;//将新的空格的横纵坐标保存下来
t2.y[k]=j;
q.push(t2);
if(flag)
return t2.time;
} }
}
}
}
return -;
}
int main()
{ for(ll i=;i<;i++){
base[i]=base[i-]*;
}
aimNum=(ll);//aimNum是通过事先计算得出的 int t;
scanf("%d",&t); while(t--){ memset(hash,-,sizeof(hash)); tmp.mp[][]=tmp.mp[][]=tmp.mp[][]=tmp.mp[][]=; int k=;
for(int i=;i<;i++){
for(int j=;j<;j++){
scanf("%I64d",&tmp.mp[i][j]); if(tmp.mp[i][j]==) {
swap(tmp.mp[i][j],tmp.mp[][]);
tmp.x[k]=i;
tmp.y[k++]=j;
}
if(tmp.mp[i][j]==) {
swap(tmp.mp[i][j],tmp.mp[][]);
tmp.x[k]=i;
tmp.y[k++]=j;
}
if(tmp.mp[i][j]==) {
swap(tmp.mp[i][j],tmp.mp[][]);
tmp.x[k]=i;
tmp.y[k++]=j;
}
if(tmp.mp[i][j]==) {
swap(tmp.mp[i][j],tmp.mp[][]);
tmp.x[k]=i;
tmp.y[k++]=j;
}
}
} tmp.time=;//时间初始化为0
flag=;
work(tmp);//先判断一遍是否可以不用调换就可以达到目的图
if(flag){
printf("0\n");
}else{
printf("%I64d\n",bfs());
}
}
return ;
}

poj 2046 Gap(bfs+hash)的更多相关文章

  1. 【BZOJ】1054: [HAOI2008]移动玩具(bfs+hash)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1054 一开始我还以为要双向广搜....但是很水的数据,不需要了. 直接bfs+hash判重即可. # ...

  2. UVA 10798 - Be wary of Roses (bfs+hash)

    10798 - Be wary of Roses You've always been proud of your prize rose garden. However, some jealous f ...

  3. poj 3414 Pots (bfs+线索)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10071   Accepted: 4237   Special J ...

  4. POJ 3414 Pots(BFS+回溯)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11705   Accepted: 4956   Special J ...

  5. POJ 3461 Oulipo(字符串hash)

    题目链接 字符串hash判断字符串是否相等. code #include<cstdio> #include<algorithm> #include<cstring> ...

  6. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

  7. POJ - 3308 Paratroopers(最大流)

    1.这道题学了个单词,product 还有 乘积 的意思.. 题意就是在一个 m*n的矩阵中,放入L个敌军的伞兵,而我军要在伞兵落地的瞬间将其消灭.现在我军用一种激光枪组建一个防御系统,这种枪可以安装 ...

  8. POJ 3279 Fliptile(翻格子)

    POJ 3279 Fliptile(翻格子) Time Limit: 2000MS    Memory Limit: 65536K Description - 题目描述 Farmer John kno ...

  9. POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配

    两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...

随机推荐

  1. hdu2209翻纸牌游戏

    翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  2. MongoDB每64位版本下载

    MongoDB每64位版本下载: http://dl.mongodb.org/dl/win32/x86_64 版权声明:本文博主原创文章,博客,未经同意不得转载.

  3. 忘记mysql 5.7的密码

    for windows: http://blog.chinaunix.net/uid-27570589-id-3511820.html 一.将net stop mysql; 二.在命令行中 C:\Us ...

  4. 网页JavaScript

    用法. JavaScript一般用于 head , body , </html> 之后. 格式<script language="javascript"> ...

  5. Set Windows IP by Batch

    netsh interface ip set address name="Local" static 192.168.1.55 255.255.255.0 192.168.1.1 ...

  6. QT-Demo-Colck-01

    QT += widgets QT += core HEADERS += \ mainwindow.h SOURCES += \ mainwindow.cpp \ main.cpp #ifndef MA ...

  7. (原)torch的训练过程

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6221622.html 参考网址: http://ju.outofmemory.cn/entry/284 ...

  8. python生成html文件浏览器中文显示乱码问题

    近来在网上采集数据,想把采集下来的数据整合成html的形式保存.以便其他的平台产品可以直接读取html显示或者根据html标签提取数据. def output_html(self): try: fou ...

  9. iOS学习之数据解析

    解析:按照约定好的格式提取数据的过程叫做解析; 后台开发人员按照约定好的格式存入数据,前端开发人员按照约定的格式读取数据; 主流的格式: XML / JSON 前端和后台都能识别的格式;  XML解析 ...

  10. iOS 第三方 需要 引用的库

    ================================================== AFNetWorking   是基于 nsurlconnection   所以不需要引入库 === ...