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
 
题意:给出表如第一个图,给出每个数,共有28个数,首先要将11,21,31,41放到前面去如第二个图,然后每次可以挑选一个空位把某个数放在此位置,但是
要保证此数恰好是空位左边的数的后一个数,比如空位左边的数是42,则只能把43移到空位去。如果是47的话是没有后一个数的。
 
解析:总共有32个数,用哈希保存状态。然后bfs即可。详见代码实现。
 
代码
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iterator>
#include<stack>
using namespace std;
const int INF=1e9+;
const int eps=0.0000001;
typedef __int64 LL;
const LL mod=;
int maze[][];
LL Hash[mod];
LL base[];
struct node
{
int px[],py[]; //保存四个空位
int S[][]; //整个图
int dist;
}Nod[];
int id;
queue<int> que; int goal[][]={ //最终状态
{ ,,,,,,, },
{ ,,,,,,, },
{ ,,,,,,, },
{ ,,,,,,, }
};
LL G;
void GetBase() //打出2^i
{
base[]=;
for(int i=;i<;i++) base[i]=base[i-]*;
}
int GetId(int x,int y){ return x*+y; }
void SetHead()
{
for(int i=;i<;i++)
for(int j=;j<;j++)
{
int a=maze[i][j]/;
int b=maze[i][j]%;
if(b==) { maze[a-][]=maze[i][j]; maze[i][j]=; } //把11,21,31,41挑出来
}
}
LL GetHash(int S[][])
{
LL ret=;
for(int i=;i<;i++)
for(int j=;j<;j++) ret+=(LL)S[i][j]*base[GetId(i,j)]; //得到哈希值
return ret;
}
bool InsertHash(LL val)
{
LL v=val%mod;
while(Hash[v]!=-&&Hash[v]!=val) v=(v+)%mod; //判重
if(Hash[v]==-){ Hash[v]=val; return true; } //可以插入
return false;
}
void init()
{
memset(Hash,-,sizeof(Hash));
while(!que.empty()) que.pop();
id=;
G=GetHash(goal);
int k=;
int cur=id++;
for(int i=;i<;i++)
for(int j=;j<;j++)
{
Nod[cur].S[i][j]=maze[i][j];
if(maze[i][j]==){ Nod[cur].px[k]=i; Nod[cur].py[k++]=j; } //得到最初的状态
}
Nod[cur].dist=;
que.push(cur);
}
void Change_S(node& e,int x,int y,int pick,int k)
{
for(int i=;i<;i++)
for(int j=;j<;j++)
if(e.S[i][j]==pick)
{
e.S[i][j]=;
e.S[x][y]=pick;
e.px[k]=i; e.py[k]=j;
return;
}
}
void AddNode(int now)
{
node& e=Nod[now];
for(int i=;i<;i++)
{
int x=e.px[i];
int y=e.py[i];
int pre=e.S[x][y-];
if(pre==) continue; //也是空位不管 int a=pre/;
int b=pre%;
if(b==) continue; //不能是*7 int pick=pre+;
node t=e;
t.dist++;
Change_S(t,x,y,pick,i);
LL nowG=GetHash(t.S);
if(!InsertHash(nowG)) continue; //能否插入 int cur=id++;
Nod[cur]=t;
que.push(cur);
}
}
int solve()
{
SetHead();
init();
while(!que.empty())
{
int now=que.front(); que.pop();
node& e=Nod[now];
LL nowG=GetHash(e.S);
if(nowG==G) return e.dist; //找到解
AddNode(now);
}
return -;
}
int main()
{
int T;
GetBase();
cin>>T;
while(T--)
{
memset(maze,,sizeof(maze));
for(int i=;i<;i++)
for(int j=;j<;j++) cin>>maze[i][j];
printf("%d\n",solve());
}
return ;
}

hdu1067-Gap(bfs+哈希)的更多相关文章

  1. UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))

    Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...

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

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

  3. poj 2432 Around the world bfs+哈希

    由于每个点的状态包含走过来的距离,所以要存二维的状态,但是状态总量太多,所以可以用哈希来搞. 那么就是bfs最短路,哈希记录状态了. #include <iostream> #includ ...

  4. Poj2946-The Warehouse(bfs+哈希)

    题目我就不粘贴了... 题意:给出地图,最大8*8,出口用'E'表示,空地用'.'表示,数字表示此处有多少个箱子,主人公的起点应该是在有箱子的地方,他可以朝四个方向移动,但是只有两种方式 一种是他移动 ...

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

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

  6. 【算法】BFS+哈希解决八数码问题

    15拼图已经有超过100年; 即使你不叫这个名字知道的话,你已经看到了.它被构造成具有15滑动砖,每一个从1到15上,并且所有包装成4乘4帧与一个瓦块丢失.让我们把丢失的瓷砖“X”; 拼图的目的是安排 ...

  7. POJ-3131-Cubic Eight-Puzzle(双向BFS+哈希)

    Description Let's play a puzzle using eight cubes placed on a 3 × 3 board leaving one empty square. ...

  8. HDU1067 Gap

    题目: Let's play a card game called Gap. You have 28 cards labeled with two-digit numbers. The first d ...

  9. codevs1004四子连棋[BFS 哈希]

    1004 四子连棋   时间限制: 1 s   空间限制: 128000 KB   题目等级 : 黄金 Gold   题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗 ...

随机推荐

  1. 找出最小的k个数

    •已知数组中的n个正数,找出其中最小的k个数. •例如(4.5.1.6.2.7.3.8),k=4,则最小的4个数是1,2,3,4 •要求: –高效: –分析时空效率 •扩展:能否设计出适合在海量数据中 ...

  2. jquery图片3D旋绕效果 rotate3Di的操作

    这是一个图片效果,很简单实用,只需要一个"rotate3Di.js"的插件就行, 关于rotate的用法有如下几种: $(选择器).rotate3Di(30); //把图片3D旋转 ...

  3. poj 2407 Relatives(简单欧拉函数)

    Description Given n, a positive integer, how many positive integers less than n are relatively prime ...

  4. tool - 支持TestLink 1.93,将excel格式用例转化成可以导入的xml格式

     tool - 支持TestLink 1.93,将excel格式用例转化成可以导入的xml格式  https://github.com/zhangzheyuk/CaseConvert

  5. java 字符串为空问题

    java 字符串为空问题 String testStr = null; System.out.println(testStr); if (testStr == null) { System.out.p ...

  6. C# Excel导入、导出

    本篇主要介绍C#的Excel导入.导出. 目录 1. 介绍:描述第三方类库NPOI以及Excel结构 2. Excel导入:介绍C#如何调用NPOI进行Excel导入,包含:流程图.NOPI以及C#代 ...

  7. (转)iOS7界面设计规范(10) - UI基础 - 文字排版与配色

    明天就是周四了.貌似前几天还在恨周一呢.话说今天几乎开了一整天的会,正经事情没做多少:这种感觉比一整天从早到晚12个小时的忙碌于一件事情还要让人感到疲惫的对叭?那今天的iOS7设计规范更新又是一篇很简 ...

  8. delphi 简单的删除字符串尾部数字的代码

    delphi  简单的删除字符串尾部数字的代码 方式一: function FilterShowName(const sName: String): String; var I: Integer; b ...

  9. Qt Label show Images

    第一.我们需要让QLabel的大小不因为图片的大小变化而变化,可以用下面语句实现 ui->imageLabel->setSizePolicy(QSizePolicy::Ignored, Q ...

  10. python 安装 memcache

    方式一: python3 -m pip install python-memcached 方式二: pip3 install python-memcached 方式三: tar zxf python- ...