Cherry Pickup

Two passes are required through an n x n grid with blocked cells. Return the maximum cherries that can be collected when one round trip starts at the top-left corner, reaches the bottom-right corner, and returns while avoiding blocked cells. Pattern focus: Dungeon / cherry pickup. This is a classic two-traveler dynamic programming problem.

Input Format

grid contains 1, 0, and -1

Output Format

maximum cherries collected or 0 if impossible

Constraints

  • 1 <= n <= 50

Examples

Example 1:

Input:

grid = [[0,1,-1],[1,0,-1],[1,1,1]]

Output:

5

Explanation:

The best round-trip collection is 5.

Example 2:

Input:

grid = [[1,1],[1,1]]

Output:

4

Explanation:

A 2x2 all-open grid yields 4.

Example 3:

Input:

grid = [[1,-1],[-1,1]]

Output:

0

Explanation:

If no valid route exists, the answer is 0.

Loading...
Cherry Pickup - Dp Grid DSA Problem