"Bzoj 1085" [SCOI2005] Knight Spirit

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.