CS 311 Fall 2024 > Assignment 4
CS 311 Fall 2024
Assignment 4
Assignment 4 is due at 5 pm Tuesday, October 8. It is worth 65 points.
Procedures
This assignment is to be done individually.
Turn in answers to the exercises below on the UA Canvas site, under Assignment 4 for this class.
- Your answers must consist of two files:
holeydtcount.hpp
,holeydtcount.cpp
, from Exercise A. Attach these files to your homework submission. - I may not look at your homework submission immediately. If you have questions, e-mail me.
Exercises (65 pts total)
Exercise A — Counting Holey Domino Tilings
Purpose
In this exercise, you will write code that does recursive backtracking.
This exercise does not emphasize implementation details and language features nearly as much as earlier programming exercises did. You still need to write high-quality code and follow the coding standards; however, this should be comparatively easy, for those that did a decent job on the previous assignments.
The key part of this exercise is the logic that the code must follow. When you do this exercise, it is probably worthwhile to spend more time planning, and less time coding, than you have before.
Background
See Thoughts on Assignment 4, in the Friday 9/27 lecture slides, for an illustrated discussion of the background.
Consider a rectangle divided into squares, like a checkerboard or chessboard. Two squares are marked as holes. We will call the result a board.
A domino can cover two adjacent squares of a board, where two squares are adjacent if they have a common edge.
We wish to cover the board with dominoes, so that each domino covers two adjacent squares on the board, the holes are not covered, and every other square is covered by exactly one domino. We call a way of doing this a holey domino tiling.
A board has specified x and y dimensions: dimx and dimy. The columns are numbered 0 … dimx − 1, and the rows are numbered 0 … dimy − 1. We will specify the x and y dimensions and the x and y coordinates of the two holes. We then wish to determine how many different holey domino tilings the board has.
Instructions
This assignment is to be done individually.
Write a C++ package that counts the number of holey domino tilings on a given board.
Be sure to follow the coding standards. Standards 4A, 4B, and 4C now apply. You do not yet need to follow standards 4D or 4E.
- Your package must be contained in files
holeydtcount.hpp
andholeydtcount.cpp
. - The public interface of this package consists of a single function:
holeyDTCount
, which returns the number of holey domino tilings on a given board (details below). - Function
holeyDTCount
must do the bulk of its work via a call to a functionholeyDTCount_recurse
, which is a function (global or member) that counts the number of holey domino tilings based on a given partial solution, using recursive backtracking.- Note. Function
holeyDTCount_recurse
is not part of the public interface of the package. Prototype and implement it however you want; I will not test it. However, it must exist, do recursive backtracking, be used byholeyDTCount
to do most of the required computational work, and perform the correct computations for the partial solution it is given. It must also follow the applicable coding standards.
- Note. Function
Here are the details for function holeyDTCount
.
- It is prototyped as
The first two parameters are the dimensions of the board: x and y. Following are the coordinates of the first hole and the coordinates of the second hole. Thus, we must haveint holeyDTCount(int dim_x, int dim_y, int hole1_x, int hole1_y, int hole2_x, int hole2_y);
0 <= hole1_x < dim_x
, and0 <= hole1_y < dim_y
, and similarly for thehole2
parameters. - For legal parameter values, function
holeyDTCount
returns the number of holey domino tilings on adim_x
×dim_y
board with holes at (hole1_x
,hole1_y
) and (hole2_x
,hole2_y
).- So, as in the first example in the slides,
holeyDTCount(4,3, 2,0, 3,2)
returns4
.
- So, as in the first example in the slides,
- The function must work properly for all meaningful parameter values.
- The test program will only call function
holeyDTCount
with parameter values that make sense. In particular,dim_x
anddim_y
will always be positive, and the holes will always be distinct and lie on the board. - Your documentation must include information about what parameter values the function will accept (preconditions!).
- The test program will only call function
- The function must execute in a reasonable amount time—say,
less than two minutes on a recent machine—whenever
(a) the total number of squares on the board is at most 42,
or (b) either
dim_x
ordim_y
is 1, and the other is at most 1000. Do not expect your function to execute in a reasonable time for larger values. For example, if you make the board 20×20. then be prepared to wait a long time for an answer.
Test Program
A test program
is available:
holeydtcount_test.cpp
.
If you compile and run the test program (unmodified!) with your code,
then it will test
whether your code works properly.
The test program requires doctest.h
,
the header for the doctest unit-testing framework,
version 2.
Do not turn in the test program or the doctest framework.
Notes
- When your homework is graded,
function
holeyDTCount
may be called with different parameters from those in the test program. - The test program does not check whether function
holeyDTCount_recurse
meets the requirements of the assignment; nor does it check whether your code is fast enough. - Your function is only required to be reasonably fast for boards with at most 42 squares, or with either the x or y dimension being 1. However, it is still required to work for larger boards—assuming no stack overflow. Do not arbitrarily limit the size of a board!
What about Speed?
This exercise emphasizes logic, not efficiency. For grading purposes, running in a reasonable time is all I ask; you will not be given extra credit for super-fast code.
However, for those who like a challenge, once you get your code working, see how fast you can get it to run. I will do timing tests on all working implementations; the names of top performers will be announced in class some time after the homework is graded.