hdu 3567(八码数 + 双向bfs)

题目连接:https://vjudge.net/contest/353606#problem/B

参考文章:https://blog.csdn.net/laaahu/article/details/96648344

 

思路:

广搜,记录路径,因为最多的交换次数不会太多,所以可以用int类型的4进制数记录路径;因为要求字典序最小,所以走得方向是dlru,当每次找到合法的路径时判断是否是字典序最小的路径,因为是双向bfs,每次判断当前路径是否是字典序最小的,不断更新就好了。

 

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 9;
const int MAXN = 4e5 + 10;
const int inf = 1e9+10;
char s1[N + 5], s2[N + 5];
int min_step, vis[2][MAXN],tp[120];
ll pre[2][MAXN], pp[120];
struct Node {
    int id, loc, b[N], stu, num;
    ll path;
};
char op[5] = "dlru";
string rs;
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, -1, 1, 0};
int cantor(int aa[]) {
    int ans = 0;
    for(int i = 0; i < 9; i++) {
        int res = 0, m = 1, c = 1;
        for(int j = i + 1; j < 9; j++) {
            if(aa[j] < aa[i])
                res++;
            m *= c;
            c++;
        }
        ans += res * m;
    }
    return ans + 1;
}
string get_path(ll x,int bj,int k)
{
    int tot = 0;
    for(int i=1;i<=vis[bj][k];i++)
    {
        tp[++tot] = x%4; x/=4;
    }
    string ss = "";
    for(int i=tot;i>=1;i--)
    {
        ss += op[ tp[i] ];
    }
    return ss;
}
void bfs() {
    memset(vis, -1, sizeof(vis));
    queue <Node> q;
    Node t1, t2;
    for(int i = 0; i < 9; i++) {
        if(s1[i] == 'X')
            t1.b[i] = 9, t1.loc = i;
        else
            t1.b[i] = s1[i] - '0';

        if(s2[i] == 'X')
            t2.b[i] = 9, t2.loc = i;
        else
            t2.b[i] = s2[i] - '0';
    }
    t1.id = 0;
    t1.stu = cantor(t1.b);
    t1.path = 0;
    vis[0][t1.stu] = 0;
    t1.num = 0;

    t2.id = 1;
    t2.stu = cantor(t2.b);
    t2.path = 0;
    vis[1][t2.stu] = 0;
    t2.num = 0;

    q.push(t1);
    q.push(t2);
    while(!q.empty()) {
        t1 = q.front();
        q.pop();
        ll tmp;
        int x = t1.loc / 3, y = t1.loc % 3, id = t1.id;
        //printf("x = %d,y = %d,id = %d\n",x,y,id);
        for(int i = 0; i < 4; i++) {
            int tx = x + dx[i];
            int ty = y + dy[i];
            if(0 <= tx && tx < 3 && 0 <= ty && ty < 3) {
                t2 = t1;
                t2.b[x * 3 + y] = t2.b[tx * 3 + ty];
                t2.b[tx * 3 + ty] = 9;
                //for(int j=0;j<9;j++) printf("%d",t2.b[j]); printf("\n");
                t2.loc = tx * 3 + ty;
                t2.stu = cantor(t2.b);
                t2.num = t1.num + 1;
                if(vis[id][t2.stu] == -1) {
                    vis[id][t2.stu] = t1.num + 1;
                    if(id == 1)
                        pre[id][t2.stu] = (3 - i) * pp[t1.num] + t1.path;
                    else
                        pre[id][t2.stu] = t1.path * 4 + i;
                } else {
                    if(t2.num > vis[id][t2.stu])
                        continue;
                    else {
                        if(id == 1)
                            tmp = (3 - i) * pp[t1.num] + t1.path;
                        else
                            tmp = t1.path * 4 + i;
                        if(tmp < pre[id][t2.stu]) {
                            pre[id][t2.stu] = tmp;
                        }
                    }
                }
                t2.path = pre[id][t2.stu];
                if(vis[id ^ 1][t2.stu] != -1) {
                    string ss = get_path(pre[0][t2.stu], 0, t2.stu) + get_path(pre[1][t2.stu], 1, t2.stu);
                    int len = ss.length();
                    if(len < min_step) {
                        min_step = len;
                        rs = ss;
                    } else if(len > min_step) {
                        cout << min_step << endl;
                        cout << rs << endl;
                        return ;
                    } else {
                        if(rs.compare(ss) > 0)
                            rs = ss;
                    }
                }
                q.push(t2);
            }
        }
    }
}
int main(void) {
    pp[0] = 1;
    for(int i = 1; i <= 33; i++)
        pp[i] = pp[i - 1] * 4;
    int T;
    scanf("%d", &T);
    for(int pt = 1; pt <= T; pt++) {
        scanf("%s%s", s1, s2);
        bool fg = false;
        for(int i = 0; i < 9; i++) {
            if(s1[i] != s2[i])
                fg = true;
        }
        //printf("s1 = %s,s2 = %s\n",s1,s2);
        if(fg == false) {
            printf("Case %d: 0\n\n", pt);
            continue;
        }
        min_step = inf;
        printf("Case %d: ", pt);
        bfs();
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值