N个难题在2维数组

N个难题在2维数组

本文介绍了解决N×N个难题在2维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解决N×N个谜中的Java 2D阵列( INT状态[] [] ),需要帮助,因为我坚持,不能走得更远。

I am solving NxN puzzle in Java 2D array (int state[][]) and need help because am stuck and can't go further.

现在用的是曼哈顿距离heureustic。这个问题我现在是如何产生的(code)节点的接班人。我已经看到了一个节点的继任者(邻居)产生的净例子不胜枚举,但他们都在1Dimensional阵列。我找不到,教导产生一个节点的接班人二维阵列的任何教程。

Am using the Manhattan distance heureustic. The problem i have now is how to generate (code) the successors of a node. I have seen numerous examples on the net how a node's successors (neighbours) are generated but they are all in 1Dimensional array. I can't find any tutorial that teaches generating the successors of a node in 2D arrays.

我要问,如果有人可以帮助我了解如何我可以code生成一个节点的接班人。节点可以移动到空的空间,以左,右,向下或向上。电路板的尺寸为N×N个。所以我想知道我可以code这一点。

I am asking if someone can help me to find out how i can code to generate the successors of a node. A node can move to the empty space to the LEFT, RIGHT, DOWN or UP. The size of the board is NxN. So i want to know how i can code this.

请这是我第一次使用二维数组编程游戏像这些puzzels这样做的很好,给我你的答案的一些解释,这样我可以把它理解得很好。

Please it is my first time using 2D arrays to program game like these puzzels so do well and give me some explanations of your answers, so that i can understand it very well.

谢谢大家

推荐答案

大概是code有定义

UP as ( i + width * (j-1))
DOWN as ( i + width * (j+1))
LEFT as ( i - 1 + width * j)
RIGHT as (i + 1 + width *j)

您需要定义这些作为(I,J-1),分别为(I,J + 1),第(i-1,j)的第(i + 1,j)的。

You need to define these as (i, j-1), (i, j+1), (i-1, j), (i+1, j) respectively.

编辑:

在一维数组,你需要扁平化元素的索引。如果元素是i列,j行,平面地址是(j *宽[指跳Ĵ行] + I [意味着跳过我的项目在该行中])。如果你有一个二维数组,那么你可以简单的使用数组[I] [J],而不是一个平面地址。

In a 1D array, you need to flatten the element index. If the element is at column i, row j, flat address would be ( j*width [meaning skip j rows] + i [meaning skip i items in this row]). If you have a 2D array then you can simple use array[i][j] instead of a flat address.

这篇关于解决N×N个难题在2维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:27