hdu 1043 Eight(BFS经典)

来源:互联网 发布:淘宝上有好看衣服吗 编辑:程序博客网 时间:2024/05/16 01:23

题意:。。。

思路:

小白上有讲解。

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

哈希:拼人品。。

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;}


0 0
原创粉丝点击