Solution: Cherry Pickup

Let’s solve the Cherry Pickup problem using the Dynamic Programming pattern.

We'll cover the following...

Statement

You are given an n×nn \times n grid representing a field of cherries. Each cell in the grid can have one of three possible values:

  • 00: An empty cell that can be walked through

  • 11: A cell containing a cherry that can be picked and then passed through

  • 1-1: A cell containing a thorn, which cannot be crossed

Your task is to find the maximum number of cherries that can be collected while following these rules:

  1. Start at the top-left corner (0,0)(0,0) and reach the bottom-right corner (n1, n1)(n-1,~n-1) by only moving right or down through valid cells (00 or 11).

  2. After reaching the bottom-right corner, return to the starting point (0,0)(0, 0) ...