Gap HDU - 1067

题目链接:Gap HDU - 1067

===================================================

Gap

Time Limit: 1000MS
Memory Limit: 32768 kB

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

===================================================

算法:BFS+hash/Map

题意:就是给出矩阵数组,然后11,21,31,41提到最左边,留出空,然后填空规则为,数一定是空左边数+1,空左边为空或者数为*7则无法填这个空

思路:

  • 难点在于判重,这里我用了两种办法

===================================================

一种就是Map,因为11,21,31,41再移位之后都为空,所以有效数字为4*6+1为25,刚好小于英文字母,不过要写个对应的map匹配数组用于映射。

后面我看了别人代码,发现AS2码中,题目中的数字都各自代表不同的字符,可以直接使用。

这里我用char数组存储,然后在最末尾加个‘\0’ 即可直接当作string使用。

#include <iostream>
#include <queue>
#include <map>
#include <cstring>

using namespace std;

char fin[33] = {
        11,12,13,14,15,16,17,1,
        21,22,23,24,25,26,27,1,
        31,32,33,34,35,36,37,1,
        41,42,43,44,45,46,47,1,'\0'};
char sta[33];

struct node{
    char str[33];
    int step;
    node(char ch[33],int s){
        strcpy(str,ch);
        step = s;
    }
    bool check(){
        for(int i=0;i<32;i++) if(str[i]!=fin[i]) return false;
        return true;
    }
    int Move(int val){
        for(int i=0;i<32;i++) if(str[i]==val) return i;
    }
};

bool bfs(){
    queue<node> q;
    map<string,bool> mp;
    q.push(node(sta,0));
    mp[sta] = true;
    while(!q.empty()){
        node p = q.front();q.pop();
        if(p.check()) {cout<<p.step<<endl;return true;}
        for(int i=0;i<32;i++) if(p.str[i]==1&&p.str[i-1]!=1&&p.str[i-1]%10!=7){
                int index = p.Move(p.str[i-1]+1);
                swap(p.str[i],p.str[index]);
                if(!mp[p.str]){
                    mp[p.str] = true;
                    q.push(node(p.str,p.step+1));
                }
                swap(p.str[i],p.str[index]);
        }

    }
    return false;
}

int main()
{
    int _;cin>>_;
    while(_--){
        for(int i = 1;i<33;){
            for(int j=0;j<7;j++){
                int ch;cin>>ch;
                if(ch==11||ch==21||ch==31||ch==41) sta[i++] = 1;
                else sta[i++] = ch;
            }i++;
        }sta[0] = 11,sta[8] = 21,sta[16] = 31,sta[24] = 41;sta[32]= '\0';
        if(!bfs()) puts("-1");
    }
    return 0;
}

===================================================

第二种就是hash;

hash分两部分,一个是hashValue公式,和hash冲突处理。

我这里选取2进制权值求和,冲突处理MOD1000007,然后+10MOD1000007.

#include <iostream>
#include <queue>
#include <map>
#include <cstring>

using namespace std;

int fin[32] = {
        11,12,13,14,15,16,17,1,
        21,22,23,24,25,26,27,1,
        31,32,33,34,35,36,37,1,
        41,42,43,44,45,46,47,1};
int sta[32], stb[4];

struct node{
    int str[32];
    int step;
    int hole[4];
    node(int ch[32],int s,int a[4]){
        for(int i=0;i<32;i++) str[i] = ch[i];
        step = s;
        for(int i=0;i<4;i++) hole[i] = a[i];
    }
    bool check(){
        for(int i=0;i<32;i++) if(str[i]!=fin[i]) return false;
        return true;
    }
    int Move(int val){
        for(int i=0;i<32;i++) if(str[i]==val) return i;
    }
};

const int MOD = 1000007;
long long hashValue[MOD*2];

long long hashCode(int ch[32]){
    long long sum = 0,tnt = 1;
    for(int i=0;i<32;i++) sum += ch[i]*tnt,tnt*=2;
    return sum;
}

bool insertHash(int ch[32]){
    long long v = hashCode(ch);
    long long key = v%MOD;
    while(hashValue[key] != -1&&hashValue[key] != v) key = key+10%MOD;
    if(hashValue[key] == -1) {hashValue[key]=v;return true;}
    return false;
}

bool bfs(){
    memset(hashValue,-1,sizeof hashValue);
    queue<node> q;
    q.push(node(sta,0,stb));
    insertHash(sta);
    while(!q.empty()){
        node p = q.front();q.pop();
        if(p.check()) {cout<<p.step<<endl;return true;}
        for(int i=0;i<4;i++){
            int ph = p.hole[i];
            if(p.str[ph-1]!=1&&p.str[ph-1]%10!=7){
                int index = p.Move(p.str[ph-1]+1);
                node pp = p;
                swap(pp.str[ph],pp.str[index]);
                pp.hole[i] = index;
                pp.step = p.step + 1;
                if(insertHash(pp.str)) q.push(pp);
            }
        }

    }
    return false;
}

int main()
{
    int _;cin>>_;
    while(_--){
        for(int i = 1,h = 0;i<33;){
            for(int j=0;j<7;j++){
                int ch;cin>>ch;
                if(ch==11||ch==21||ch==31||ch==41) stb[h++] = i,sta[i++] = 1;
                else sta[i++] = ch;
            }i++;
        }sta[0] = 11,sta[8] = 21,sta[16] = 31,sta[24] = 41;

        //cout<<endl;
        //for(int i=0;i<32;i++){ cout<<sta[i]<<" ";if(i==7||i==15||i==23||i==31) cout<<endl;}

        //for(int i=0;i<4;i++) cout<<stb[i]<<" ";cout<<endl;


        if(!bfs()) puts("-1");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盐太郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值