冰滑拼图寻路

2024-04-06

我对这个有点模糊的标题表示歉意,我不确定你会如何称呼这个谜题。

我正在制作一种路径查找方法来查找移动次数最少的路线,而不是行驶的距离。

游戏规则很简单,你必须从橙色方块移动到绿色方块,但你只能沿直线移动,并且不能停止沿该方向移动,直到碰到边界(无论是竞技场的墙壁还是障碍物),就像他们在冰上滑行一样。

示例地图,除非我弄错了,否则是所需的路径(8步)

竞技场.java:https://gist.github.com/CalebWhiting/3a6680d40610829b1b6d https://gist.github.com/CalebWhiting/3a6680d40610829b1b6d

ArenaTest.java:https://gist.github.com/CalebWhiting/9a4767508831ea5dc0da https://gist.github.com/CalebWhiting/9a4767508831ea5dc0da

我假设这最好用 Dijkstras 或 A* 路径查找算法来处理,但是我不仅对这些算法没有太多经验,而且也不知道如何定义路径规则。

感谢您提前提供的任何帮助。


这是我的解决方案(Java),以防有人仍然感兴趣。正如@tobias_k 在他的建议中所建议的comment https://stackoverflow.com/questions/29170368/ice-sliding-puzzle-path-finding#comment46559110_29170368上面确实BFS是要走的路:

import java.util.LinkedList;

public class PokemonIceCave {
    public static void main(String[] args) {
        int[][] iceCave1 = {
                {0, 0, 0, 1, 0},
                {0, 0, 0, 0, 1},
                {0, 1, 1, 0, 0},
                {0, 1, 0, 0, 1},
                {0, 0, 0, 1, 0}
        };
        System.out.println(solve(iceCave1, 0, 0, 2, 4));
        System.out.println();

        int[][] iceCave2 = {
                {0, 0, 0, 1, 0},
                {0, 0, 0, 0, 1},
                {0, 1, 1, 0, 0},
                {0, 1, 0, 0, 1},
                {0, 0, 0, 1, 0},
                {0, 0, 0, 0, 0}
        };
        System.out.println(solve(iceCave2, 0, 0, 2, 5));
    }

    public static int solve(int[][] iceCave, int startX, int startY, int endX, int endY) {
        Point startPoint = new Point(startX, startY);

        LinkedList<Point> queue = new LinkedList<>();
        Point[][] iceCaveColors = new Point[iceCave.length][iceCave[0].length];

        queue.addLast(new Point(0, 0));
        iceCaveColors[startY][startX] = startPoint;

        while (queue.size() != 0) {
            Point currPos = queue.pollFirst();
            System.out.println(currPos);
            // traverse adjacent nodes while sliding on the ice
            for (Direction dir : Direction.values()) {
                Point nextPos = move(iceCave, iceCaveColors, currPos, dir);
                System.out.println("\t" + nextPos);
                if (nextPos != null) {
                    queue.addLast(nextPos);
                    iceCaveColors[nextPos.getY()][nextPos.getX()] = new Point(currPos.getX(), currPos.getY());
                    if (nextPos.getY() == endY && nextPos.getX() == endX) {
                        // we found the end point
                        Point tmp = currPos;  // if we start from nextPos we will count one too many edges
                        int count = 0;
                        while (tmp != startPoint) {
                            count++;
                            tmp = iceCaveColors[tmp.getY()][tmp.getX()];
                        }
                        return count;
                    }
                }
            }
            System.out.println();
        }
        return -1;
    }

    public static Point move(int[][] iceCave, Point[][] iceCaveColors, Point currPos, Direction dir) {
        int x = currPos.getX();
        int y = currPos.getY();

        int diffX = (dir == Direction.LEFT ? -1 : (dir == Direction.RIGHT ? 1 : 0));
        int diffY = (dir == Direction.UP ? -1 : (dir == Direction.DOWN ? 1 : 0));

        int i = 1;
        while (x + i * diffX >= 0
                && x + i * diffX < iceCave[0].length
                && y + i * diffY >= 0
                && y + i * diffY < iceCave.length
                && iceCave[y + i * diffY][x + i * diffX] != 1) {
            i++;
        }

        i--;  // reverse the last step

        if (iceCaveColors[y + i * diffY][x + i * diffX] != null) {
            // we've already seen this point
            return null;
        }

        return new Point(x + i * diffX, y + i * diffY);
    }

    public static class Point {
        int x;
        int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        @Override
        public String toString() {
            return "Point{" +
                    "x=" + x +
                    ", y=" + y +
                    '}';
        }
    }

    public enum Direction {
        LEFT,
        RIGHT,
        UP,
        DOWN
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

冰滑拼图寻路 的相关文章

随机推荐

  • VBA:单击元素后无法更改图像控制图片

    Setup 我有一个带有图像控件元素的 Excel VBA 用户窗体 当单击表单中的按钮时 图像控件的Picture使用以下代码设置 更新源 Set MyForm imgControl Picture LoadPicture pathToF
  • Angular 5 - 我应该使用哪个来向后导航 - href 或 location.back()?

