hdu1067-Gap(bfs+哈希)
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.
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.
#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+哈希)的更多相关文章
- UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))
Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...
- hdu.1067.Gap(bfs+hash)
Gap Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- poj 2432 Around the world bfs+哈希
由于每个点的状态包含走过来的距离,所以要存二维的状态,但是状态总量太多,所以可以用哈希来搞. 那么就是bfs最短路,哈希记录状态了. #include <iostream> #includ ...
- Poj2946-The Warehouse(bfs+哈希)
题目我就不粘贴了... 题意:给出地图,最大8*8,出口用'E'表示,空地用'.'表示,数字表示此处有多少个箱子,主人公的起点应该是在有箱子的地方,他可以朝四个方向移动,但是只有两种方式 一种是他移动 ...
- HDU - 1067 Gap (bfs + hash) [kuangbin带你飞]专题二
题意: 起初定28张卡牌的排列,把其中11, 21, 31, 41移动到第一列,然后就出现四个空白,每个空白可以用它的前面一个数的下一个数填充,例如43后面的空格可以用44填充,但是47后面即 ...
- 【算法】BFS+哈希解决八数码问题
15拼图已经有超过100年; 即使你不叫这个名字知道的话,你已经看到了.它被构造成具有15滑动砖,每一个从1到15上,并且所有包装成4乘4帧与一个瓦块丢失.让我们把丢失的瓷砖“X”; 拼图的目的是安排 ...
- 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. ...
- HDU1067 Gap
题目: Let's play a card game called Gap. You have 28 cards labeled with two-digit numbers. The first d ...
- codevs1004四子连棋[BFS 哈希]
1004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗 ...
随机推荐
- 《Learn python the hard way》Exercise 48: Advanced User Input
这几天有点时间,想学点Python基础,今天看到了<learn python the hard way>的 Ex48,这篇文章主要记录一些工具的安装,以及scan 函数的实现. 首先与Ex ...
- 用c++编写一个不能被继承的类(但是可以在类外部定义该类的对象)
据我们知道,我们只要把类的构造函数和析构函数定义为private类型,那么就不能够在外部建立给类的对象,也就不能以给类为基类进行继承,因为如果继承,建立对象的时候将要调用基类的构造函数,但是因为为pr ...
- day49
几天没写了 这几天比较麻木呢 各种课程的再看 想买一直不舍得money 今天下定决心买了 这样我也静下心好好备战把 一天背的东西好多 政治和作文也是背了就忘记 尽力把 今天的买的课很悬乎 就不在这说了 ...
- 第32讲 UI组件之 时间日期控件DatePicker和TimePicker
第32讲 UI组件之 时间日期控件DatePicker和TimePicker 在Android中,时间日期控件相对来说还是比较丰富的.其中, DatePicker用来实现日期输入设置, Time ...
- hdu 4135 Co-prime(容斥)
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- #python基础学习模块:marshal 对象的序列化
#标准库地址:https://docs.python.org/2/library/marshal.html"""有时候,要把内存中一个对象持久化保存磁盘或者序列化二进制流 ...
- iOS会议和组织
全世界有许多iOS会议和组织,如果你没有机会去参加,知道他们的存在和向他们学习对你也是有益的.事实上,他们中有些提供免费的幻灯片.视频,有用资料等,所以你不能够忽视他们. 有一些会议的主题并不仅仅关于 ...
- 掌握jQuery插件开发,这篇文章就够了
---恢复内容开始--- 在实际开发工作中,总会碰到像滚动,分页,日历等展示效果的业务需求,对于接触过jQuery以及数据jQuery使用的人来说,首先想到的肯定是寻找现有的jQuery插件来满足形影 ...
- ORacle 复制表
create table r_register_company as select companyid,companyname,from grdata.r_register_company inser ...
- Java 日期与字符串的转换
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public cl ...