UVALive 6665 Dragonas Cruller[通俗易懂]

UVALive 6665 Dragonas Cruller

大家好,又见面了,我是全栈君。

题目链接:https://icpcarchive.ecs.baylor.edu/external/66/6665.pdf

题目大意:

有一个3 * 3 的格子:

UVALive 6665 Dragonas Cruller[通俗易懂]

UVALive 6665 Dragonas Cruller[通俗易懂]

每个格子上面的数字能够朝上下左右四个方向移动。假设移出范围。则到与其边界上字母相应的还有一边。

例如以下图所看到的:

UVALive 6665 Dragonas Cruller[通俗易懂]

UVALive 6665 Dragonas Cruller[通俗易懂]

空白部分分别向上下左右移动之后的情况。

如今。给你左右移动的费用ch,上下移动cv。给你一个初始状态(9个数字,当中0代表该空格为空),一个结束状态,问从初始状态移动到结束状态的最小花费。

解题思路:

事实上这道题想法非常easy,简单的bfs + 优先队列。主要是细节处理方面比較麻烦。

把上述九宫格变为一个序列,会发现状态最多就9!种,所以状态能够依照序列的排序离散化。详细參考代码。

然后改成序列之后,会发现左右移动事实上就是 i + 1。 i – 1的操作。上下移动是 i + 3, i – 3 的操作。

代码:

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
#define maxn 1000
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, n) for (int i = 0; i < n; i++)
#define debug puts("===============")
const int N = 362881;
typedef long long ll;
using namespace std;
int ch, cv;
int fac[10];
struct node {
    int x, w;
    node(int _x, int _w) {
        x = _x, w = _w;
    }
    bool operator < (const node & T) const {
        return w > T.w;
    }
};
void Atoint(int* a, int &x) {
    int i, j, y;
    x = 0;
    for (i = 0; i < 9; i++) {
        y = a[i];
        for (j = 0; j < i; j++)
            if (a[j] < a[i]) y--;
        x += fac[8 - i] * y;
    }
}
void inttoA(int x, int* a) {
    int has[10] = {0};
    int i, j, y;
    for (i = 0; i < 9; i++) {
        y = x / fac[8 - i];
        for (j = 0; j < 9; j++) if (!has[j]) {
            if (y == 0) break;
            y--;
        }
        a[i] = j, has[j] = 1, x %= fac[8 - i];
    }
}
int st, ed;
priority_queue<node> q;
int d[N], vis[N], a[10], b[10];
void update(int x, int w) {
    if (!vis[x] && d[x] > w) {
        d[x] = w, q.push(node(x, w));
    }
}
void work() {
    Atoint(a, st), Atoint(b, ed);
    while(!q.empty()) q.pop();
    memset(d, 0x7F, sizeof(d));
    memset(vis, 0, sizeof(vis));
    d[st] = 0;
    q.push(node(st, 0));
    int x, w, i, y;
    while(!q.empty()) {
        x = q.top().x, w = q.top().w, q.pop();
        if (vis[x]) continue;
        vis[x] = 1;
        if (x == ed) {
            printf("%d\n", w);
            break;
        }
        inttoA(x, a);
        for (i = 0; i < 9; i++) if (!a[i]) break;
        swap(a[i], a[(i + 1) % 9]);
        Atoint(a, y);
        update(y, w + ch);
        swap(a[i], a[(i + 1) % 9]);
        swap(a[i], a[(i + 8) % 9]);
        Atoint(a, y);
        update(y, w + ch);
        swap(a[i], a[(i + 8) % 9]);
        swap(a[i], a[(i + 3) % 9]);
        Atoint(a, y);
        update(y, w + cv);
        swap(a[i], a[(i + 3) % 9]);
        swap(a[i], a[(i + 6) % 9]);
        Atoint(a, y);
        update(y, w + cv);
    }
}
int main () {
    //freopen("1.txt", "r", stdin);
    fac[0] = 1;
    for (int i = 1; i < 10; i++) fac[i] = fac[i - 1] * i;
    while(scanf("%d%d", &ch, &cv), ch || cv) {
        for (int i = 0; i < 9; i++) scanf("%d", a + i);
        for (int i = 0; i < 9; i++) scanf("%d", b + i);
        work();
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115862.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • OJ错误提示类型

    OJ错误提示类型答案正确(Acepted,AC)  恭喜你!所提交的代码通过了数据!这个评测结果应该是大家最喜欢见到的,也非常好理解。如果是单点测试,那么没通过一组数据,就会返回一个Accepted;如果是多点测试,那么只有当通过了所有数据时,才会返回Accepted。编译错误(CompileError,CE)  很显然,如果代码没有办法通过编译,那么就会返回CompileError。这是要…

    2022年6月24日
    24
  • 明翰英国硕士常见词汇与固定搭配V1.2(持续更新)

    明翰英国硕士常见词汇与固定搭配V1.2(持续更新)下面的所有词汇与例句都是在英国留学期间,用到的、听到的、见到的,都是学校、教职人员、同学使用过的,对于高频词汇一定要掌握听说读写。

    2022年10月2日
    2
  • 使用 Java DB (Derby) 数据库

    使用 Java DB (Derby) 数据库使用JavaDB(Derby)数据库https://netbeans.org/kb/docs/ide/java-db_zh_CN.html本文档说明了如何在NetBeansIDE中设置与JavaDB数据库的连接。在建立连接之后,即可开始在IDE中使用该数据库,您可以执行的操作包括创建表、用数据填充表、运行SQL语句和查询等。…

    2022年7月8日
    23
  • c语言 bnf,c语言的bnf

    c语言 bnf,c语言的bnftokenint constchar constfloat constidstrin const translation unit external decl translation unitexternal decl external decl function definition decl function definition decl

    2025年12月15日
    2
  • eclipse自动补全变量快捷键_java代码提示快捷键

    eclipse自动补全变量快捷键_java代码提示快捷键(1)将鼠标光标移到代码末尾处,按下【ctrl+1】,会弹出如下所示选择项。(2)然后选择第一个(Assignstatementtonewlocalvariable),则会自动补全代码返回值,如下所示;List<FixedVo>fixedList=ConfigManager.getInstance().getFixedList(BigClassT…

    2022年10月15日
    1
  • 项目管理-5大过程组-10大知识领域-47过程

    项目管理五大过程组:1、启动过程组:获得授权,定义一个新项目或现有项目的一个新阶段,正式开始该项目或阶段的一组过程。2、规划过程组:明确项目范围,优化目标,为实现目标而制定行动方案的一组过程。3、执行过程组:完成项目管理计划中确定的工作以实现项目目标的一组过程。4、监控过程组:跟踪、审查和调整项目进展与绩效,识别必要的计划变更并启动相应变更的一组过程。5、收尾过程组:为完结所有过程组的…

    2022年4月13日
    51

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号