CIT 594 Assignment 4: Backtracking
Spring 2015, David Matuszek
Stack class and Set interface.Write a program to read in and solve path-finding puzzles. Each puzzle consists of a sequence of integers, like this:
| 3 | 6 | 4 | 1 | 3 | 4 | 2 | 5 | 3 | 0 |
The orange color on the first square indicates your current position. At each step in the puzzle, you may move exactly the number of squares indicated by the integer in the square you are in. You may move either left or right along the row but may not move past either end. For example, the only legal first move is to move three squares to the right because there is no room to move three spaces to the left. The goal of the puzzle is to move the marker to the end of the row. In this configuration, you can solve the puzzle by making the following set of moves:
| Starting position |
|
||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Step 1: Move right |
|
||||||||||
| Step 2: Move left |
|
||||||||||
| Step 3: Move right |
|
||||||||||
| Step 4: Move right |
|
||||||||||
| Step 5: Move left |
|
||||||||||
| Step 6: Move right |
|
Even though this puzzle is solvable—and indeed has more than one solution—some puzzles of this form may be impossible, such as the following one:
| 3 | 1 | 2 | 3 | 0 |
In this puzzle, you can bounce between the two 3’s, but you cannot reach any other squares.
You may assume:
You may not assume:
20, for example.) 3 1 2 3 0 .)You may not change the numbers in the array.
Write the following methods, with the exact signatures specified:
public static Stack<Character> solve(int[] puzzle)puzzle[0]) to the last location in the array (puzzle[puzzle.length - 1]). No location in the array may be entered more than once. A move to the left will be represented by the capital letter 'L'; a move to the right will be represented by the capital letter 'R'. The top value on the stack is the first move.null value (instead of a Stack) to indicate that there is no solution.public static Set<Stack<Character>> findAllSolutions(int[] puzzle)solve, but this method finds and returns all such solutions. If there are no solutions, an empty set is returned.Unit test these methods.
Notes:
JFileChooser to allow the user to select a file containing one or more puzzles of this sort. The file should contain one puzzle per line, as a list of integers separated by spaces.Backtrackingbacktrackingclass Backtracking
public static Stack<Character> solve(int[] puzzle)public static Set<Stack<Character>> findAllSolutions(int[] puzzle)solve and findAllSolutions.Backtracking class and all methods in it.