Solving N-Puzzles
What are some methods for solving arbitrary N sized N puzzles ? How can I determine unsolvable configurations?
A configuration is unsolvable if it doesn't have the same parity as the target configuration. To solve a solvable configuration, use a pathfinding algorithm, like A*.
Agreed... if you can't parse the answer above, perhaps you need to do some more studying before even asking the question.
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play
"Reducing the world to mathematical equations!"
First lets look at determining an unsolvable puzzle. I agree with the above posters, you should understand the parity concept presented earlier if asking the question - however here another perspective to view.
A puzzle is solvable if the number of permutations is even. This can be easily calculated. Assume you have the following puzzle where the open square is represented by a "_", and the solution puzzle has the numbers in sequential order with the open square at the bottom right. I will demonstrate this with an 8 puzzle, however the idea applies to N-puzzles.
3 4 1
7 5 6
8 _ 2
Now to calculate the number of permutations, go through each position in the board. The number where you are currently at will be called N. The number of permutations is the total from all positions of the amount of numbers smaller than N, which appears after N. Thus for this example you have the following:
3 is before 1,2 = 2
4 is before 1,2 = 2
1 is not before any lower number = 0
7 is before 5,6,2 = 3
5 is before 2 = 1
6 is before 2 = 1
8 is before 2 = 1
_ is before 2 = 1
2 + 2 + 0 + 3 + 1 + 1 + 1 + 1 = 11
11 is an odd number, thus the puzzle is not solvable. If the sum is even, the puzzle is solvable.
As for solving the puzzle, look up A*. It's a simple graph search algorithm which is easily applied to the program. As a hint, consider each state of the puzzle a node in the graph, a child node is a state that can be created from one move from the state. However, you must be sure not to create child states that exist previously in the graph.
Hope that helps a bit
A puzzle is solvable if the number of permutations is even. This can be easily calculated. Assume you have the following puzzle where the open square is represented by a "_", and the solution puzzle has the numbers in sequential order with the open square at the bottom right. I will demonstrate this with an 8 puzzle, however the idea applies to N-puzzles.
3 4 1
7 5 6
8 _ 2
Now to calculate the number of permutations, go through each position in the board. The number where you are currently at will be called N. The number of permutations is the total from all positions of the amount of numbers smaller than N, which appears after N. Thus for this example you have the following:
3 is before 1,2 = 2
4 is before 1,2 = 2
1 is not before any lower number = 0
7 is before 5,6,2 = 3
5 is before 2 = 1
6 is before 2 = 1
8 is before 2 = 1
_ is before 2 = 1
2 + 2 + 0 + 3 + 1 + 1 + 1 + 1 = 11
11 is an odd number, thus the puzzle is not solvable. If the sum is even, the puzzle is solvable.
As for solving the puzzle, look up A*. It's a simple graph search algorithm which is easily applied to the program. As a hint, consider each state of the puzzle a node in the graph, a child node is a state that can be created from one move from the state. However, you must be sure not to create child states that exist previously in the graph.
Hope that helps a bit
If a puzzle is solvable, and you just need to solve it (i.e. you don't need to find the MINIMUM numbers of moves), there's a faster and simpler method than A*:
Just try to move 1, 2, 3, ... in place, in order, while keeping the previous tiles at the correct position.
Just try to move 1, 2, 3, ... in place, in order, while keeping the previous tiles at the correct position.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement