Files
CS412-Applied-Algorithms/Recursive-Grid-Tiling/README.md

158 lines
5.0 KiB
Markdown
Raw Normal View History

# Coding 2: Recursive Grid Tiling
## Grid Tiling
Here's an interesting theorem: consider a square grid of squares with sides
given by some power of (i.e. a grid of size $2^n \times 2^n$) like this example
of a $2^3 \times 2^3$ grid:
![t0](./images/t0.png)
If you select exactly one grid square to remove (which is colored black below),
then the remaining squares can be tiled by 3-square L-shape tiles, like this.
![tiling](./images/tiling.png)
The proof of this fact is inductive and leads to a simple recursive algorithm
for producing a tiling. Assume that you can tile any $2^{n-1} \times 2^{n-1}$
grid with a single square removed with L-shaped tiles. Suppose we are given a
$2^n \times 2^n$ grid with one square removed.
Here's an example of what we've got:
![t1](./images/t1.png)
How can we use our inductive hypothesis to show that the entire grid can be
tiled? Well, let's start by splitting our grid vertically and horizontally in
half:
![t2](./images/t2.png)
Notice that this splits our $2^n \times 2^n$ sized grid into four
$2^{n-1} \times 2^{n-1}$ sized sub-grids, and one of them (the top left in our
example above) already has a square removed _but notice that three of them do
not yet have a square removed_. This means we can use our inductive hypothesis
to tile whichever sub-grid contains the square we already wanted removed:
![t3](./images/t3.png)
But now we're a bit stuck, because the conditions of the inductive hypothesis
apply if we have a $2^{n-1} \times 2^{n-1}$ grid with one square removed, but
instead we have three $2^{n-1} \times 2^{n-1}$ grids with no squares removed.
But now notice the following trick. We can place a single L-tile that covers
exactly one grid square in each of three sub-grids that we haven't yet tiled!
![t4](./images/t4.png)
But notice that since this new L-tile covers exactly one square from each of the
three untiled sub-grids, we now have three $2^{n-1} \times 2^{n-1}$ each with a
single square removed and so the inductive hypothesis now applies! We can tile
each quadrant individually using the inductive hypothesis.
![t5](./images/t5.png)
Then
![t6](./images/t6.png)
Then
![t7](./images/t7.png)
And finally, with the red dividing lines removed:
![t8](./images/t8.png)
The base case for this induction are the 2x2 grids, which become L-tiles if we
remove anyone grid square.
## Your Task
You are going to write a program to compute L-tilings of $2^n \times 2^n$ square
grids. You will assign each tile a number starting with 00. The first tile you
generate should have all of its grid cells marked with a 00, the next tile 01,
etc.
### Input
The first line of input will give the grid size parameter n as an integer. The
second line will contain the index (i, j) of the removed tile.
### Output
Your code should output $2^n$ lines, each containing $2^n$ 2-digit numbers
separated by spaces (and formatted so that single digit numbers have a leading
0). The value of each grid location should be the tile index you generated. (You
may assume you won't need 3 digits).
<table>
<tr>
<td>Sample Input</td>
<td>Sample Output</td>
</tr>
<tr>
<td><pre>3<br>0 0</pre></td>
<td>
<pre>
-1 02 03 03 07 07 08 08
02 02 01 03 07 06 06 08
04 01 01 05 09 09 06 10
04 04 05 05 00 09 10 10
12 12 13 00 00 17 18 18
12 11 13 13 17 17 16 18
14 11 11 15 19 16 16 20
14 14 15 15 19 19 20 20</pre>
</td>
</tr>
<tr>
<td><pre>3<br>1 2</pre></td>
<td>
<pre>
02 02 03 03 07 07 08 08
02 01 -1 03 07 06 06 08
04 01 01 05 09 09 06 10
04 04 05 05 00 09 10 10
12 12 13 00 00 17 18 18
12 11 13 13 17 17 16 18
14 11 11 15 19 16 16 20
14 14 15 15 19 19 20 20</pre>
</td>
</tr>
<tr>
<td><pre>4<br>10 10</pre></td>
<td>
<pre>
03 03 04 04 08 08 09 09 24 24 25 25 29 29 30 30
03 02 02 04 08 07 07 09 24 23 23 25 29 28 28 30
05 02 06 06 10 10 07 11 26 23 27 27 31 31 28 32
05 05 06 01 01 10 11 11 26 26 27 22 22 31 32 32
13 13 14 01 18 18 19 19 34 34 35 35 22 39 40 40
13 12 14 14 18 17 17 19 34 33 33 35 39 39 38 40
15 12 12 16 20 17 21 21 36 36 33 37 41 38 38 42
15 15 16 16 20 20 21 00 00 36 37 37 41 41 42 42
45 45 46 46 50 50 51 00 66 66 67 67 71 71 72 72
45 44 44 46 50 49 51 51 66 65 65 67 71 70 70 72
47 44 48 48 52 49 49 53 68 65 -1 69 73 73 70 74
47 47 48 43 52 52 53 53 68 68 69 69 64 73 74 74
55 55 56 43 43 60 61 61 76 76 77 64 64 81 82 82
55 54 56 56 60 60 59 61 76 75 77 77 81 81 80 82
57 54 54 58 62 59 59 63 78 75 75 79 83 80 80 84
57 57 58 58 62 62 63 63 78 78 79 79 83 83 84 84</pre>
</td>
</tr>
</table>
## Visualizing Your Results:
You can use the [plot_tiles.py](./plot_tiles.py) program to visualize your
input. It will draw figures similar to the ones shown in this assignment and may
help you debug your program if it has issues.
## Program Grading and Submission
This assignment is to be submitted into Gradescope. Grading criterion is:
- 10 points for the example problems shown in the assignment
- 8 points on hidden example problems
- 2 points for style