![]() |
CS 112 - Introduction to Computer Science II
Homework #3 |
1. Roll Generator: Recursively compute and print all unique possible different rolls of n dice, representing each as a sorted sequence of digits, and presenting them in sorted order. Output should be space-separated on a single line. The number of dice n is given as the first command line argument. Hint: No sorting is required. First compute all possible rolls of n-1 dice. Then consider how these may be used to generate all possible rolls of n dice.
Here is an example transcript:
$ java RollGenerator 0 $ java RollGenerator 1 1 2 3 4 5 6 $ java RollGenerator 2 11 12 13 14 15 16 22 23 24 25 26 33 34 35 36 44 45 46 55 56 66 $ java RollGenerator 3 111 112 113 114 115 116 122 123 124 125 126 133 134 135 136 144 145 146 155 156 166 222 223 224 225 226 233 234 235 236 244 245 246 255 256 266 333 334 335 336 344 345 346 355 356 366 444 445 446 455 456 466 555 556 566 666 $ java RollGenerator 4 1111 1112 1113 1114 1115 1116 1122 1123 1124 1125 1126 1133 1134 1135 1136 1144 1145 1146 1155 1156 1166 1222 1223 1224 1225 1226 1233 1234 1235 1236 1244 1245 1246 1255 1256 1266 1333 1334 1335 1336 1344 1345 1346 1355 1356 1366 1444 1445 1446 1455 1456 1466 1555 1556 1566 1666 2222 2223 2224 2225 2226 2233 2234 2235 2236 2244 2245 2246 2255 2256 2266 2333 2334 2335 2336 2344 2345 2346 2355 2356 2366 2444 2445 2446 2455 2456 2466 2555 2556 2566 2666 3333 3334 3335 3336 3344 3345 3346 3355 3356 3366 3444 3445 3446 3455 3456 3466 3555 3556 3566 3666 4444 4445 4446 4455 4456 4466 4555 4556 4566 4666 5555 5556 5566 5666 6666
2. Simple Sudoku Solver:
Skim the Wikipedia article on the Sudoku puzzle. Then solve a Sudoku puzzle online. To work the puzzles, I recommend using software such as Sudo Cue (free PC software) or Mark Huckvale's applet. For tips on human solution techniques, www.sudokuoftheday.com has a good techniques overview.
Of course it's easy to solve a Sudoku puzzle computationally. There are many ways to implement a Sudoku solver, from transformation to an exact cover problem and the application of Knuth's Dancing Links algorithm (DLX), to the use of general purpose constraint programming.
As it turns out, even difficult Sudoku puzzles can be solved quickly using the simplest, brute-force techniques. For this assignment, we will implement a simple recursive depth-first Sudoku solver.
Input-output specification
Sudoku input will consist of a grid of single digits to denote clues and periods to denote empty cells. Two test files, sudokuTest.txt and sudokuTest-noSoln.txt are provided. For a solvable puzzle, the output will be a legally filled grid in the same format as the input. For an unsolvable puzzle, the message "No solution." is printed. Thus, a transcript using the test files would look like this:
$ java SudokuSolver < sudokuTest.txt 362547189 947821635 158639742 214765893 689312457 735984261 821453976 473296518 596178324 $ java SudokuSolver < sudokuTest-noSoln.txt No solution.
After reading in the grid, the core solution algorithm is simple:
boolean solve():
To generate candidate values for a given row and column, start with a list of all candidate digits. Then remove digits appearing in the same row, column, and block.
Enjoy!
Extra credit (10%): Create a class SudokuGenerator that generates and prints a Sudoku puzzle in the above input format. The puzzle should have clues such that (1) there is a unique solution, and (2) the removal of any clue would make solutions non-unique. Hint: First, modify your solver so that it will (1) indicate if there are 0, 1, or >= 2 solution(s), and (2) allow an option to randomize the order of candidates. Next, solve a blank grid using randomized candidates to generate a solution. Iterate through the cells in a random sequence. For each clue, remove it and resolve the grid. If the removed clue introduces new solution(s), replace the clue.