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
刚看到这题的时候看半天都没懂题意,搜博客大部分都不讲题意,有的也只是一笔带过,可能是我英语太渣了。。。
题意:给你一个4*8的表格,首先输入的是四行第二~八列的数,然后把11放在一行一列,21放在二行一列,31放在三行一列,41放在四行一列,这样会出现四个空格,每个空格当且仅当左边的数有后继才可以移动数字,42的后继是43,17没有后继,问你最少移动多少次数字可以达到图3的效果
思路:这题麻烦的地方是怎么判断当前状态是否已经搜过了,很显然不能开32维数组存状态,只能转换成数字,hash一下,然后判断是否出现,hash的方法的话就是常见的按从前往后每次乘个数再加上当前数,然后取余一个素数存起来。乘的这个数一般情况下用素数比较好,试了下*8和*7的区别,差了一倍多。。。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 1000007
struct node
{
int t,s[32],p[48],d[4];
//s记录4*8个格子里面的数 p记录每个数字的位置 d记录空格的位置 t记录移动次数
}q[N];
int head[N],next[N];
int hash(int s[])
{
unsigned int k=0;
for(int i=0;i<32;i++)
k=k*7+s[i]; //这里试了下*8的区别,发现耗时多了一倍多,估计跟7是素数有关,乘11耗时差不多
return k%N;
}
void add(int x)
{
int i=hash(q[x].s);
next[x]=head[i];
head[i]=x;
}
int check(int s1[],int s2[])
{
int i;
for(i=0;i<32;i++)
if(s1[i]!=s2[i])
return 0;
return 1;
}
int find(int s[])
{
int h=hash(s),i;
i=head[h];
while(i!=-1)
{
if(check(s,q[i].s))
return i; //0表示找到了答案,>0表示已经存在
i=next[i];
}
return -1;//-1表示这个方案没有出现过
}
int bfs()
{
int i,j,k,head1,tail,t,x;
node e,r;
head1=1;
tail=2;
while(head1<tail)
{
r=q[head1];
for(i=0;i<4;i++)
{
e=r;
k=r.d[i];
if(r.s[k-1]%10<7&&r.s[k-1]!=0)
{
j=r.s[k-1]+1;
e.s[k]=j;
t=r.p[j];
e.s[t]=0;
if((x=find(e.s))<0) //千万不能先判0,因为0只表示找到了答案,但是不能知道当前状态是否存在
{
e.d[i]=t;
e.p[j]=k;
e.t=r.t+1;
q[tail]=e;
add(tail);
tail++;
}
else
if(x==0)
return r.t+1;
}
}
head1++;
}
return -1;
}
int main()
{
int i,j,k,t;
for(i=0;i<4;i++)
for(j=0;j<7;j++)
q[0].s[i*8+j]=(i+1)*10+j+1;
scanf("%d",&t);
while(t--)
{
memset(head,-1,sizeof(head));
add(0);
for(i=0;i<4;i++)
for(j=1;j<8;j++)
{
scanf("%d",&q[1].s[i*8+j]);
if(q[1].s[i*8+j]%10==1)
q[1].s[i*8+j]=0;
q[1].p[q[1].s[i*8+j]]=i*8+j;
}
for(i=0;i<4;i++)
q[1].s[i*8]=(i+1)*10+1,q[1].p[q[1].s[i*8]]=i*8;
if(find(q[1].s)==0)
{
printf("0\n");
continue;
}
k=0;
for(i=0;i<4;i++)
for(j=0;j<8;j++)
if(q[1].s[i*8+j]==0)
q[1].d[k++]=i*8+j;
q[1].t=0;
add(1);
printf("%d\n",bfs());
}
return 0;
}