Problem D
Knights in FEN
Input:Standard input
Output:Standard output
Time Limit:10 seconds
There are black and white knights on a 5 by 5 chessboard. there are twelve of each color, and there is one square that is empty. at any time, a knight can move into an empty square as long as it moves like a knight in normal chess (what else did you have CT ?).
Given an initial position of the board, the question is: what is the minimum number of moves in which we can reach the final position which is:
Input <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kpha + ratio = "206" height = "207" src = "http://www.bkjia.com/uploadfile/Collfiles/20140107/20140107095121228.jpg" alt = "\">
Output
For each set your task is to find the minimum number of moves leading from the starting input configuration to the final one. If that number is bigger than 10, then output one line stating
Unsolvable in less than 11 move(s).
Otherwise output one line stating
Solvable inNMove (s ).
WhereN<= 10.
The output for each set is produced in a single line as shown in the sample output.
Sample Input2
01011
110 1
01110
01010
00100
10110
01 11
10111
01001
00000
Sample OutputUnsolvable in less than 11 move(s).
Solvable in 7 move(s).
(Problem Setter: Piotr Rudnicki, University of Alberta, Canada)
"A man is as great as his dreams ."
A status chart is provided to move the server guard to the initial state.
Typical Implicit Graph Search problems: BFS search + hash. Hash is implemented using set. In addition, it should be noted that the server guard in chess uses Japanese characters.
# Include
# Include
# Include
# Include
Using namespace std; typedef int state [25]; const int maxn = 5000000; const int dx [] = {,-1,-2,-2, -1}; const int dy [] = {-2,-, 1,-1,-2}; state st [maxn]; int dist [maxn]; int front, rear, s; set
Vis; state goal = {, 0, 0}; int try_to_insert (int s) // hash function {int v = 0; for (int I = 0; I <25; I ++) v = v * 2 + st [s] [I]; if (vis. count (v) return 0; vis. insert (v); return 1;} int bfs () {front = 1, rear = 2; vis. clear (); while (front
10) return-1; // pruning. if (memcmp (goal, p, sizeof (p) = 0) return front; int I, j, x, y; for (I = 0; I <25; I ++) if (st [front] [I] = 2) break; int z = I; x = I/5, y = I % 5; for (I = 0; I <8; I ++) {int newx = x + dx [I]; int newy = y + dy [I]; if (newx> = 0 & newx <5 & newy> = 0 & newy <5) {state & u = st [rear]; memcpy (& u, & p, sizeof (p )); u [x * 5 + y] = u [newx * 5 + newy]; u [newx * 5 + newy] = 2; dist [rear] = dist [front] + 1; if (try_to_insert (rear) rear ++;} fr Ont ++ }}int main () {cin> s; string str; getline (cin, str); while (s --) {memset (dist, 0, sizeof (dist); int I, j; for (I = 0; I <5; I ++) {getline (cin, str); for (j = 0; j <5; j ++) {if (str [j]! = '') St [1] [I * 5 + j] = str [j]-'0 '; else st [1] [I * 5 + j] = 2 ;}} int d = bfs (); if (d <= 0) cout <"Unsolvable in less than 11 move (s ). "<