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. Ext.window的close的问题

    以前每次都是用的hide,关闭后隐藏窗体,下一次点击再打开,这种方法在我的随笔里面有,可是现在遇到一个问题,我的窗体里面有个formpanel,formpanel每一项都有一个默认值,意思就是修改的时 ...

  2. DIV 与 Table 嵌套

    当然可以了.对于DIV定义表示一块可显示 HTML 的区域. Specifies a container that renders HTML. 注释此元素在 Internet Explorer 3.0 ...

  3. Tomcat 原理篇

    TOMCAT 原理篇一.Tomcat 组成(Tomcat 由以下组件组成) 1.server a) Server是一个Catalina Servlet容器: b) Server 可以包含一个或多个se ...

  4. java基础之代理

    代理的定义,代理的应用,代理的特性

  5. DotNet程序汉化过程--SnippetCompiler简单解说

    SnippetCompiler介绍 平时要验证一段C#代码或者写一个算法,就得打开庞大的VS新建一个解决方案,占用了硬盘空间不说还费时费力.SnippetCompiler这个工具就可以在这里帮到我们了 ...

  6. 调试EF源码

    在解决方案中添加下载好的EF的源码的引用 在新建项目中添加程序集的引用 添加配置文件中的节点 测试 获取程序集的公钥标记: 使用sn.exe命令 使用reflector

  7. (转)wcf client与webservice通信(-)只修改配置文件而改变服务端

    http://www.cnblogs.com/yiyisawa/archive/2008/12/16/1356191.html 问题: 假设有一个大型系统新版本使用wcf 作为服务端,生成wcf cl ...

  8. (转).net程序员转战android第三篇---登录模块之静态登录

    这一篇我将分2个部分记录登录界面,第一部分是静态登录, 这部分将如何从界面布局.控件使用.文件关系.数据验证.登陆实现等5小块记录. 第二部分是动态登录,这块会基于上面的4小块,在数据验证不是静态数据 ...

  9. Jquery:Jquery中的事件<二>

    这几天快忙死了,办了离职还得办入职,完全打乱了我的计划,但是能有一个理想的工作,还是很开心的,以后加把劲,争取把计划再赶上来!不说了,学习!!! 五.事件对象的属性 1.event.type:获取事件 ...

  10. SQL中常用的时间格式

    一些常用的时间格式 先讲一下一些基本的格式模式 格式模式      说明 d                   月中的某一天.一位数的日期没有前导零. dd                 月中的某 ...