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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pycharm无法连接服务器_pycharm部署项目到服务器

    pycharm无法连接服务器_pycharm部署项目到服务器Pycharm连接服务器Pycharm连接服务器是最为常用的内容,本文记录了Pycharm连接服务器的方法。BlueStragglers分享技术成长的乐趣目录Pycharm连接服务器1.操作步骤1.1创建连接1.2新建项目1.3运行配置2.常见问题2.1不显示Package1.操作步骤1.1创建连接首先,需要创建连接。进入Tools→Deployment→Configuration,打开配置页面。在Deployment页面的Connection

    2022年8月29日
    4
  • python的缩进规则具体是什么_python语言中的缩进

    python的缩进规则具体是什么_python语言中的缩进python的缩进规则有哪些发布时间:2020-09-2315:18:50来源:亿速云阅读:70作者:Leah本篇文章给大家分享的是有关python的缩进规则有哪些,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。一般的语言都是通过{}或end来作为代码块的标记,而Python则是通过缩进来识别代码块的。对于Python的这种“缩进”风…

    2022年10月13日
    5
  • 贪吃蛇程序代码python_python 贪吃蛇

    贪吃蛇程序代码python_python 贪吃蛇Python贪吃蛇源代码Python代码狂人Python代码大全程序运行截图如下:importpygameaspgfromrandomimportrandintimportsysfrompygame.localsimport*FPS=6#画面帧数,代表蛇的移动速率window_width=600window_height=500cellsize=20c…

    2022年8月10日
    11
  • dropdown和dropdownlist_list的clear方法

    dropdown和dropdownlist_list的clear方法DropDownList的详细使用方法,希望对大家有用

    2025年8月28日
    5
  • Ubuntu下查看cuda版本的两种方法

    Ubuntu下查看cuda版本的两种方法参考资料:https://blog.csdn.net/qq_16525279/article/details/80662217  在安装Pytorch等深度学习框架的时候,我们往往需要检查是否和cuda版本匹配。在Ubuntu系统下查看cuda版本的方法,我发现有两个。方法一:  比较常用的一个方法是使用如下命令:cat/usr/local/cuda/version.txt使用该命令的效果如下图所示:方法一使用效果方法二:  方法一主要是依据cuda安装时保存的关于版本的txt文件。但

    2022年6月12日
    205
  • Using ZipLib to create a Zip File in C#

    Using ZipLib to create a Zip File in C#System.IO.Compression是.Net2.0里与压缩有关的命名空间,但是使用起来并不是很方便。使用第3方库ziplib可以很方便地进行压缩类的操作。从[1]下载动态库,然后在工程里AddReference,把ICSharpCode.SharpZipLib.dll加进去。在代码来创建一个zip包的例子如下(摘自ziplibsamplecode)…

    2022年7月26日
    6

发表回复

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

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