HDU 1067 Gap
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.
让我们玩个叫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的更多相关文章
- hdu.1067.Gap(bfs+hash)
Gap Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- HDU - 1067 Gap (bfs + hash) [kuangbin带你飞]专题二
题意: 起初定28张卡牌的排列,把其中11, 21, 31, 41移动到第一列,然后就出现四个空白,每个空白可以用它的前面一个数的下一个数填充,例如43后面的空格可以用44填充,但是47后面即 ...
- 【hdu 1067】Gap
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission( ...
- [hdu 1067]bfs+hash
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1067 queue里面果然不能放vector,还是自己写的struct比较省内存…… #include& ...
- BFS+Hash(储存,判重) HDOJ 1067 Gap
题目传送门 题意:一个图按照变成指定的图,问最少操作步数 分析:状态转移简单,主要是在图的存储以及判重问题,原来队列里装二维数组内存也可以,判重用神奇的hash技术 #include <bits ...
- KUANGBIN带你飞
KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题 //201 ...
- [kuangbin带你飞]专题1-23题目清单总结
[kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...
- ACM--[kuangbin带你飞]--专题1-23
专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find T ...
- HDU 5643 King's Game 打表
King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...
随机推荐
- Publisher/Subscriber
public interface IPublisher { void Publish<T>(T data); void Subscribe<T>(object subscrib ...
- socket通信的遇到的问题1
/*使用select对fd可读写,格式*/ while(ctrl){ //// FD_ZERO(&readSocketSet); FD_SET(readSocketFd,&readSo ...
- DAX/PowerBI系列 - 关于时间系列 - 时间相关数值比较 - 用非自带函数
DAX/PowerBI系列 - 关于时间系列 - 时间相关数值比较 - 用非自带函数 文末有彩蛋,解决蛋疼问题 难度: ★★☆☆☆(2星) 适用范围: ★★★☆☆(3星) 概况: 基于时间的汇总可能是 ...
- Extjs6 grid 导出excel功能类,支持renderer
/* grid 导出excel扩展(纯客户端,提交到后台再导的可以自己改改代码也在) 参考自 https://blog.csdn.net/tianxiaode/article/details/4596 ...
- app ios info权限配置:
info权限配置: Privacy - Bluetooth Peripheral Usage Description --> App需要您的同意,才能访问蓝牙 Privacy - Calenda ...
- 1#认识Java
Java是一种面对对象的编程语言. Java共分为三个体系:JavaEE.JavaSE.JavaMS Java SE 1: Java Platform Standard Edition,Java平台标 ...
- flask框架中,利用数据库增删查改
# 配置数据库app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:mysql@127.0.0.1:3306/booktest" ...
- Linux 命令整理-ps
ps 命令 ps -ef | grep tomcat ps -ef :以长格式(全格式)显示所有进程:“|” :是管道grep :检索tomcat :与字符tomcat有关的进程 ps[选项]-e:显 ...
- ORACLE中 大量数据插入表 SQL
declare g_commit_count number; cursor cu1 is select gl_flexfields_pkg.get_description_sql(gcc.chart_ ...
- pymysql.err.InternalError: Packet sequence number wrong - got 45 expected 0
原因: 使用了多线程,多线程共享了同一个数据库连接,但每个execute前没有加上互斥锁 方法: 方法一:每个execute前加上互斥锁 lock.acquire() cursor.e ...