HDU 1067 Gap(哈希+bfs)

来源:互联网 发布:图片分割打印软件 编辑:程序博客网 时间:2024/05/16 13:47

Gap

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 997    Accepted Submission(s): 526


Problem 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
412 13 14 15 16 17 2122 23 24 25 26 27 3132 33 34 35 36 37 4142 43 44 45 46 47 1126 31 13 44 21 24 4217 45 23 25 41 36 1146 34 14 12 37 32 4716 43 27 35 22 33 1517 12 16 13 15 14 1127 22 26 23 25 24 2137 32 36 33 35 34 3147 42 46 43 45 44 4127 14 22 35 32 46 3313 17 36 24 44 21 1543 16 45 47 23 11 2625 37 41 34 42 12 31
 

Sample Output
03360-1
 

Source
Asia 2003(Aizu Japan)
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1016 1010 1175 1043 1241


题目大意:
    有种游戏,就是根据空格交换格子,具体规则有点麻烦。问最少多少步可以交换到目标状态。

解题思路:
    明显bfs从起始状态搜索,然后用哈希保存状态来进行比较就可以了。

附AC代码:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <vector>using namespace std;struct Node{    int x[4],y[4];//空格位置    int G[4][8];//状态    int step;//步数    Node& operator=(const Node &other)    {        step=other.step;        for(int i=0;i<4;++i)        {            x[i]=other.x[i];            y[i]=other.y[i];            for(int j=0;j<8;++j)                G[i][j]=other.G[i][j];        }        return *this;    }};struct Hash{    int v,nid,next;//v是哈希值,nid是指对应的节点下标,next是指向下一个Hash节点,初始化时置为-1};const int maxn=500005;//自己估计大小const int mod=100000+7;//自己估计大小int hash_id;//开全局,用于增加Hash节点,初始化值可置为mod-1Hash ha[mod+maxn];Node node[maxn];//状态数组并用数组模拟队列或栈int l,r;//队列的左右端点bool is_final(int nid)//判断是否到终点{    for(int i=0;i<4;++i)    {        for(int j=0;j<7;++j)            if(node[nid].G[i][j]!=10*(i+1)+j+1)                return false;        if(node[nid].G[i][7]!=1)            return false;    }    return true;}void hash_init()//哈希初始化{    for(int i=0;i<mod;++i)        ha[i].next=-1;    hash_id=mod-1;}int GetHash(int A[][8])//得到哈希值(具体参数自行修改){    int ret=0,k=1;  //ret是哈希值,k是系数    for(int i=0;i<4;i++)//根据状态的大小修改        for(int j=0;j<8;j++)        {            ret+=A[i][j]*k;            //k*=2;          //每次扩大2倍            k+=131;  //或者每次加131        }    return ret;}//可能哈希值会爆int,反正你写的时候注意就是了bool check(int a,int b)//自己写比较函数{    for(int i=0;i<4;++i)        for(int j=0;j<8;++j)            if(node[a].G[i][j]!=node[b].G[i][j])                return false;    return true;}bool Insert_Hash(int v,int nid)//v是要插入状态的哈希值,nid是要插入的结构体下标{    int a=v%mod;    int p=ha[a].next;    while(p!=-1)//一直找到末尾    {        if(ha[p].v==v&&check(ha[p].nid,nid))            return false;        //有完全相同的状态        p=ha[p].next;//下一个Hash节点    }    p=++hash_id;      //增加新节点    ha[p].v=v; ha[p].nid=nid;    ha[p].next=ha[a].next; ha[a].next=p;//前插法    return true;}int bfs(){    while(l<r)//用数组模拟队列    {        Node &now_node=node[l++];        for(int k=0;k<4;++k)//枚举四个点        {            int nx=now_node.x[k],ny=now_node.y[k];            if(now_node.G[nx][ny-1]%10==7||now_node.G[nx][ny-1]==1)                continue;            int ox=-1,oy=-1;            for(int i=0;i<4;++i)//寻找要交换的点            {                for(int j=0;j<8;++j)                {                    if(now_node.G[i][j]==now_node.G[nx][ny-1]+1)                    {                        ox=i;                        oy=j;                        break;                    }                }                if(ox!=-1)                    break;            }            Node &tt=node[r];            tt=now_node;            swap(tt.G[nx][ny],tt.G[ox][oy]);            tt.x[k]=ox;            tt.y[k]=oy;            tt.step=now_node.step+1;            int ha=GetHash(tt.G);            if(Insert_Hash(ha,r))            {                if(is_final(r))                    return tt.step;                ++r;            }        }    }    return -1;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        hash_init();        l=0;//队列初始化        r=1;        node[0].step=0;        for(int i=0;i<4;++i)        {            node[0].G[i][0]=10*(i+1)+1;            for(int j=1;j<8;++j)            {                scanf("%d",&node[0].G[i][j]);                int &this_n=node[0].G[i][j];                if(this_n==11||this_n==21||this_n==31||this_n==41)                {                    node[0].x[this_n/10-1]=i;                    node[0].y[this_n/10-1]=j;                    this_n=1;                }            }        }        if(is_final(0))//开始就满足条件            puts("0");        else printf("%d\n",bfs());    }        return 0;}

 
0 0
原创粉丝点击