gpt4 book ai didi

java - 计算曼哈顿距离

转载 作者:搜寻专家 更新时间:2023-10-30 21:36:22 27 4
gpt4 key购买 nike

我正在 Java 二维数组 int[][] 状态下实现 NxN 拼图。我需要按以下方式使用曼哈顿启发式:

             the sum of the vertical and horizontal distances from 
the current node to the goal node/tile

+(plus)

the number of moves to reach the goal node from the initial position

目前我不知道如何走得更远。我是使用 2D 数组进行益智游戏编程的初学者,因此无法理解某些概念。如何用 Java 编写这段代码?

最佳答案

这更像是一道数学题,但无论如何曼哈顿距离是水平距离和垂直距离的绝对值之和

int distance = Math.abs(x1-x0) + Math.abs(y1-y0);

更多信息:http://en.wikipedia.org/wiki/Taxicab_geometry

关于java - 计算曼哈顿距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8224470/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com