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. poj 2836 Rectangular Covering(状态压缩dp)

    Description n points are given on the Cartesian plane. Now you have to use some rectangles whose sid ...

  2. python字符串(移除空白,长度,索引,分割,切片,拼接,格式化输出)

    常用功能: 移除空白: >>> name = "meng" >>> name 'meng' >>> name.strip() ...

  3. Window7下vagrant的部署

    1. 下载并安装VirtualBox     下载地址:https://www.virtualbox.org/wiki/Downloads,下载最新的安装包,接下来的安装步骤就是下一步下一步了,你懂的 ...

  4. Android万能分辨率适应法

    (1)获取屏幕的尺寸 WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); D ...

  5. highcharts的使用

    步骤: 1. 去highcharts官网下载最新版本 2. 在.aspx页面添加引用 例: <link href="../JS/highcharts/css/highslide.css ...

  6. javascript系统时间

    <div>                <%--系统时间--%>                当前时间是:                <script type=& ...

  7. asp.net 实现 tts

    之前用WinForm实现tts已经成功,就调用了下系统的类库.但我把相同的代码搬到asp.net上时却碰到了许多问题,查了好多网站.试过了很多方法,到现在算是做出了一部分吧. 之前调用微软的TTS是用 ...

  8. .NET踩坑记录【不断更新】

    NET 4.0 Tasks 使用 ThreadPool 可设置最大并发级别. 多个WebClient多线程下载受System.Net.ServicePointManager.DefaultConnec ...

  9. C/C++中的switch使用

    代码: #include <iostream> #include <string> #include <cstdio> using namespace std; i ...

  10. 编译Boost 详细步骤

    vs2008编译boost [一.Boost库的介绍] Boost库是一个经过千锤百炼.可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一. Boost库由C++标准委员会 ...