Move letters
2x3=6 in ABCDE five letters, the lower right corner of the lattice empty.
The letters in the lattice adjacent to the empty lattice can be moved into spaces, for example, the C and E in the diagram can be moved, and the situation after the move is:
A B
D E C
A B C
D E
In order to be convenient, we use a string representation of the 6 lattice letter configuration, for example, the two above situations are represented as:
Ab*dec
Abcd*e
The requirements of the topic are: Please write a program, the user input a number of strings to indicate the situation, the program through the calculation, the output can be through the initial state through a number of moves to reach the state. Output 1 can be achieved, otherwise output 0. The initial state is: abcde*
The format of the user input is: first an integer n, which indicates the next n-line state. The program output should also be N rows 1 or 0
For example, user input:/p>
3
abcde*
Ab*dec
Caed*b
The program should output:
1
1
0
--------------------------------------------------------------------------------------------------------------- -----------------
As you can see, the movement of the letter is equivalent to the unique "space" of the moving tool.
We can consider the movement of spaces, which is more convenient.
Starts with the initial state and moves the space to form a new state. Continue to add new States until all possible states are included.
The idea of recursion can also be recursive:
The state that can be reached by a step in the initial state is judged as a solution;
Otherwise, the recursive solution is continued by the derived state.
To prevent calls to continue indefinitely, the conditions for ending recursion must be considered. Consider, for example, that recursion depth cannot exceed the total number of squares