poj 2046 Gap

Language:
Gap
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 1832 Accepted: 831
Description
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
Source
Japan 2003,Aizu

/*
恩这题挺好的.
BFS+hash判重.
打表目标状态并判断(BFS最优解).
每种状态扩展(最先扩展完成的即是最优解). 
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 10
#define MAXM 101
#define mod 1000007
using namespace std;
int t;
int d[5][8]=// 目标状态
{
    0, 0, 0, 0, 0 ,0, 0, 0,
    11,12,13,14,15,16,17,0,
    21,22,23,24,25,26,27,0,
    31,32,33,34,35,36,37,0,
    41,42,43,44,45,46,47,0
};
bool b[mod+10]; 
struct data
{
    int g[MAXN][MAXN];
    int step;
}s,q[mod>>1],e,w;
int hash(int g[][10])
{
    long long tot=0,hash=0,s[MAXM]={0};
    for(int i=1;i<=4;i++)
        for(int j=0;j<=7;j++)
        {
            s[++tot]=g[i][j]%10;
            s[++tot]=g[i][j]/10;
        }
    for(int i=1;i<=tot;i++)
        hash=hash*7+s[i];
    return (hash&0x7fffffff)%mod;
}
bool jd(data e)
{
    for(int i=1;i<=4;i++)
        for(int j=0;j<=7;j++)
        {
            if(e.g[i][j]!=d[i][j]) return false;
        }
    return true;
}
void change(int x,int x1,int y1)
{
    for(int i=1;i<=4;i++)
        for(int j=1;j<=7;j++)
        {
            if(x==w.g[i][j])
            {
                swap(w.g[i][j],w.g[x1][y1]);
                return ;
            }
        }
}
bool bfs()
{
    int head=0,tail=1;
    s.g[1][0]=11;
    s.g[2][0]=21;
    s.g[3][0]=31;
    s.g[4][0]=41;
    b[hash(s.g)]=true;
    s.step=0;
    q[tail]=s;
    while(head<tail)
    {
        head++;
        e=q[head];
        if(jd(e))
        {
            printf("%d\n",e.step);
            return true;
        }
        for(int i=1;i<=4;i++)
            for(int j=1;j<=7;j++)
            {
                w=e;
                if(!e.g[i][j])
                {
                    int x=w.g[i][j-1];
                    if(x==17||x==27||x==37||x==47||x==0) continue;
                    change(x+1,i,j);
                    int xx=hash(w.g);
                    if(!b[xx])
                    {
                        b[xx]=true;
                        w.step++;
                        q[++tail]=w;
                    }
                }
            }
    }
    return false;
}
int main()
{
    cin>>t;
    while(t--)
    {
        int x;
        memset(s.g,0,sizeof(s.g));
        memset(b,0,sizeof(b));
        for(int i=1;i<=4;i++)
            for(int j=1;j<=7;j++)
            {
                cin>>x;
                s.g[i][j]=x;
                if(x==11||x==21||x==31||x==41)
                    s.g[i][j]=0;
            }    
        if(!bfs())  printf("-1\n");
    }
    return 0;
}
posted @ 2016-06-11 20:45  nancheng58  阅读(136)  评论(0编辑  收藏  举报