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 ;
}

最新文章

  1. MySQL Binlog Mixed模式记录成Row格式
  2. c#实现远程操作svn
  3. Ubuntu-tomcat7目录
  4. 开启mysql慢查询
  5. Postfix Completion 的使用
  6. 【转】iOS25彩票 幸运转盘
  7. xml的加密和解密
  8. js---疑点代码段解析
  9. 聊聊RocksDB Compact
  10. JSP标签JSTL(4)--URL
  11. 衡量android开发者水平的面试问题-android学习之旅(91)
  12. Android状态栏着色
  13. 记录es在虚拟机的开启步骤
  14. gulp使用详解
  15. 画图必备numpy函数
  16. SpringBoot在IDEA中实现热部署
  17. BFS广度优先搜索 poj1915
  18. C程序设计语言习题(1-12)
  19. 由浅入深之Tensorflow(3)----数据读取之TFRecords
  20. NOIP赛前集训营-提高组(第一场)#B 数数字

热门文章

  1. 关于使用mybatis插件自动生成代码
  2. Android学习【Android内核编译流程和错误笔记】
  3. MFC原创:三层架构01(人事管理系统)DAL
  4. 通过数组初始化链表的两种方法:指向指针的引用node *&amp;tail和指向指针的指针(二维指针)node **tail
  5. linux下查阅文件内容cat,more,less,tail
  6. javascript 实现jsonp
  7. JAVA-FileInputStream之read方法
  8. C# List&lt;T&gt;转为 DataTable
  9. Arcgis Engine - 鹰眼(减少闪烁)
  10. 使用DataSet数据集删除记录