× Home (CS 211) Notes Projects Waxman's Website

Purpose

The goal of this program is to find the shortest way to move throw a weighted 2D array. This project uses memoization to efficiently find the solution.

Rules

  1. Must go through one square on each column.
  2. Each move can only go one square horizontally or diagonally.
  3. The 2D array grid is a cylinder, it wraps around.
  4. The squares are weighted; the number in each square represents the time it takes to travel through the square.

Data Representation

2D array Weighted matrix:

3 1 4
2 3 5
4 5 4
5 2 3

Cost matrix:

The values at the matrix tell you the lowest possible cost to get to this square.
cost[i][j] = the total cost of the shortest path from the left side to square in row i and column j.
cost is what is being used for memoization.

The highlighted square (row 0, col 1) has a cost of 3 because the shortest, cheapest path to the square is 2(row 1, col 0). The cost of traveling through square (row 0, col 1) is 1. So 2+1 =3.

3 3 7
2 5 8
4 7 9
5 5 6

Path matrix:

The path matrix keeps track of what path it took to get to that square.
Path[i][j] tells what path is took to get to the square on row I and col j.
The Path matrix stores STRINGS not integers.

The highlighted 103 is path[3][2] or square at row 3 col 2.
103 tells us that it got to the square by going from square 1 of the first column. Then square 0 of the second column. And finally it got to square 3 of the last column.

0 10 100
1 11 101
2 12 112
3 03 103

Top-Down Recursion

  1. Create function calCost that that’s in 2 integers; int i and int j. The function cal the shortest path/lowest cost to that location
  2. Base case is if you are at the left most column. The cost of the left most column is just its initial weight
  3. If the cost for square (i j) has already been calculated, no need to recalculate it, just return the cost.
  4. Calculate the cost of the left, up, and down square.
  5. Save the min of the 3 cost into minCost. (you can use min function in the algorithm library)
  6. Update the path matrix. Concatenate the current string for that square with the row of the current square. To concatenate two strings use + operator and to_string(i) (this convert the int i to a string)
  7. Update the cost matrix with the minCost + the weight of the current square
  8. In the main function, call the calCost function on each of the right most squares. Check for the lowest costing one and print its cost and path.

Bottom-Up Non-Recursive

  1. Create a function called calCost that will do all the work.
  2. Fill out the cost matrix one at a time from the left end.
  3. Copy the weight of the left-most col of the weight matrix to the left-most col of the cost matrix.
  4. Go through the columns and calculate the cost of getting to each square. Save only the lowest cost. Update the path array. You can calculate the cost similar to how the recursive did it. Calculate the up, down, left(right) of the square and save the smallest one.
  5. In the main function just call the calCost function. Check for the lowest cost in the right most column and print the path to it.

Resource

Recursive Template
Non-Recursive Template
Output
This is to be submitted on blackboard as a .cpp file. File name follows the following format: lastName_firstName_rec.cpp and lastName_firstName_nonrec.cpp