hihocoder 1233 Boxes(bfs+状态压缩+hash表)

题意:

有n个卡槽,放有体积不同的n个空盒子,每次你可以移动一个空盒子到相邻卡槽,但前提是相邻卡槽若已经有空盒子,那么要移动的空盒子体积必须小于已有的空盒子,问要移动多少步才能使得从左到右,每个卡槽空盒子的体积递增。

解析:

状态压缩,用一个 long long 的状态表示当前所有的状态,每个盘子所在的位置,每个盘子的位置用 7 位二进制表示,总共的状态为247,因为每次盘子只能向左放或者向右放,那么我们从结束的位置开始搜索, bfs 预处理出目标状态到所有状态所需要的最小步数插入 hash 表中(反方向建表),然后对于每个询问 O(1) 回答即可。

my code

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;

const int MAXSIZE = 1000003;
const int N = 7;
const int dir[] = {-1, 1};
int goal[] = {1, 2, 3, 4, 5, 6, 7};
int bit[65];

struct Node { ll state; int step; };

int v[N], save[N], n;

struct HashTable {
    int head[MAXSIZE], next[MAXSIZE];
    ll state[MAXSIZE], dist[MAXSIZE];
    int sz;

    void clear() {sz = 1; memset(head, 0, sizeof(head)); }

    int getHash(ll state) { return state % MAXSIZE; }

    bool insert(ll st, ll step) {
        int h = getHash(st);
        int u = head[h];
        while(u) {
            if(st == state[u]) return false;
            u = next[u];
        }
        state[sz] = st;
        dist[sz] = step;
        next[sz] = head[h];
        head[h] = sz++;
        return true;
    }

    int find(ll st) {
        int h = getHash(st);
        int u = head[h];
        while(u) {
            if(st == state[u]) return dist[u];
            u = next[u];
        }
        return -1;
    }
} table[7];

ll getState(int a[]) {
    ll state = 0;
    for(int i = 0; i < n; i++) state = (state << 7) + (1 << (a[i] - 1)); 
    return state;
}

inline int lowbit(int x) { return x & (-x); };

inline ll getSlot(ll state, int pos) { return ( state >> (7 * pos) ) % 128; }

inline bool move(ll &state, int cur, int next) {
    ll x = getSlot(state, cur);
    ll curTop =  bit[lowbit(x)];

    x = getSlot(state, next);
    ll nextTop = bit[lowbit(x)];

    if(curTop > nextTop && nextTop != 0) return false;

    state = state^(1ll << (curTop - 1 + 7 * cur))^(1ll << (curTop - 1 + 7 * next));
    return true;
}

void bfs(int id) {
    queue<Node> que;
    ll endState = getState(goal);
    que.push( (Node){endState, 0} );
    table[id].insert(endState, 0);

    while(!que.empty()) {
        Node node = que.front();
        que.pop();

        for(int i = 0; i < n; i++) {
            if(getSlot(node.state, i) == 0)
                continue;

            for(int d = 0; d < 2; d++) {
                int next = i + dir[d];
                if(next < 0 || next >= n) continue;
                Node newNode = node;
                if(move(newNode.state, i, next)) {
                    newNode.step = node.step + 1;
                    if(table[id].insert(newNode.state, newNode.step))
                        que.push(newNode);
                }
            }
        }
    }
}

void prepare() {
    bit[0] = 0;
    for(int i = 0; i < 7; i++) bit[1 << i] = i+1;

    for(int i = 0; i < 7; i++) {
        n = i+1;
        table[i].clear();
        bfs(i);
    }
}

void discrete() {
    for(int i = 0; i < n; i++) save[i] = v[i];
    sort(save, save+n);
    for(int i = 0; i < n; i++)
        v[i] = lower_bound(save, save+n, v[i]) - save + 1;
}

int main() {
    prepare();
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        for(int i = 0; i < n; i++) scanf("%d", &v[i]);
        discrete();
        printf("%d\n", table[n-1].find( getState(v) ));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值