JS:给定二维数组中的一个点和一个距离,哪些坐标是可移动的?
给定一个任意大小的二维数组,如下所示:
var board = [
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0]
];
...以及该数组中给定的 [y][x] 点,例如:
board[3][4]
...以及它可以移动的给定数量的空间(上/下/左/右) ,不是对角线),例如:
var distance = 3;
...函数如何循环遍历 2D 数组并创建仅包含可能经过的那些坐标的列表?
(这是数组中给定坐标 (*) 以及周围可行驶坐标的直观示例。)
0 0 0 0 0 0 0 0
0 0 0 3 0 0 0 0
0 0 3 2 3 0 0 0
0 3 2 1 2 3 0 0
3 2 1 * 1 2 3 0
0 3 2 1 2 3 0 0
0 0 3 2 3 0 0 0
0 0 0 3 0 0 0 0
参考:JS:如何通过算法突出显示x/y坐标的菱形选择? (我之前问过这个问题,但我不明白如何输入坐标并接收坐标列表)
Given a 2D array of any size like so:
var board = [
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0]
];
...and a given [y][x] point in that array, such as:
board[3][4]
...and a given number of spaces it can travel (up/down/left/right, not diagonally), like:
var distance = 3;
...how would a function loop through the 2D array and create a list of only those coordinates that may be traveled?
(Here's a visual example of the given coordinate (*) in the array, and the surrounding travelable coordinates.)
0 0 0 0 0 0 0 0
0 0 0 3 0 0 0 0
0 0 3 2 3 0 0 0
0 3 2 1 2 3 0 0
3 2 1 * 1 2 3 0
0 3 2 1 2 3 0 0
0 0 3 2 3 0 0 0
0 0 0 3 0 0 0 0
Reference: JS: how to algorithmically highlight a diamond-shaped selection of x/y coordinates?
(I asked this question before, but I can't understand how to input a coordinate and receive a list of coordinates)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
迭代所有坐标(如果面积很大,则迭代子集 xd,yd ... x+d,y+d)。
对于其中的每个字段,计算距离 - 在您的情况下为
dx - dy
- 每当您找到距离 > 的点时0,用它做任何你想做的事。否则,忽略它。就是这样!与洪水填充方法相比,您可以获得简单的代码,并且没有额外的查找表的开销。
Iterate over all coordinates (or a subset
x-d,y-d
...x+d,y+d
if the area is big).For each field of those, calculate the distance - in your case as
dx - dy
- and whenever you find a point with the distance > 0, do anything you want with it. Otherwise, ignore it. That's it!Compared to a flood-fill approach, you get simple code and no overhead of additinal lookup tables.
这是我能想到的最简单的解决方案,它涉及从上到下、从左到右的工作,仅迭代允许移动的坐标,所以它应该非常快:
给你一个更好的主意,我整理了一个演示,允许您调整所有不同的变量,例如板尺寸、距离等。
工作演示:http://jsfiddle.net/AndyE/fWDHy/2/
This is the simplest solution I could think of, it involves working from top to bottom and left to right, iterating over only the co-ordinates that are permissible moves so it should be pretty fast:
To give you a better idea, I threw together a demo that allows you to adjust all the different variables, e.g. board size, distance, etc.
Working demo: http://jsfiddle.net/AndyE/fWDHy/2/
使用递归以及访问过的链接的列表/散列。迈出一步,将你旅行的能力降低一级,并传递你所看到的清单。将您当前的位置添加到访问过的地点列表中。向每个方向走一步(使用递归),传递一个比给定的值少 1 的“左步”值。
这是一个几乎有效的答案;唯一的问题是,即使经过很长的路才到达某个单元,它也永远不会重新访问该单元。您可以通过 广度优先搜索 或检测访问的单元格是否是通过比您将要采取的步骤更多的步骤来达到目标。
Use recursion along with a list/hash of visited links. Take a step, reduce your ability to travel by one and pass along the list of what you've seen. Add your current location to the list of visited spots. Go in each direction one step (using recursion), passing along a 'steps left' value that is one less than you were given.
Here's an answer that almost works; the only problem is that it never re-visits a cell even if the long way was used to get there. You could overcome this either via a breadth-first search or by detecting if the visited cell was reached via more steps than you are about to take to get there.