COSC 2P03 Assignment 2

advertisement
COSC 2P03 – Advanced Data Structures
Fall 2015
Assignment #2
Due Date: October 19th, noon
Late Date: October 22nd, noon
This assignment accounts for 9% of your final grade and is worth a total of 90 marks.
The purpose of this assignment is to practice manipulating trees and using traversals.
You are to implement a program that will generate Shidoku boards. Shidoku is a puzzle that is a
smaller version of Sudoku. A Shidoku puzzle is a 4x4 board divided into four 2x2 squares. A
valid complete Shidoku solution must satisfy all of the following conditions:
 every row contains each of the numbers 1, …, 4 exactly once, and
 every column contains each of the numbers 1, …, 4 exactly once, and
 every 2x2 square contains each of the number 1, …, 4 exactly once.
A Shidoku puzzle is a partially-completed board for which there is exactly one possible complete
solution. Consider the following examples.
1
2
3
4
1
2
3
4
1
2
3
4
1
2
3
4
3
4
1
2
3
4
1
2
3
4
1
2
3
4
1
2
2
1
4
3
2
3
4
3
2
1
2
4
1
Valid board with
Invalid board
Valid board with
2
complete
1 complete
solutions
solution
The first board, being complete already, cannot be considered a puzzle. The second board can be
used as a Shidoku puzzle, as there is exactly one complete solution that corresponds to this board.
The third board cannot be used as a puzzle, since there are two complete solutions that correspond
to this board. The fourth board cannot be used as a puzzle since it cannot possibly lead to a valid
complete solution.
Valid complete
solution
Suppose that you are presented with a valid incomplete board. By using a single step, several
other boards may be generated. A single step consists of choosing a value for any of the
remaining empty positions. You can verify for yourself that in the second example board above, 8
boards may be generated using a single step (6 of which will eventually lead to a valid solution);
meanwhile, in the third example above, 10 boards may be generated using a single step. The
boards thus generated can be considered “children” of the initial board.
For this assignment, you are to construct a tree to represent all of the descendants of a given
starting board. The tree should only contain valid boards. Many of the incomplete boards in the
tree will lead to more than 1 valid complete solution – this is intentional and this information is
required in a later step. You should use the representation of a general tree discussed in class and
also described in section 4.1.1 of your textbook, in which each node has a pointer to its leftmost
child and its (own) next sibling. Note that even for this simple puzzle, this tree will be enormous
for certain inputs. Therefore you may assume that at the very least, the entire first row (and
usually more) is provided.
Having constructed the tree, we are now interested in how many distinct complete solutions are
descendants of each node. There are a total of 288 possible complete Shidoku solutions; when the
first row is provided, only 288/(4!) = 12 are possible (see [1] and [2]). However, generally each
complete Shidoku solution will appear as a leaf several times, since it may be obtained by
different sequences of choices. Consider the following example:
1
2
3
4
1
2
3
4
1
2
3
4
3
4
1
2
3
4
1
2
3
4
1
2
2
1
4
3
2
1
4
3
2
1
Valid Complete
Solution
2
3
Valid board
leading to
complete solution
Valid board
leading to
complete solution
The first board above is a descendant of both the second board and the third board (and others).
To find the number of distinct complete solutions descended from each board, use a recursive
postorder traversal and store the count (together with the corresponding distinct complete
solutions themselves) in the node corresponding to that board.
Finally, use a recursive preorder traversal to examine each node in the tree for the following
condition: if there is only one distinct complete solution for a given board, and its parent has more
than one distinct complete solution, then print out the board.
After the traversals are complete, print a count of the total number of nodes that correspond to a
single complete solution (this total number may be obtained during one of the traversals). This is,
of course, the total number of distinct puzzles that arise from the initial board configuration.
There are many thousands of possible Shidoku puzzles (see [2]). Since we are restricting
ourselves to a given starting configuration, we should obtain a much smaller number.
For submission purposes, you must hand in the output obtained by running your program using
the input file assn2in.txt found on the course website. This file gives the known entries (1,2,3
or 4) of the board in row-major order, with 0 for unknown entries. Your program will be tested
using other input files; therefore you must make sure that your program is thoroughly tested.
Warning: do not attempt to run your program starting from an initially empty board, as this will
take a huge amount of both time and memory.
Submission Requirements:
All of the following must be submitted in an envelope in the COSC 2P03 assignment box:
1. A cover sheet, available from http://www.cosc.brocku.ca/forms/cover, completely
filled out. Your assignment will not be marked unless one is submitted with the assignment.
2. A commented and properly documented source code listing for your program.
3. A listing of the output for your program from the supplied input, assn2in.txt.
4. A statement specifying whether you used NetBeans or DrJava (to make it easy for the marker
to run it).
You must also submit your assignment electronically so that it can be checked for plagiarism
using MOSS. To do this, create a directory on Sandcastle containing all files for this assignment,
and run the script submit2p03 from this directory. Your assignment will not be marked unless
this is done.
References:
1. Dianne Henry, “Sudoku for Dummies: A Mathematicians Approach, manuscript available
online from math.missouristate.edu/assets/Math/Sudoku.doc
2. Sourendu Gupta, “Shi Doku: Exploring the Mathematics of Su Doku”, website available at
http://theory.tifr.res.in/~sgupta/sudoku/shidoku.html
Download