2 shunfurh shunfurh 于 2017.09.03 20:47 提问

Gap

Let's play a card game called Gap.
You have 28 cards labeled with two-digit numbers. The first digit (from 1 to 4) represents the suit of the card, and the second digit (from 1 to 7) 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 1, and put them in the open space at the left end of the rows: "11" to the top row, "21" to the next, and so on.

Now you have 28 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 "42" is "43", and "27" has no successor.

In the above layout, you can move "43" to the gap at the right of "42", or "36" to the gap at the right of "35". If you move "43", a new gap is generated to the right of "16". You cannot move any card to the right of a card of value 7, 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 1. If there is no move sequence from the initial layout to the goal layout, produce "-1".

Sample Input

4

12 13 14 15 16 17 21
22 23 24 25 26 27 31
32 33 34 35 36 37 41
42 43 44 45 46 47 11

26 31 13 44 21 24 42
17 45 23 25 41 36 11
46 34 14 12 37 32 47
16 43 27 35 22 33 15

17 12 16 13 15 14 11
27 22 26 23 25 24 21
37 32 36 33 35 34 31
47 42 46 43 45 44 41

27 14 22 35 32 46 33
13 17 36 24 44 21 15
43 16 45 47 23 11 26
25 37 41 34 42 12 31

Sample Output

0
33
60
-1

1个回答

fight_in_dl
fight_in_dl   Ds   Rxr 2017.09.05 05:15
已采纳
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
Bluetooth GAP介绍
GAP Profile: Generic Access Profile 该Profile保证不同的Bluetooth产品可以互相发现对方并建立连接 GAP定义了蓝牙设备如何发现和建立与其他设备的安全/不安全连接 它处理一些一般模式的业务(如询问、命名和搜索)和一些安全性问题(如担保) 同时还处理一些有关连接的业务(如链路建立、信道和连接建立) GAP规定的是一些一般性
[BLE--GAP]GAP Service及其使用
简述GAP本身是一个Profile,并不是Protocol,其作用是定义了如何来使用蓝牙的Host的各层的Protocol。GAP中有一个GAP Service,是用来表明设备的基本信息的,只要作为GATT Server,就需要实现这个GAP Service。GAP Service先看一张图,对GAP Service有个整体了解: 从此图可看到,BR/EDR可以选择实现GAP Service的
BLE 协议栈之GAP与GATT层小小一瞥
Generic Access Profile (GAP) GAP 一共有四种角色: Broadcaster ,Observer,Peripheral ,Central   Connection Interval  (主从设备连接间隔)    最小7.5ms 最大4.0s Slave Latency  (从设备延迟): 忽略主设备的应答 继续处于睡眠状态 Supervision Time
Dataguard的Log Gap检测和处理原理
author:skate time:2011/09/05     Dataguard的Log Gap检测和处理原理     提到dataguard的Gap处理机制,大多数人能说出的措施便是设置fal_server和fal_client,从9i开始, Oracle提供
Maximum Gap (Java)
Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements
GAP软件的使用(20150514、20151002、20151004、20151012)
20151118:删除11种4阶环的内容。 20151026添加: gap> LoadPackage("guava"); true gap> q:=5;F:=GF(q);R:=PolynomialRing(F,2);; 5 GF(5) gap> vars:=IndeterminatesOfPolynomialRing(R);x:=vars[1];y:=vars[2]; [ x_
MYSQL INNODB中GAP LOCK引起死锁的问题
先了解一下什么是GAP LOCK 在INNODB中,record-level lock大致有三种:Record, Gap, and Next-KeyLocks。简单的说,RECORDLOCK就是锁住某一行记录;而GAPLOCK会锁住某一段范围中的记录;NEXT-KEYLOCK则是前两者加起来的效果。 下面是MYSQL官方文档中相关内容的链接 http://dev.mysql.com/doc/
LTE Measurement Gap
Before the handover, UE normally measure the cell power (signal quality) of the target cell and report it to the network, so that network can make a decision whether to allow UE to handover to the t
Gap Locks 区间锁
Gap Locks 区间锁 1. 区间锁不能用于语句锁定记录使用一个唯一索引来搜索一个唯一的记录 2.READ COMMITTED 没有区间锁 区间锁是一个锁在一个在index记录间的区间,或者一个lock 在gap 在第一个前或者最后一个index recoed 之后, 比如,SELECT c1 FOR UPDATE FROM t WHERE c1 BETWEEN 10 and 2
oracle物理dg gap解决
一般 redo传输服务(redo transport services)会自动探测并重发日志文件到备库来解决gap,但也有自动解决不了的时候,此时就需要人工介入,以下是操作步骤。 1.备库查看gap select * from v$archive_gap; 2.主库查看gap对应的日志文件,sql中的40和49是上一步查出的日志sequence号