您的位置:首页 > 其它

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

2015-09-22 16:36 281 查看

题意:

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

解析:

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

mymy codecode

[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
, save
, 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: