2012-07-15 41 views
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 - 

回答

0

這是更好地利用一些人工智能算法像曼哈頓距離啓發式A *。

對於A *算法讀here

對於曼哈頓距離啓發式讀here