PROGRAMMING PROJECT (10 points)
A popular puzzle requires the player to fill in a 9 X 9 grid with the
numbers
from 1 to 81. The catch is that the numbers must form a path from 1 to 81,
connecting horizontally and vertically only (not diagonally). The figures
below show an initial puzzle and then the solution showing the path that
connects the numbers in order from 1 to 81.
The puzzle begins with the numbers shown in the shaded spaces above. The
player has to fill in the remaining numbers.
Assignment
Write a Java class NumberPuzzle that reads in the
initial state of the puzzle and then outputs the unique solution to the
puzzle. Your solution should use recursion and backtracking.
The size of the puzzle will be N × N, using the numbers 1 to
N2, where 6 ≤ N ≤ 10.
The initial numbers provided can be in any position of the board (not just
the
shaded area shown in the example).
You may NOT assume that the value 1 is given
to you initially.
Input Format
You will read the input puzzles from a text file with the following
format.
The first line of input will be the number of test cases to be executed,
followed by the data for each test case.
For each test case, the first line will indicate the dimension of the
puzzle
as a single integer (between 6 and 10 inclusive). The following N lines
will
contain N integers per line separated by whitespace, representing the
initial
puzzle.
Blank cells are represented by the value 0; non-blank cells are
represented by their individual values. You may assume that the number
of values per line and number of lines per puzzle is correct based on the
given dimension of the puzzle.
Here is an example of an input data file with 2 puzzles, the first puzzle
matching the example shown in the picture above.
2
9
0 0 0 0 0 0 0 0 0
0 81 76 75 72 65 62 61 0
0 6 0 0 0 0 0 56 0
0 1 0 0 0 0 0 55 0
0 10 0 0 0 0 0 50 0
0 13 0 0 0 0 0 51 0
0 20 0 0 0 0 0 44 0
0 19 22 27 26 35 38 39 0
0 0 0 0 0 0 0 0 0
6
0 0 12 13 0 0
9 0 0 0 0 16
0 0 4 5 0 0
0 0 23 22 0 0
0 1 0 0 26 0
0 0 31 30 0 0
Output Format
For each test case, your output should consist of N lines showing the
final
solution to each puzzle, row by row, where N is the dimension of the
puzzle.
Each line should have N numbers
separated by a single space between each number. Print one blank line
between
test cases for readability.
Here is the sample output
79 78 77 74 73 64 63 60 59
80 81 76 75 72 65 62 61 58
7 6 5 4 71 66 67 56 57
8 1 2 3 70 69 68 55 54
9 10 11 30 31 48 49 50 53
14 13 12 29 32 47 46 51 52
15 20 21 28 33 34 45 44 43
16 19 22 27 26 35 38 39 42
17 18 23 24 25 36 37 40 41
10 11 12 13 14 15
9 8 7 6 17 16
36 3 4 5 18 19
35 2 23 22 21 20
34 1 24 25 26 27
33 32 31 30 29 28
Programming Guidance
First, solve this problem assuming that the number 1 is given in the
initial puzzle. Once you have this working, save a copy of this program
elsewhere, then determine what you need
to change to make the program work if the number 1 is not present.
If you can't get this version of the program to work correctly, you
can hand in the previous version to maximize your partial credit.
Documentation & Style
Be sure to document all of the code that you write to explain what you are
doing. Use appropriate Java style (indentation, variable names, etc.) to
make your code as readable as possible.
Hand-In Instructions & Late Submissions
Your submitted zip file should include the complete Java program.
See the course website for instructions on how to
hand in your program and the policy for late
submissions.