Skip to content

Commit

Permalink
inh
Browse files Browse the repository at this point in the history
  • Loading branch information
pkirlin committed Nov 8, 2024
1 parent d85c05d commit cd8d468
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 14 deletions.
6 changes: 5 additions & 1 deletion _modules/week-01.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ Nov 6
: **Lab 9**

Nov 8
: **TBA**
: **Inheritance I**
: [Slides](lectures/inheritance/day1-inherit-slides.pdf)[10am code](https://github.com/pkirlin/cs142-f24-inclass/blob/10am/src/inherit1)
[11am code](https://github.com/pkirlin/cs142-f24-inclass/blob/11am/src/inherit1)
: *Handouts:* [Inheritance handout](lectures/inheritance/inheritance-handout.pdf)
: *Reading*: Liang 11.1-11.2 (inheritance)

Nov 11
: **TBA**
Expand Down
84 changes: 71 additions & 13 deletions projects/proj4/rec1.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,82 @@ nav_exclude: true

# First recursive formulation

- Is the patronus at the same location as the cup? If so, return success.
- Check if the patronus can move one step north. If so, recursively solve the maze from one step north. If this recursive call succeeds, then we succeed.
- Check if the patronus can move one step south. If so, recursively solve the maze from one step south. If this recursive call succeeds, then we succeed.
- Repeat for east and west. (So always north first, then south, then east, then west.)
- If none of the (up to four) recursive calls succeeded, then fail.
- Is our current location the ending location, AND are all row/col counts zero? If so, return success (true).
- Else if our current location is *not* the ending location: check the following:
- Check if we can move one step north. If so, recursively solve the maze from one step north. If this recursive
call succeeds, then we succeed.
- Check if we can move one step south. If so, recursively solve the maze from one step south. If this
recursive call succeeds, then we succeed.
- Repeat for east and west. (So always north first, then south, then east, then west.)
- If none of the (up to four) recursive calls succeeded, then fail. Note that we also fail if we are at the ending location
but the row/col counts are not all zero.

Let's run through this idea with maze0.txt, repeated here, with labels on the rows and columns:
Let's run through this idea with maze0.txt, drawn out as a diagram here:


```
S . . 3
. . . 1
F . . 3
2 2 3
```

S=start, F=finish, but those letters do not actually appear in the maze. Only `'` and `*` do.

Remember, the numbers at the end of each row/col indicate how many squares in each row/col must be filled
by the solution path to the maze.

We begin at (0, 0) *(remember this is row=0, col=0)*, so we begin by calling `canSolve(0, 0)`. We decrease the row/col
counts for (0, 0), so our maze looks like this:

```
* . . 2
. . . 1
F . . 3
1 2 3
```

The ending location is not (0, 0), so we will examine north, south, east, west in that order, and consider if it is a legal
move to go there.

North is illegal (off the board).

South is possible, so we will try
solving one from step south: make the recursive call canSolve(1, 0).

So far our recursion diagram looks like this:


```
canSolve(0, 0)
|
+--> canSolve(1, 0)
```

And our maze looks like this (after updating row/col counts):

```
0123
0####
1# ##
2# C#
3#H##
4####
* . . 2
* . . 0
F . . 3
0 2 3
```

The patronus begins at (3, 1) *(remember this is row=3, col=1)*, so we begin by calling `canSolve(3, 1)`. The cup is not at (3, 1), so we try solving one from step north (make a recursive call at (2, 1). The cup is not at (2, 1), so again we recursively solve from one step north, so we make a recursive call at (1, 1). Again, the cup isn't at (1, 1), so we try going north, but that isn't possible (because the hedge blocks us). We try south, but that's where we just came from, so that isn't a good idea (your code will drop a breadcrumb to prevent this). We try east and west, but those are both blocked. So it turns out (1, 1) is a dead end. So far this is a diagram of our recursive calls:
The ending location is not (1, 0), so again we consider north/south/east/west in that order.

We can't go north because (0, 0) is already part of the path.

We can't go south because (2, 0) the column 0 counter is already at zero, so we can't use any more squares in column 0 as part
of our answer.

We can't go east because the row 1 counter is also at zero.

We can't go west because that would leave the boundary of the maze. So clearly we are at a dead-end and need to BACKTRACK.

So this current call to canSolve(1, 0) is going to return failure (false).

```
canSolve(3, 1)
Expand Down

0 comments on commit cd8d468

Please sign in to comment.