poj 2046 Gap(bfs+hash)

时间:2024-08-02 23:37:38

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.

poj 2046 Gap(bfs+hash)

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. 

poj 2046 Gap(bfs+hash)

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. 

poj 2046 Gap(bfs+hash)

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

Japan 2003,Aizu
这题的关键在用hash来保存状态,其他的是bfs基础了。。。
 #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 ;
}

相关文章