hdu 1043 Eight(BFS经典)

本文介绍了一种解决八数码问题的方法,使用哈希表来记录状态,并通过BFS搜索算法寻找最优路径。代码中详细展示了如何实现状态的哈希、状态生成及回溯路径打印等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:。。。

思路:

小白上有讲解。

编码:速度快,但适用范围小...

哈希:拼人品。。

map:简单,但速度慢

HDU上单向BFS无数TLE。。代码先放这。。

//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cassert>
#include <algorithm>
#include <cmath>
#include <climits>
#include <set>
#include <map>
using namespace std;

#define SPEED_UP iostream::sync_with_stdio(false);
#define FIXED_FLOAT cout.setf(ios::fixed, ios::floatfield);
#define rep(i, s, t) for(int (i)=(s);(i)<=(t);++(i))
#define urep(i, s, t) for(int (i)=(s);(i)>=(t);--(i))

typedef long long LL;

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

int n=3, m=3;
char mm[9];
int tab[11], last[362880+100], sav[362880+100], spos;

struct st{
    char s[9];
    int t, h, pos;
    st(){};
    st(const char *m, int t, int h, int pos):t(t), h(h), pos(pos) {
        memcpy(s, m, 9);
    }
};
st q[362880+100];

bool init() {
    memset(last, -1, sizeof(last));
    char ch;
    rep(i, 0, 8) {
            if (scanf("%c", &ch) == 1) {
                if (ch == 'x')
                    mm[i] = '9', spos = i;
                else
                    mm[i] = ch;
            }
            else
                return false;
            getchar();
        }
    return true;
}

int Hash(const char* a, int len) {
    int ret = 0;
    rep(i, 0, len-1) {
        int t = 0;
        rep(j, i+1, len-1)
            if (a[j] < a[i]) ++t;
        ret += tab[len-1-i]*t;
    }
    return ret;
}

void print_state(const st &x) {
    cout << endl;
    rep(i, 0, 2) {
        rep(j, 0, 2) cout << x.s[i*3+j];
        cout << endl;
    }
    cout << "t: " << x.t << " pos: " << x.pos << endl;
}

void print(int x) {
    if (last[x] == -1 || last[x] == -2) return;
    print(last[x]);
    switch(sav[x]) {
    case 0:putchar('u');break;
    case 1:putchar('d');break;
    case 2:putchar('l');break;
    case 3:putchar('r');break;
    }
}

int check(const char* p) {
    rep(i, '1', '9')
        if (i != (*p++)) return 0;
    return 1;
}

void solve() {
    int x, y, nx, ny, npos;
    int hcode = Hash(mm, 9);
    q[0] = st(mm, 0, hcode, spos);
    last[hcode] = -2;
    int head = 0, tail = 1;
    while (head < tail) {
        const st &fr = q[head++];
        int h = fr.h;
        //print_state(fr);
        if (check(fr.s)) {
            //cout << fr.t << endl;
            print(h);
            cout << endl;
            return;
        }

        x = fr.pos/3,  y = fr.pos%3;
        rep(i, 0, 3) {
            nx = x + dx[i];
            ny = y + dy[i];
            npos = nx*3+ny;
            if (0 <= nx && nx < 3 && 0 <= ny && ny < 3) {
                st &tmp = q[tail];
                tmp = fr;
                swap(tmp.s[fr.pos], tmp.s[npos]);
                int _h = Hash(tmp.s, 9);
                if (last[_h] != -1) continue;

                tmp.t += 1;
                tmp.h = _h;
                tmp.pos = npos;
                ++tail;
                sav[_h] = i;
                last[_h] = h;
            }
        }
    }

    puts("unsolvable");
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
#endif
    //SPEED_UP
    tab[0] = 1;rep(i, 1, 9) tab[i] = tab[i-1]*i;

    while (init()) {
        solve();
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值