2

我正在解决一个难题,如下所示。

有一个 5x5 矩阵,其中一个元素为“-”,所有其他元素为整数。
我可以用“-”直线(不是对角线)交换任何元素。
最后,我必须对矩阵进行排序。

这些是我遵循的步骤:

1) Receive user input for 5x5 matrix
2) Locate the position of "-"
3) Find the eligible candidates to be swapped with "-"
4) Apply some algorithm and find the most eligible candidate
5) Swap the element with "-"
6) Repeat the steps 3-5 until matrix is sorted

我已经完成了第 3 步。但是我不知道第 4 步要应用什么逻辑。有人可以给出一些想法,如何找到最符合条件的候选人?

例子

Input Matrix

17   7   9   18   3
15   11  1   12   14
2    -   4   21   24
5    19  6   18   8
10   13  16  19   20

Eligible candidates to swap with "-" are 11,2,4,19


Sorted matrix

1   2   3   4   5
6   7   8   9   10
11  12  13  14  15
16  17  18  19  20
21  22  23  24  -
4

2 回答 2

4

这不是最简单的任务。这里有两个链接:

http://en.wikipedia.org/wiki/N-puzzle#Solvability

http://cseweb.ucsd.edu/~ccalabro/essays/15_puzzle.pdf

于 2012-07-15T09:23:04.917 回答
0

最好使用带有曼哈顿距离启发式的 A* 等 AI 算法。

对于 A* 算法,请阅读此处

对于曼哈顿距离启发式阅读here

于 2012-07-15T13:19:02.030 回答