这些数组表示8-puzzle 的状态。为此类难题实现求解器的一种方法归结为最短路径搜索(有关更多信息,请参阅How do you solve the 15-puzzle with A-Star or Dijkstra's Algorithm?)。特别是对于A* algorithm,您将需要一个admissible heuristic,在这种情况下,它可以由当前数组中图块位置与其在目标状态中的位置之间的Taxicab- or Manhattan distances 之和给出。使用这种启发式方法是因为它定义了实际所需移动次数的下限。正如评论中提到的:实际移动次数必须至少与瓷砖之间的纯几何(曼哈顿)距离一样大。
实现这一点并不难。确切的实现将取决于董事会状态的表示。也可以为此使用二维数组。但是使用一维数组有一个很好的优势:找到瓷砖位置之间的对应关系很简单。
一般情况下,当你在当前状态的(sx,sy)位置找到某个值v时,你必须搜索这个值在目标中的位置(gx,gy)状态,以便您可以计算两者之间的距离。
但由于数组只包含从 0 到 array.length-1 的数字,因此您可以计算目标状态的“逆”,并使用它直接查找数组中值的位置(索引)。
根据 cmets 中的要求添加了示例和详细信息:
例如,考虑谜题中的值6,它出现在位置(1,2)。现在你必须找到6 在目标状态下的位置。在您的示例中,这是(2,2),或索引8。您可以通过在目标状态数组中搜索值 6 来找到该位置。但是,当您对每个值执行此操作时,这将是 O(n*n) - 即效率低下。对于给定的目标状态,invert 方法将返回 [2,1,0,3,4,5,8,7,6]。此数组的元素i 是值 i 在原始数组中的位置。例如,您可以在索引6(您要查找的值)处访问此数组,并在那里找到值8。而8 正是值6 在目标数组中的索引。所以在目标数组中找到某个值的索引可以通过简单的查找来完成(即无需搜索整个数组)。
根据一维数组中的索引和棋盘的大小,您可以计算出(x,y) 坐标,然后使用这些坐标来计算距离。
这是一个例子:
public class TaxicabPuzzle
{
public static void main(String[] args)
{
int[] myPuzzle = {
1,3,4,
0,2,5,
8,6,7
};
int[] goalPuzzle = {
2,1,0,
3,4,5,
8,7,6
};
int distanceBound = computeDistanceBound(myPuzzle, goalPuzzle, 3);
System.out.println("Distance bound: "+distanceBound);
}
private static int computeDistanceBound(
int state[], int goal[], int sizeX)
{
int inverseGoal[] = invert(goal);
int distance = 0;
for (int i=0; i<state.length; i++)
{
int goalIndex = inverseGoal[state[i]];
distance += distance(i, goalIndex, sizeX);
}
return distance;
}
// For two indices in an array that represents a 2D array in row-major
// order, compute the manhattan distance between the positions that are
// defined by these indices
private static int distance(int index0, int index1, int sizeX)
{
int x0 = index0 % sizeX;
int y0 = index0 / sizeX;
int x1 = index1 % sizeX;
int y1 = index1 / sizeX;
return Math.abs(x1 - x0) + Math.abs(y1 - y0);
}
// Given an array containing the values 0...array.length-1, this
// method returns an array that contains at index 'i' the index
// that 'i' had in the given array
private static int[] invert(int array[])
{
int result[] = array.clone();
for (int i=0; i<array.length; i++)
{
result[array[i]] = i;
}
return result;
}
}