HDU-1067

求最短路的问题还是用广度搜索,但问题在于判重不好办,不判重又会暴内存和超时,所以参考了一下别人写的哈希表进行判重。

将4*8的矩阵内的数字按照各自的权值相乘之后加起来对一个素数求模即可得到该状态所对应哈希表的下标。如果有重复就乘10再模一次。


#include <iostream>
#include <queue>
#include <string.h>
using namespace std;

struct node{
    int t;
    int x[4],y[4],map[4][8];
};

int sx1,sy1,sx2,sy2,sx3,sy3,sx4,sy4;
int map[4][8];
long long HASH[1000010];
int value[4][7] = {{11,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}};

bool Judge(int m[4][8]){
    int i,j,l=0;
    for(i=0;i<4;i++){
        for(j=0;j<7;j++){
            if(value[i][j] != m[i][j]){
                l = 1;
                break;
            }
        }
        if(l) break;
    }
    if(l) return false;
    else return true;
}

long long Find(int m[4][8]){
    long long sum=0,l=1;
    int i,j;
    for(i=0;i<4;i++){
        for(j=1;j<8;j++){
            sum += (long long)m[i][j]*l;
            l *= 2;
        }
    }
    return sum;
}

int BFS(){
    int i,j,l,temp;
    long long v,vv;
    queue<node> q;
    node temp1,temp2;
    temp1.x[0] = sx1;
    temp1.x[1] = sx2;
    temp1.x[2] = sx3;
    temp1.x[3] = sx4;
    temp1.y[0] = sy1;
    temp1.y[1] = sy2;
    temp1.y[2] = sy3;
    temp1.y[3] = sy4;
    temp1.t = 0;
    memcpy(temp1.map,map,sizeof(map));
    if(Judge(temp1.map)) return temp1.t;
    v = Find(temp1.map);
    HASH[v%1000007] = v;
    q.push(temp1);
    while(!q.empty()){
        temp1 = q.front();
        q.pop();
        for(i=0;i<4;i++){
            temp2.x[0] = temp1.x[0];
            temp2.x[1] = temp1.x[1];
            temp2.x[2] = temp1.x[2];
            temp2.x[3] = temp1.x[3];
            temp2.y[0] = temp1.y[0];
            temp2.y[1] = temp1.y[1];
            temp2.y[2] = temp1.y[2];
            temp2.y[3] = temp1.y[3];
            temp2.t = temp1.t+1;
            memcpy(temp2.map,temp1.map,sizeof(map));
            temp = temp2.map[temp2.y[i]][temp2.x[i]-1];
            if(temp%10 == 7) continue;
            for(j=0;j<4;j++){
                for(l=1;l<8;l++){
                    if(temp2.map[j][l] == temp+1){
                        temp2.map[temp2.y[i]][temp2.x[i]] = temp+1;
                        temp2.map[j][l] = 0;
                        temp2.x[i] = l;
                        temp2.y[i] = j;
                        break;
                    }
                }
                if(l != 8) break;
            }
            if(Judge(temp2.map)) return temp2.t;
            v = Find(temp2.map);
            vv = v%1000007;
            while(HASH[vv] != -1 && v != HASH[vv]) vv = vv*10%1000007;
            if(HASH[vv] == -1){
                HASH[vv] = v;
                q.push(temp2);
            }
        }
    }
    return -1;
}

int main(){
    int t,i,j;
    cin>>t;
    while(t--){
        for(i=0;i<4;i++){
            for(j=1;j<8;j++) cin>>map[i][j];
        }
        for(i=0;i<4;i++){
            for(j=1;j<8;j++){
                if(map[i][j] == 11){
                    map[0][0] = 11;
                    map[i][j] = 0;
                    sx1 = j;
                    sy1 = i;
                }
                if(map[i][j] == 21){
                    map[1][0] = 21;
                    map[i][j] = 0;
                    sx2 = j;
                    sy2 = i;
                }
                if(map[i][j] == 31){
                    map[2][0] = 31;
                    map[i][j] = 0;
                    sx3 = j;
                    sy3 = i;
                }
                if(map[i][j] == 41){
                    map[3][0] = 41;
                    map[i][j] = 0;
                    sx4 = j;
                    sy4 = i;
                }
            }
        }
        for(i=0;i<=1000007;i++) HASH[i] = -1;
        cout<<BFS()<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值