[BZOJ1085] [SCOI2005] 骑士精神 [IDA*]

[ L i n k \frak{Link} Link]


#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<ctime>
using namespace std;
struct mat {
    short p[6][6];
    int s;
    int t;
};
short tar[6][6] = {
    0, 0, 0, 0, 0, 0,
    0, 1, 1, 1, 1, 1,
    0, 0, 1, 1, 1, 1,
    0, 0, 0, 2, 1, 1, 
    0, 0, 0, 0, 0, 1,
    0, 0, 0, 0, 0, 0
};
bool a[6][6];
int ans = 0x3f3f3f3f;
int dx[8] = {1, -1, 1, -1, 2, -2, 2, -2};
int dy[8] = {2, -2, -2, 2, 1, -1, -1, 1};
char ch;
void Astar(const mat &hash, const int &diff, const int &step, const int &lim, const int &las) {
    if (!diff) {
        ans = min(ans, step);
        return;
    }
    if (step == lim) return;
    for (register int vs, vt, i = 0; i < 8; ++i) {
        if (i == (las ^ 1)) continue;
        vs = hash.s + dx[i];
        vt = hash.t + dy[i];
        if (vs > 5 || vs < 1) continue;
        if (hash.t + dy[i] > 5 || hash.t + dy[i] < 1) continue;
        mat tmp(hash);
        int add( (tmp.p[tmp.s][tmp.t] == tar[tmp.s][tmp.t]) + (tmp.p[vs][tmp.t+dy[i]] == tar[vs][vt]) - (tmp.p[tmp.s][tmp.t] == tar[vs][vt]) - (tmp.p[vs][vt] == tar[tmp.s][tmp.t]) );
        if (step + diff + add > lim) continue;
        swap(tmp.p[tmp.s][tmp.t], tmp.p[vs][vt]);
        tmp.s += dx[i];
        tmp.t += dy[i];
        Astar(tmp, diff + add, step + 1, lim, i);
    }
}
int main() {
    int T; 
    scanf("%d", &T);
    while (T--) {
        mat hash;
        ans = 0x3f3f3f3f;
        int diff(0);
        for (int i = 1; i <= 5; ++i) {
            for (int j = 1; j <= 5; ++j) {
                scanf(" %c", &ch);
                if (ch == '1') {
                    hash.p[i][j] = 1;
                } else if (ch == '0') {
                    hash.p[i][j] = 0;
                } else {
                    hash.p[i][j] = 2;
                    hash.s = i, hash.t = j;
                }
                if (hash.p[i][j] != tar[i][j]) ++diff;
            }
        }
        for (int i = 0; i <= 15; ++i) {
            Astar(hash, diff, 0, i, 8);
            if (ans != 0x3f3f3f3f) break;
        }
        if (ans == 0x3f3f3f3f) ans = -1;
        printf("%d\n", ans);
    }
    return 0;
}
BKDRhash记忆化
好像反而更慢了(也可能是我写丑了
没有判冲突,这个代码哇了
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<ctime>
using namespace std;
struct mat {
    short p[6][6];
    int s;
    int t;
};
short tar[6][6] = {
    0, 0, 0, 0, 0, 0,
    0, 1, 1, 1, 1, 1,
    0, 0, 1, 1, 1, 1,
    0, 0, 0, 2, 1, 1, 
    0, 0, 0, 0, 0, 1,
    0, 0, 0, 0, 0, 0
};
bool a[6][6];
int ans = 0x3f3f3f3f;
int dx[8] = {1, -1, 1, -1, 2, -2, 2, -2};
int dy[8] = {2, -2, -2, 2, 1, -1, -1, 1};
char ch;
int mns[1000005];
const int mod = 1000000;
int bkdrhash(const mat &x) {
    int ret;
    for (int i = 1; i <= 5; ++i) {
        for (int j = 1; j <= 5; ++j) {
            ret = ret * 31 + x.p[i][j];
        }
    }
    if (ret < 0) ret += ((-ret)/mod+1)*mod;
    ret %= mod;
    return ret;
}
void Astar(const mat &hash, const int &diff, const int &step, const int &lim, const int &las) {
    if (!diff) {
        ans = min(ans, step);
        return;
    }
    if (step == lim) return;
    int val = bkdrhash(hash);
    if (mns[val] < step) return;
    mns[val] = step;
    for (register int vs, vt, i = 0; i < 8; ++i) {
        if (i == (las ^ 1)) continue;
        vs = hash.s + dx[i];
        vt = hash.t + dy[i];
        if (vs > 5 || vs < 1) continue;
        if (hash.t + dy[i] > 5 || hash.t + dy[i] < 1) continue;
        mat tmp(hash);
        int add( (tmp.p[tmp.s][tmp.t] == tar[tmp.s][tmp.t]) + (tmp.p[vs][tmp.t+dy[i]] == tar[vs][vt]) - (tmp.p[tmp.s][tmp.t] == tar[vs][vt]) - (tmp.p[vs][vt] == tar[tmp.s][tmp.t]) );
        if (step + diff + add > lim) continue;
        swap(tmp.p[tmp.s][tmp.t], tmp.p[vs][vt]);
        tmp.s += dx[i];
        tmp.t += dy[i];
        Astar(tmp, diff + add, step + 1, lim, i);
    }
}
int main() {
    int T; 
    scanf("%d", &T);
    while (T--) {
        for (int i = 0; i < 1000005; ++i) mns[i] = 0x3f3f3f3f;
        mat hash;
        ans = 0x3f3f3f3f;
        int diff(0);
        for (int i = 1; i <= 5; ++i) {
            for (int j = 1; j <= 5; ++j) {
                scanf(" %c", &ch);
                if (ch == '1') {
                    hash.p[i][j] = 1;
                } else if (ch == '0') {
                    hash.p[i][j] = 0;
                } else {
                    hash.p[i][j] = 2;
                    hash.s = i, hash.t = j;
                }
                if (hash.p[i][j] != tar[i][j]) ++diff;
            }
        }
        for (int i = 0; i <= 15; ++i) {
            Astar(hash, diff, 0, i, 8);
            if (ans != 0x3f3f3f3f) break;
        }
        if (ans == 0x3f3f3f3f) ans = -1;
        printf("%d\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值