1085: [SCOI2005] Knight Spirit time limit: ten Sec Memory Limit: 162 MB
Submit: 953 Solved: 500
[Submit] [Status] Description
On a 5x5 chessboard there are 12 white knights and 12 Black Knights, and there is a vacancy. At any time a knight can follow the Knight's Walk (it can go to and its horizontal axis difference is 1, the ordinate difference is 2 or the horizontal axis difference is 2, the ordinate difference is 1 lattice) moved to the vacancy. Given an initial chessboard, how can it be moved to become the following target board: In order to demonstrate chivalry, they must complete the task with a minimum number of steps.
Input
The first line has a positive integer T (t<=10), which represents a total of n sets of data. Next there is the T-5x5 matrix, 0 for the White Knight, 1 for the Black Knight, * for the empty space. There are no blank lines between the two sets of data.
Output
Output one row for each set of data. If you can reach the target State within 15 steps (including 15 steps), the output steps, otherwise output-1.
Sample Input2
10110
01*11
10111
01001
00000
01011
110*1
01110
01010
00100
Sample Output7
-1
A * algorithm.
Introduction to A * algorithm.
A * algorithm is equivalent to adding a pruning to a normal search:
F[i]=g[i]+h[i],f is the estimation function, G is the optimal solution from the starting point to I (known), H is the estimated value of I to the end point (estimate), if f is greater than the optimal solution then we can prune it.
"Bzoj 1085" [SCOI2005] Knight Spirit