HDU 1067 Gap

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

 

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.
让我们玩个叫Gap的游戏。
你有28个标有两位数的卡片。第一个数( ~ )表示卡片种类,第二个数(~)表示卡片的值。
首先,在桌上摆放四行七列的卡片,每行的最左边留下一张卡片的空位。初始布局如下:

CN

  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.

接着,把所有值为1的卡片放到左侧的空位: ""在最上面,""在下一行,以此类推。
现在你有28张卡片与4个空位,共四行八列,并需要从这个布局开始移动卡片。

CN

  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.

每次移动,你需要选择一个空位,放上空格左边的后续卡。后继卡是同一套牌中的下一张。例如""的后继卡为"",""没有后继卡。

上述布局中,你可以移动""至""右边的空位,或者移动""至""右边。若你移动"",则有一个新的空位出现在""右边。不能移动任何卡片到值为7的卡片右边,也不能移动到空位的右边。

游戏的目标是使用明智移动得出如下四个上升系列。

CN

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

你的目标是找出达成目标布局的最小移动数。

CN

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.

输入起始行的数字表示初始布局的数量。
每个布局五行 - 一行空,四行初始布局。每行有七个对应卡片的两位数。

CN

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".

对于每个初始布局,输出一行达成目标布局的最小移动数。注意,这个数不包括值为1卡片的四次初始化移动。如果不存在从初始至目标布局的移动序列,则输出"-1"。

CN

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

题解

  BFS + 哈希。

  之前作死想用map来个暴力判重先试试水,结果不但速度慢,内存直接炸了。

  然后灵(脑)一光(子)一闪(抽),一个哈希数组坐标,一个哈希数字值,目测出问题的概率不大,A了之后就懒得改了。

代码 C++

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#define MX 32
#define INF 0x7F7F7F7F
#define LL long long
#define mod 1000007
struct Ary {
int a[MX], s;
}now, nxt;
int hash[mod], opt, ed[MX] = { , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , };
int getHash_i() {
LL i, j = , rtn;
for (i = rtn = ; i < MX; ++i) {
rtn ^= (j*nxt.a[i]) ^ ed[i];
j = (j << ) % mod;
}
return rtn%mod;
}
int getHash_v() {
LL i, j = , rtn;
for (i = rtn = ; i < MX; ++i) {
rtn ^= j*(nxt.a[i] ^ ed[i]);
j = (j << ) % mod;
}
return rtn%mod;
}
bool setHash() {
for (int i = getHash_i(), v = getHash_v(); i < mod; ++i) {
if (~hash[i]) {
if (hash[i] == v) return ;
}
else { hash[i] = v; return ; }
}
return ;
}
void BFS() {
int idx[], zro[], iz, i, j;
hash[getHash_i()] = getHash_v();
std::queue<Ary> q; q.push(nxt);
while (!q.empty()) {
now = q.front(); q.pop();
if (now.s >= opt) continue;
if (memcmp(now.a, ed, sizeof ed) == ) {
opt = std::min(opt, now.s);
continue;
}
++now.s;
for (i = iz = ; i < MX; ++i) {
(now.a[i] ? idx[now.a[i]] : zro[iz++]) = i;
}
for (i = ; i < ; ++i) {
j = zro[i];
if (now.a[j - ] % == || !now.a[j - ]) continue;
nxt = now;
nxt.a[j] = now.a[j - ] + ;
nxt.a[idx[nxt.a[j]]] = ;
if (setHash()) q.push(nxt);
}
}
}
int main() {
int t, i, j;
scanf("%d", &t);
while (t--) {
memset(hash, -, sizeof hash); opt = INF;
for (i = nxt.s = , j = ; i < MX; ++i) {
if (i % ) {
scanf("%d", nxt.a + i);
if (nxt.a[i] % == ) nxt.a[i] = ;
continue;
}
nxt.a[i] = (j += );
}
BFS();
printf("%d\n", opt == INF ? - : opt);
}
return ;
}

HDU 1067 Gap的更多相关文章

  1. hdu.1067.Gap(bfs+hash)

    Gap Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  2. HDU - 1067 Gap (bfs + hash) [kuangbin带你飞]专题二

    题意:    起初定28张卡牌的排列,把其中11,  21, 31, 41移动到第一列,然后就出现四个空白,每个空白可以用它的前面一个数的下一个数填充,例如43后面的空格可以用44填充,但是47后面即 ...

  3. 【hdu 1067】Gap

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

  4. [hdu 1067]bfs+hash

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1067 queue里面果然不能放vector,还是自己写的struct比较省内存…… #include& ...

  5. BFS+Hash(储存,判重) HDOJ 1067 Gap

    题目传送门 题意:一个图按照变成指定的图,问最少操作步数 分析:状态转移简单,主要是在图的存储以及判重问题,原来队列里装二维数组内存也可以,判重用神奇的hash技术 #include <bits ...

  6. KUANGBIN带你飞

    KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题    //201 ...

  7. [kuangbin带你飞]专题1-23题目清单总结

    [kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...

  8. ACM--[kuangbin带你飞]--专题1-23

    专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find T ...

  9. HDU 5643 King's Game 打表

    King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...

随机推荐

  1. Linux基础命令2

    1.修改网络状态: 1).Cd  /etc/sysconfig/network-scripts/network-scripts 2).vi ifcfg-eth0   编辑  onboot=yes: 3 ...

  2. [js]练习绘制拓扑图

  3. extjs 跨域 ajax.request

    https://www.cnblogs.com/yuzhongwusan/p/3677955.html https://stackoverflow.com/questions/25727306/req ...

  4. 70个Python练手项目列表(都有完整教程)

    前言: 不管学习那门语言都希望能做出实际的东西来,这个实际的东西当然就是项目啦,不用多说大家都知道学编程语言一定要做项目才行. 这里整理了70个Python实战项目列表,都有完整且详细的教程,你可以从 ...

  5. leetcode148. Sort List

    和数组里面的归并排序相同,用两个指针分别对应low high,递归进行归并排序然后merge把两个链表合在一起 /** * Definition for singly-linked list. * s ...

  6. springmvc中的一些服务器报错

    这是springmvc的前端控制器的加载格式要求.服务器加载web.xml后,它会在web-inf/下找名为spring[servlet名/handler处理器名]-servlet.xml文件. 可以 ...

  7. UnzipUtil

    public class UnzipUtil { private static final Logger logger = LoggerFactory.getLogger(CopyFileUtil.c ...

  8. hibernate框架的简单入门

    1.什么是框架 框架是一个半成品,框架帮我们实现了一部分的功能. 2.使用框架的最大好处 使用框架的最大好处就是,少写一部分代码但仍能实现我们所需要实现的功能. 3.什么是hiberbnate框架 ( ...

  9. Windows 10 关闭Hyper-V

    以管理员身份运行命令提示符 关闭 bcdedit /set hypervisorlaunchtype off 启用 bcdedit / set hypervisorlaunchtype auto 禁用 ...

  10. uva 10123 - No Tipping dp 记忆化搜索

    这题的题意是 在双脚天平上有N块东西,依次从上面取走一些,最后使得这个天平保持平衡! 解题: 逆着来依次放入,如果可行那就可以,记得得有木板自身的重量. /********************** ...