Question: Implicit Graph Search
Given a 5x5 chessboard with 12 black pawns, 12 white pawns, and a space on it, the positions of these pawns are not fixed at the very beginning, we need to find the minimum number of steps to convert the board into the last one, and finally output the result.
Solution: we can easily find the minimum number of steps in bfs, but this problem can also be solved using dfs + backtracking. First, we open a Fin array to save the last position, and then we perform a one-to-one deep search and backtracking on the eight directions around the space, each time we perform a search, we can determine whether the search is current and the final position (this is determined by the judge function). If so, we can determine whether the ans can be updated. We can also optimize it, that is, the search for Tans> 10 is definitely useless, so we can simply remove it. In addition, each point of this question can be repeated because the points around spaces may be different at a time, so we do not need to open the vis array to mark whether the points have passed.
Code:
[Cpp]
# Include <algorithm>
# Include <iostream>
# Include <cstring>
# Include <string>
# Include <vector>
# Include <cstdio>
# Include <stack>
# Include <queue>
Using namespace std;
Int Fin [5] [5] = {// Save the final result
{1, 1, 1, 1, 1 },
{0, 1, 1, 1, 1 },
{0, 0,-1, 1, 1 },
{0, 0, 0, 0, 1 },
{0, 0, 0, 0, 0 },
};
Int dir [8] [2] = {// direction Array
{-2,-1 },
{-2, 1 },
{-1, 2 },
{1, 2 },
{2, 1 },
{2,-1 },
{1,-2 },
{-1,-2}
};
Int G [5] [5]; // Save the input location
Char ch;
Int t, ans;
Struct Point {// coordinate struct
Int x;
Int y;
};
// Determine whether the last position is reached
Int judge (){
For (int I = 0; I <5; I ++ ){
For (int j = 0; j <5; j ++ ){
If (G [I] [j]! = Fin [I] [j]) // return 0 if there is something different from the same
Return 0;
}
}
Return 1;
}
// Deep search and backtracking
Void dfs (int x, int y, int Tans) {// input three parameter coordinates and number of steps
If (Tans> 10) return; // No search is required for steps greater than 10.
If (judge () {// if this condition is met, determine if ans are greater than Tans
If (ans> Tans) ans = Tans;
Return;
}
For (int I = 0; I <8; I ++) {// search in 8 directions
If (dir [I] [0] + x <0 | dir [I] [0] + x> = 5) continue; // The next direction is continued when the boundary is exceeded.
If (dir [I] [1] + y <0 | dir [I] [1] + y> = 5) continue; // The next direction is continued when the boundary is exceeded.
Swap (G [x] [y], G [dir [I] [0] + x] [dir [I] [1] + y]); // swap Spaces
Dfs (x + dir [I] [0], y + dir [I] [1], Tans + 1); // recursive search continues
Swap (G [x] [y], G [dir [I] [0] + x] [dir [I] [1] + y]); // return a space
}
}
// Main Function
Int main (){
Scanf ("% d % * c", & t );
While (t --){
Point p;
// Process input
For (int I = 0; I <5; I ++ ){
For (int j = 0; j <5; j ++ ){
If (j = 4) scanf ("% c % * c", & ch );
If (j <4) scanf ("% c", & ch );
If (ch = ''){
G [I] [j] =-1;
P. x = I; p. y = j;
}
Else G [I] [j] = ch-'0 ';
}
}
Ans = 999999999;
Dfs (p. x, p. y, 0 );
If (ans> 10) printf ("Unsolvable in less than 11 move (s). \ n ");
Else printf ("Solvable in % d move (s). \ n", ans );
} Www.2cto.com
Return 0;
}
Author: cgl1079743846