对方形矩阵进行排序
Sort a square matrix
我正在解一个谜题,如下所示。
有一个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*算法,请阅读此处
对于曼哈顿距离启发式,请阅读此处
相关文章:
- 二叉排序树无法编译
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- C++选择排序算法中的逻辑错误
- 使用C++程序合并排序没有得到正确的输出
- 计算排序向量的向量中唯一值的计数
- 排序算法c++
- 使用2个键的cpp-stl::优先级队列排序不正确
- 将结构向量排序为子组
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 如何对点云数据进行排序
- 对字符串进行排序时,在c++中处理sort()
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- 下面是排序算法O(n)吗
- std::sort()函数无法对向量的一部分进行排序
- shell排序中的交换和比较
- clang格式:禁用排序包含
- 显示错误输出的简单数组排序程序
- 为什么我的排序算法会更改数组值
- 试图在c++中对数字列表进行排序
- 对方形矩阵进行排序