对方形矩阵进行排序

Sort a square matrix

本文关键字:排序 方形矩      更新时间:2023-10-16

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

有一个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

我已经完成了第三步。然而,我不知道步骤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  -

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

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

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

最好使用一些AI算法,如A*和曼哈顿距离启发式算法。

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

对于曼哈顿距离启发式,请阅读此处