logo CodeStepByStep logo

goldMine

Language/Type: C++ dynamic programming

Write a function named goldMine that solves the following "gold mine problem" using dynamic programming. Suppose you are given a 2-dimensional rectangular grid of integers representing a gold mine. Each square in the grid stores a positive integer representing the amount of gold on that square.

Imagine a gold miner who begins a journey from any square in the first column. The miner can take turns moving one square to the right, or one square up-right, or one square down-right.

Your function accepts the mine as a parameter, represented as a reference to a Grid of integers. You must find and return an integer representing the maximum amount of gold the miner can collect on any path to the right edge of the mine. For example, if the mine is the following grid:

{{1, 3, 3},
 {2, 1, 4},
 {0, 6, 4}}

Then your function should return 12, which is the amount of gold mined by walking on the squares containing the values 2, 6, 4.

You must solve this problem using a bottom-up dynamic programming approach. Do not use recursion. You are allowed to construct any data structures (array, vector, set, map, etc.) necessary to store the data for your dynamic programming algorithm.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.