hdu1067-Gap(bfs+哈希)

时间:2022-11-06 15:55:08
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.
hdu1067-Gap(bfs+哈希)
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.
hdu1067-Gap(bfs+哈希)
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.
hdu1067-Gap(bfs+哈希)
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
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&lpar;bfs &plus; 哈希判重&lpar;记忆化搜索&quest;&rpar;&rpar;

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

  2. hdu&period;1067&period;Gap&lpar;bfs&plus;hash&rpar;

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

  3. poj 2432 Around the world bfs&plus;哈希

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

  4. Poj2946-The Warehouse&lpar;bfs&plus;哈希&rpar;

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

  5. HDU - 1067 Gap &lpar;bfs &plus; hash&rpar; &lbrack;kuangbin带你飞&rsqb;专题二

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

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

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

  7. POJ-3131-Cubic Eight-Puzzle&lpar;双向BFS&plus;哈希&rpar;

    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四子连棋&lbrack;BFS 哈希&rsqb;

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

随机推荐

  1. 注意linux下面的命令行,要将PATH声明出来

    export PATH=/apps/svr/maven/bin:/apps/svr/jdk7/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/s ...

  2. Swift3&period;0语言教程比较、判断字符串

    Swift3.0语言教程比较.判断字符串 Swift3.0语言教程比较.判断字符串,在一个程序中字符串很多时,常常会做的操作就是对这些字符串进行比较和判断.本小节将讲解这些内容. 1.不区分大小写比较 ...

  3. Web 在线文件管理器学习笔记与总结(2)显示文件列表(名称,类型,大小,可读,可写,可执行,创建时间,修改时间,访问时间)

    主要函数: filetype() 判断文件类型 filesize() 得到文件大小(字节) is_readable() 判断文件是否可读 is_writeable() 判断文件是否可写 is_exec ...

  4. ios 获取崩溃日志

    虽然有了try catch异常捕获,但是还是存在崩溃异常无法捕获到的.我可以通过下面的方式来获取崩溃日志: - (BOOL)application:(UIApplication *)applicati ...

  5. HDU 5601 N&ast;M bulbs 找规律

    N*M bulbs 题目连接: http://codeforces.com/contest/510/problem/C Description NM个灯泡排成一片,也就是排成一个NM的矩形,有些开着, ...

  6. asp&period;net viewstate的模拟登陆

    其实 VIEWSTATE 不用太在意,倒是 JTCookieID 需要注意,这个才应该是服务器上用来维护 Session 的那个 Cookie.所以,你用 httpclient 的时候,不能上来就直接 ...

  7. sql char类型的空处理

    对是varchar类型的数据进行不是空的判断时,可以采用 --<>''或者!='' int类型的数据可以采用 is not null,但是它对char类型的数据没用 distinct 用于 ...

  8. ECHO不换行

    我想用批处理实现向s.txt中多次分别导入文本例如:“aaaa","bbbb","cccc","dddd"实现s.txt内效果如: ...

  9. javascript和python取dict字典对象的不同

    dict1={"a":1,"b":2,"22a":44} JS: dict1.a 和 dict1["a"]都可以 pyt ...

  10. 怎样高速地安装Ubuntu SDK

    我在先前的文章"Ubuntu SDK 安装"中已经具体地介绍了怎样安装Ubuntu SDK.可是非常多的开发人员可能在最后安装SDK所须要的chroots时候会失败.这里面的原因是SDK在安装chro ...