[hdu 6171 Admiral]Hash+暴搜+中途相遇法

[hdu 6171 Admiral]Hash+暴搜+中途相遇法

分类:meet in middle brute force Hash

1. 题目链接

[hdu 6171 Admiral]

2. 题意描述

给你一个高度为6的数塔,问最少多少步能走成如下状态。大于20步可以直接输出“too difficult”!
每次只能将值为0的点与其左上,上方,下方,右下四个方向的一个点进行交换。
0
1 1
2 2 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5

3. 解题思路

很裸的暴搜+Hash。对每一个状态朝四个方向进行暴搜。
如果不用meet in middle方法 理论上暴搜的状态数和复杂度会爆炸。

对于“固定源点和端点的条件下,求 t 步之内是否可以从源点达到端点,如果能,求出最短步数。”的这类问题,一般都可以用meet in middle的方法来解决。
meet in middle:枚举分别从源点、端点开始走t2步,记录到达中间所有的状态需要的最短步数。然后,求出源点、端点走 t2 步能够达到的公共状态。对于所有公共状态,源点到公共状态的步数+端点到公共状态的步数的最小值就是从源点到端点所需要的最短步数。

4. 实现代码

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef long double LB;
typedef unsigned int uint;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, LL> PLL;
typedef pair<LB, LB> PLB;
typedef vector<int> VI;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const long double PI = acos(-1.0);
const long double eps = 1e-4;
void debug() { cout << endl; }
template<typename T, typename ...R> void debug (T f, R ...r) { cout << "[" << f << "]"; debug (r...); }
template<typename T> inline void umax(T &a, T b) { a = max(a, b); }
template<typename T> inline void umin(T &a, T b) { a = min(a, b); }
template <typename T> inline bool scan_d (T &ret) {
    char c; int sgn;
    if (c = getchar(), c == EOF) return 0; //EOF
    while (c != '-' && (c < '0' || c > '9') ) if((c = getchar()) == EOF) return 0;
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return 1;
}
template<typename T> void print(T x) {
    static char s[33], *s1; s1 = s;
    if (!x) *s1++ = '0';
    if (x < 0) putchar('-'), x = -x;
    while(x) *s1++ = (x % 10 + '0'), x /= 10;
    while(s1-- != s) putchar(*s1);
}
template<typename T> void println(T x) { print(x); putchar('\n'); }
template<typename T> T randIntv(T a, T b) { return rand() % (b - a + 1) + a; } /*[a, b]*/

struct Node {
    static const int MAXB = 6;
    static const int MAXL = 21;
    int d[MAXB][MAXB];
    LL getHashV() {
        LL ret = 0;
        for(int i = 0; i < MAXB; ++i) {
            for(int j = 0; j <= i; ++j) {
                ret += d[i][j];
                ret *= MAXB;
            }
        }
        return ret;
    }
    static PII getHashD(LL hv, Node& e) {
        static int b[MAXL];
        int k = MAXL;
        while(k >= 0) {
            b[-- k] = hv % MAXB;
            hv /= MAXB;
        }

        PII ret(-1, -1);
        for(int i = 0; i < MAXB; ++i) {
            for(int j = 0; j <= i; ++j) {
                e.d[i][j] = b[k ++];
                if(e.d[i][j] == 0) ret = make_pair(i, j);
            }
        }
        return ret;
    }
    void P() {
        for(int i = 0; i < Node::MAXB; ++i) {
            for(int j = 0; j <= i; ++j) {
                print(d[i][j]); putchar(' ');
            }
            putchar('\n');
        }
    }
} S, E, O;
int T;

int dx[] = { -1, -1, 1, 1};
int dy[] = { -1, 0, 0, 1};

inline bool okay(const int& x, const int& y) { return 0 <= x && x < Node::MAXB && 0 <= y && y <= x; }

map<LL, int> mp1, mp2;
map<LL, int>::iterator it;

void bfs(map<LL, int>& mp, Node& u, const int& mstep) {
    typedef PIL QNode; queue<QNode> q;
    int x, y, step = 0, mpv; LL hashV = u.getHashV();
    mp.clear(); mp[hashV] = 0;
    q.push(QNode(step, hashV));
    while(!q.empty()) {
        tie(step, hashV) = q.front(); q.pop();
        if(step > mstep) continue;

        mpv = mp[hashV];
        if(mpv != 0 && mpv <= step) continue;
        mp[hashV] = step;

        tie(x, y) = Node::getHashD(hashV, O);

        for(int i = 0; i < 4; ++i) {
            int nx = x + dx[i], ny = y + dy[i];
            if(!okay(nx, ny)) continue;

            swap(O.d[x][y], O.d[nx][ny]); hashV = O.getHashV();
            q.push(QNode(step + 1, hashV));
            swap(O.d[x][y], O.d[nx][ny]);
        }
    }
}

int main() {
#ifdef ___LOCAL_WONZY___
    freopen ("input.txt", "r", stdin);
#endif // ___LOCAL_WONZY___
    scan_d(T);
    for(int i = 0; i < Node::MAXB; ++i) {
        for(int j = 0; j <= i; ++j) {
            E.d[i][j] = i;
        }
    }
    bfs(mp1, E, 10);    /** mp1.size() == 4034 **/
    while(T --) {
        for(int i = 0; i < Node::MAXB; ++i) {
            for(int j = 0; j <= i; ++j) {
                scan_d(S.d[i][j]);
            }
        }

        bfs(mp2, S, 10);

        int ans = 21;
        if(S.getHashV() == E.getHashV()) ans = 0;
        for(it = mp2.begin(); it != mp2.end(); ++it) {
            int s1 = mp1[it->first], s2 = it->second;
            if(s1 == 0 || s1 + s2 >= ans) continue;
            ans = s1 + s2;
        }
        if(ans == 21) puts("too difficult");
        else println(ans), assert(ans <= 20);
    }
#ifdef ___LOCAL_WONZY___
    cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // ___LOCAL_WONZY___
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值