    我有一个应用程序 可以导航到一个无法前进的页面 想象一下 单击电视指南中的某个节目以打开包含该节目详细信息的页面 显然您会想要返回到电视指南 对于返回电视指南的按钮 我有一个 a tag 最建议的用例是 Using href guide o
  • 按日期和状态获取订单 woocommerce

    我需要获取 woocommerce 中的订单列表 包含开始日期 最终日期和状态 我尝试使用一些技术 如所描述的迈克 乔利 http develop woothemes com woocommerce 2014 08 wc 2 2 order
  • 取子集时保留 R 对象的类属性

    我有一个名为gene table的R对象 它有类foo 现在我将这个gene table子集为 gene data gene table 1 100 1 5 然而 当我打电话时class gene data 它不再属于类foo 但相反 它有
  • 为什么Python3.0中sort/sorted去掉了cmp参数?

    from 蟒蛇维基 https wiki python org moin HowTo Sorting The Old Way Using the cmp Parameter In Py3 0 the cmp parameter was re
  • 正式支持 MonoTouch 的 NoSQL 数据库

    我无法通过设备上的本地数据库找到正式支持 MonoTouch 的 NoSQL 数据库 如果是的话 有人可以在这里提供他们的名单吗 根据http nosql database org http nosql database org 有siaq
  • 尝试在jpa中删除时出现并发问题

    我有 2 个 EAR 1 EAR 和 2 EAR 这些有 websearvices 和其他代码 现在我有 1 个项目 DB prj 用于与数据库交互 现在所有项目 1 EAR 2 EAR DB prj 在各自的 meta inf 文件夹中都
  • 我的域正在 AWS Certificate Manager 中等待验证

    使用 AWS Certificate Manager 配置 mydomain com 并在等待验证中显示超过一天 即使 CNAME 记录已发布到该域名下的 AWS Route53 也是如此 一切似乎都合适 但尚不清楚 为什么域名没有得到验证
  • 404 找不到页面 codeigniter url

    我是一个使用 codeigniter 的初学者 我正在使用以下网址 http localhost ci index php shopcart http localhost ci index php shopcart 访问控制器 我收到错误
  • 验证网站所有权的简化方法(使用 JavaScript?)

    我有一个 Rails 应用程序 它要求用户在提交来自该网站的链接之前验证他们是否拥有该网站 我已经实现了一个网站验证系统 由于给出的答案 该系统可以正常工作我几个月前提出的一个问题 https stackoverflow com quest
  • Json 到 C# 对象处理动态属性

    我正在尝试在 c 对象中实现 json 结构 并且我正在尝试了解如何根据类型使用正确的对象 例如 public class RootObject public string name get set public Content conte
  • 有什么方法可以绘制印度地图吗?

    我正在尝试使用plotly 绘制印度地图 但无法找到方法来做到这一点 下面是我在美国尝试过的代码 import pandas as pd df sample pd read csv https raw githubusercontent c
  • 软件中显示过多“皮肤”检测

    我正在构建一个 ASP NET 网站 用户可以在其中上传自己的照片 每天可能会上传数千张照片 我的老板问过几次的一件事是 是否有任何方法可以检测到是否有任何照片显示太多 皮肤 并在编辑做出最终决定之前自动将这些照片标记为 仅限成人 最好的选
  • 如何确保 div 和同位素没有空白

    我正在尝试创建一个网格布局http www gablabelle com http www gablabelle com 我有多个漂浮着同位素的 div 我想知道为什么有一些空白空间以及为什么漂浮的 div 没有填充间隙 与这里同样的问题
  • CSS 最小宽度 div 不强制其容器达到预期的正确尺寸

    我有一个需要最小宽度的 DIV 我无法使用 CSSmin width因为它不是跨浏览器的 我创建了一个具有设定宽度的内部 div 当浏览器小于这个内部 div 时 我会按预期得到一个滚动条 问题是外部 div 不断缩小 小于内部 div S
  • 如何从 github 手动或离线安装 R 包

    我尝试从 github 下载 tsdyn 包 它尚未在 cran 上更新 但我的代理阻止我连接到 github library devtools install github MatthieuStigler tsDyn ref Dev94
  • 在 React 中将函数向下传递给子组件

    我试图将一个函数传递给 React 中的子组件 如果我将该函数放在 ES6 类中 我就可以访问this props dispatch 但无权访问mapStateToProps 相反 当我在 ES6 类之外定义函数时 我似乎可以访问该函数 但
  • 如何使用office.js获取office应用程序版本值

    Office 应用程序版本可以在清单文件中提及 如下所示
  • C++11 中一个表达式中同一变量的双重赋值

    C 11 标准 http www open std org jtc1 sc22 wg21 docs papers 2012 n3337 pdf 5 17 expr ass 指出 在所有情况下 分配都是在值计算之后排序的 右操作数和左操作数的
  • 冰滑拼图寻路

    我对这个有点模糊的标题表示歉意 我不确定你会如何称呼这个谜题 我正在制作一种路径查找方法来查找移动次数最少的路线 而不是行驶的距离 游戏规则很简单 你必须从橙色方块移动到绿色方块 但你只能沿直线移动 并且不能停止沿该方向移动 直到碰到边界