Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1123 Accepted Submission(s): 595

Problem Description

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

【题目链接】:http://acm.hdu.edu.cn/showproblem.php?pid=1067

【题解】



搜索题。

这题的判重方法和八数码类似;

把整张4*8的图降成一维的图;

判重的时候转换成一个字符串判重就好;

在bfs的队列里面

记录

{

———-4*7个数码在哪一个位置;

———-4个空格的位置;

———-当前的步数;

———-这张图用一维字符串的表示;

}

在扩展的时候

枚举4个空格.

看看4个空格左边是什么;

如果是0或者尾数为7就跳过;

否则找到比它大1的数码(我们有记录)的位置;

(二维的坐标和一维的坐标可以通过(x-1)*8+y来转换)

然后调换字符串中两个数目;

更改那个被调换的数目的位置(不要忘了!)

更改空格的位置;

修改当前步数;

入队。

如此循环一下就OK了.

判重用map



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const int goal[32] = {11,12,13,14,15,16,17,0,21,22,23,24,25,26,27,0,31,32,33,34,35,36,37,0,41,42,43,44,45,46,47,0};
const double pi = acos(-1.0);
const int MAXN = 110; struct abc
{
pii po[50],kong[5];
int dis;
string s;
}; char chushi[5][10];
string sgoal,ts;
map <string,int> dic;
queue <abc> dl; bool bfs()
{
dic.clear();
while (!dl.empty()) dl.pop();
abc pre;
pre.dis = 0;pre.s = " ";
int num = 0;
rep1(i,1,4)
{
rep1(j,1,8)
{
pre.s+=chushi[i][j];
if (chushi[i][j]==0)
pre.kong[++num] = {i,j};
else
pre.po[int(chushi[i][j])] = {i,j};
}
}
dic[pre.s] = 1;
if (dic[sgoal])
{
puts("0");
return true;
}
dl.push(pre);
while (!dl.empty())
{
abc temp = dl.front();dl.pop();
for (int i = 1;i <= 4;i++)
{
abc t = temp;
int tx = t.kong[i].fi,ty = t.kong[i].se;
int sz = (tx-1)*8+ty;
int judge = t.s[sz-1];
if (judge==0 || judge%10==7) continue;
int tx1 = t.po[judge+1].fi,ty1 = t.po[judge+1].se;
int sz1 = (tx1-1)*8+ty1;
swap(t.s[sz],t.s[sz1]);
if (!dic[t.s])
{
dic[t.s] = 1;
t.kong[i] = {tx1,ty1};
t.po[judge+1] = {tx,ty};
t.dis++;
if (dic[sgoal])
{
printf("%d\n",t.dis);
return true;
}
dl.push(t);
}
}
}
return false;
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
sgoal = " ";
for (int i = 0;i <= 31;i++)
sgoal+=goal[i];
int T;
rei(T);
while (T--)
{
memset(chushi,0,sizeof chushi);
rep1(i,1,4)
rep1(j,2,8)
{
int x;
rei(x);
chushi[i][j] = x;
if (x%10==1)
swap(chushi[x/10][1],chushi[i][j]);
}
if (!bfs())
puts("-1");
}
return 0;
}

【hdu 1067】Gap的更多相关文章

  1. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  2. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  3. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  4. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  5. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  6. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  7. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

  8. 【HDU 3068】 最长回文

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...

  9. 【HDU 4699】 Editor

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...

随机推荐

  1. IDFA和IMEI

    这里有一些解释: https://www.zhihu.com/question/38856446

  2. [NPM] Test npm packages locally in another project using npm link

    We will import our newly published package into a new project locally to make sure everything is wor ...

  3. 利用 istio 来对运行在 Kubernetes 上的微服务进行管理

    尝试在一个准生产环境下,利用 istio 来对运行在 Kubernetes 上的微服务进行管理. 这一篇是第一篇,将一些主要的坑和环境准备工作. 内容较多,因此无法写成手把手教程,希望读者有一定 Ku ...

  4. python序列中是否包含某个元素

    http://outofmemory.cn/code-snippet/9098/python-list-contains-with-in-not-in theList = ['a','b','c'] ...

  5. C# for 和 foreach的执行效率

    for和foreach哪个执行效率快,相信很多人都会说当然是foreach快啊,在我实验之前我也是这么认为的,直到今天.费话不多说,下面是测试的结果,区分Debug和Release,数据采用int[] ...

  6. 深拷贝&浅拷贝

    1.区别 浅拷贝:只拷贝了基本数据类型,引用数据类型只复制了引用,没有复制实体. 深拷贝:拷贝所有的层级属性 2.浅拷贝 (1) 直接赋值 拷贝之后,所有层级属性仍然公用了地址,会被影响 var a ...

  7. 洛谷 P1206 [USACO1.2]回文平方数 Palindromic Squares

    P1206 [USACO1.2]回文平方数 Palindromic Squares 题目描述 回文数是指从左向右念和从右向左念都一样的数.如12321就是一个典型的回文数. 给定一个进制B(2< ...

  8. [转]C#连接操作mysql实例

    本文转自:http://hi.baidu.com/zhqngweng/item/c4d2520cb7216877bfe97edf 第三方组件:Mysql.Data.dll说明:去官方网站下载Mysql ...

  9. Loadrunner--运行场景报Socket descriptor not found错误

    今天早上在使用LoadRunner时,报了如下的错误,开始并未看错误以为是录制问题引起,就重新录制了一遍,简单施压看看错误是否还有,结果错误仍然有,如下所示: Error: Socket descri ...

  10. 关于LWIP断开网线后重连问题(热插拔问题)

    近期在弄STM32+LWIP协议.在网络拔掉网线情况下.无法又一次连接. 网上找了好多方法都没有实现,着实郁闷! 后来无意间看到了临时解决这一问题的方法.尽管不是那么完美,但最算能解决这个问题.分享给 ...