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. uvalive 4851 Restaurant(扫描法)

    题意:有一个M*M的网格,坐标[0...M-1,0...M-1] 网格里面有两个y坐标同样的宾馆A和B.以及n个餐厅,宾馆AB里面各有一个餐厅,编号1,2,其它餐厅编号3-n.如今你打算新开一家餐厅. ...

  2. JNI(5)The Invocation API

    调用API允许软件提供商加载Java VM 到任意的本地应用中.供应商可以提供支持Java的应用程序而无需链接Java VM的代码. 概述 下面代码展示了通过调用API如何使用函数.这个例子中C++代 ...

  3. Java 之 StringTokenizer

    class StringTokenizer Object to: break a string into tokens. Contructs StringTokenizer(String str)   ...

  4. MySQL与mabits大小比较、日期比较示例

    首先,使用mysql查询从今往后的60天数据 SELECT count(*), b1.record_date FROM nk_house_use_record AS b1, ( SELECT a.th ...

  5. Java设计模式---工厂模式(简单工厂、工厂方法、抽象工厂)

    工厂模式:主要用来实例化有共同接口的类,工厂模式可以动态决定应该实例化那一个类.工厂模式的形态工厂模式主要用一下几种形态:1:简单工厂(Simple Factory).2:工厂方法(Factory M ...

  6. sys--system-sysdba-sysoper用户区别

    当Oracle 数据库安装完毕后,系统会自动创建sys和system这两个帐户.1.sys :缺省密码为CHANGE_ON_INSTALL ,且被授予DBA角色system :缺省密码为MANAGER ...

  7. python自动化执行脚本

    ---恢复内容开始--- 1 (1)首先在你的.py文件上加上一行代码注释: #!/usr/local/bin/python2.7 (2)终端下执行: crontab -e 进入后,输入i 进入可编辑 ...

  8. Android-操作栏之选项菜单

    回答第一个问题:什么是选项菜单?答:选项菜单就是可以显示在操作栏上的菜单. 菜单的视图需要建立在res/menu下. 其中,showAsAction属性用于指定菜单选项是显示在操作栏还是隐藏到溢出菜单 ...

  9. Oracle怎样方便地查看报警日志错误

    由于报警日志文件很大,而每天都应该查看报警日志(查看有无“ORA-”,Error”,“Failed”等出错信息),故想找到一种比较便捷的方法,查看当天报警日志都有哪些错误. 在网上查了几天的资料,尝试 ...

  10. 关于PagedDataSource,非常好用的一个分页属性!

    Asp.net提供了三个功能强大的列表控件:DataGrid.DataList和Repeater控件,但其中只有DataGrid控件提供分页功能.相对DataGrid,DataList和Repeate ...