poj 2046 Gap(bfs+hash)
Description
Let's play a card game called Gap.
You have cards labeled with two-digit numbers. The first digit (from to ) represents the suit of the card, and the second digit (from to ) 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 , and put them in the open space at the left end of the rows: "" to the top row, "" to the next, and so on. Now you have 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 "" is "", and "" has no successor. In the above layout, you can move "" to the gap at the right of "", or "" to the gap at the right of "". If you move "", a new gap is generated to the right of "". You cannot move any card to the right of a card of value , 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 . If there is no move sequence from the initial layout to the goal layout, produce "-1".
Sample Input
Sample Output
-
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define M 1000007
#define ll long long
ll aimNum;
ll hash[M];
struct Node{
ll x[],y[];//存空格的横纵坐标
ll mp[][];//存整张地图
long long time;//存时间
}tmp;
ll flag;
ll aim[][]={
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,
};
ll base[]={}; bool inserNum(ll ans){//hash的插入,看看是否跟之前的状态相同,其实跟vis数组标记一个意思
ll val=ans%M;
while(hash[val]!=- && hash[val]!=ans){
val=(val+)%M;
}
if(hash[val]==-){
hash[val]=ans;
return true;//可以插入返回true
}
return false;//否则返回false
} bool work(Node cnt){
ll ans=;
for(ll i=;i<;i++){
for(ll j=;j<;j++){
ans=ans+cnt.mp[i][j]*base[i*+j];//ans为整张图的hash值
}
}
if(ans==aimNum){
flag=;
}
if(inserNum(ans))
return true;
return false;
} ll bfs(){
queue<Node>q;
q.push(tmp);
Node t1,t2;
while(!q.empty()){
t1=q.front();
q.pop(); for(ll k=;k<;k++){//4个空格依次遍历
t2=t1;
ll tx=t2.x[k];
ll ty=t2.y[k];
for(ll i=;i<;i++){//遍历整张图,寻找符合的数
for(ll j=;j<;j++){
if(t2.mp[i][j]==) continue;//如果要调换的还是空格,则不行
if(t2.mp[i][j]!=t2.mp[tx][ty-]+) continue;//需要填入的数为前一个+1 swap(t2.mp[i][j],t2.mp[tx][ty]);
if(work(t2)){//判断是否可以继续往下走
t2.time=t1.time+;
t2.x[k]=i;//将新的空格的横纵坐标保存下来
t2.y[k]=j;
q.push(t2);
if(flag)
return t2.time;
} }
}
}
}
return -;
}
int main()
{ for(ll i=;i<;i++){
base[i]=base[i-]*;
}
aimNum=(ll);//aimNum是通过事先计算得出的 int t;
scanf("%d",&t); while(t--){ memset(hash,-,sizeof(hash)); tmp.mp[][]=tmp.mp[][]=tmp.mp[][]=tmp.mp[][]=; int k=;
for(int i=;i<;i++){
for(int j=;j<;j++){
scanf("%I64d",&tmp.mp[i][j]); if(tmp.mp[i][j]==) {
swap(tmp.mp[i][j],tmp.mp[][]);
tmp.x[k]=i;
tmp.y[k++]=j;
}
if(tmp.mp[i][j]==) {
swap(tmp.mp[i][j],tmp.mp[][]);
tmp.x[k]=i;
tmp.y[k++]=j;
}
if(tmp.mp[i][j]==) {
swap(tmp.mp[i][j],tmp.mp[][]);
tmp.x[k]=i;
tmp.y[k++]=j;
}
if(tmp.mp[i][j]==) {
swap(tmp.mp[i][j],tmp.mp[][]);
tmp.x[k]=i;
tmp.y[k++]=j;
}
}
} tmp.time=;//时间初始化为0
flag=;
work(tmp);//先判断一遍是否可以不用调换就可以达到目的图
if(flag){
printf("0\n");
}else{
printf("%I64d\n",bfs());
}
}
return ;
}
poj 2046 Gap(bfs+hash)的更多相关文章
- 【BZOJ】1054: [HAOI2008]移动玩具(bfs+hash)
http://www.lydsy.com/JudgeOnline/problem.php?id=1054 一开始我还以为要双向广搜....但是很水的数据,不需要了. 直接bfs+hash判重即可. # ...
- UVA 10798 - Be wary of Roses (bfs+hash)
10798 - Be wary of Roses You've always been proud of your prize rose garden. However, some jealous f ...
- poj 3414 Pots (bfs+线索)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10071 Accepted: 4237 Special J ...
- POJ 3414 Pots(BFS+回溯)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11705 Accepted: 4956 Special J ...
- POJ 3461 Oulipo(字符串hash)
题目链接 字符串hash判断字符串是否相等. code #include<cstdio> #include<algorithm> #include<cstring> ...
- POJ 3126 Prime Path(BFS算法)
思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...
- POJ - 3308 Paratroopers(最大流)
1.这道题学了个单词,product 还有 乘积 的意思.. 题意就是在一个 m*n的矩阵中,放入L个敌军的伞兵,而我军要在伞兵落地的瞬间将其消灭.现在我军用一种激光枪组建一个防御系统,这种枪可以安装 ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配
两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...
随机推荐
- VC用OLE方式读写Excel
前几天要做一个项目,需要读取Excel中的数据.从网上查资料发现,主要是有两种方式.一是把Excel表当成数据库使用ODBC读写,这样操作起来就跟操作Access数据库似的.但这种方式效率比较低.另一 ...
- Ubuntu安装配置Qt环境
安装 QT4.8.6库+QT Creator 2.4.1 下载地址发布 QT4.8.6库 http://mirrors.hustunique.com/qt/official_releases/qt/ ...
- i春秋30强挑战赛pwn解题过程
80pts: 栈溢出,gdb调试发现发送29控制eip,nx:disabled,所以布置好shellcode后getshell from pwn import * #p=process('./tc1' ...
- django: db howto - 2
继 django: db howto - 1 : 一 操作数据库的三种方式: [root@bogon csvt03]# python2.7 manage.py shell Python 2.7.5 ( ...
- flv网页播放器播放失败
在IIS6.0上发布网站时,在路径正确的情况下,网页flv播放器还是无法播放flv视频的解决方法. 1.打开IIS6.0管理器,打开发布的网站,点击打开属性窗口. 2.在HTTP头选项里找到MIME类 ...
- MySQL innotop实时监测工具
安装:wget http://innotop.googlecode.com/files/innotop-1.8.0.tar.gz# perl Makefile.PL # make install解决C ...
- MySql用statement实现DDL,DML,DQL的操作Demo
Demo1 Connection connection=null; Statement stmt=null; int result=-1; try { Class.forName("com. ...
- CSS学习笔记总结和技巧
跟叶老师说项目,他叫我写一个静态首页,看起来挺简单的,但是下手才发现在真的不会怎么下手啊,什么模型啊模块啊都不懂,写毛线啊!! 如图:页面下拉还有侧栏,中间内容等. 可是答应跟老师做了,不能怂啊,于是 ...
- Struts2:ValueStack
一.ValueStack 1 .ValueStack是一个接口,在struts2中使用OGNL(Object-Graph Navigation Language)表达式实际上是使用 ...
- debug连线指令
-當發現原本正常的連線突然斷線的,請用底下的指令debug 1.ifconfig (看看IPaddress) 2.wpa_clira stat (看看連線狀態) 3.如果是在連線中(wp ...