<p>You have a 2-D <code>grid</code> of size <code>m x n</code> representing a box, and you have <code>n</code> balls. The box is open on the top and bottom sides.</p>

<p>Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the right or to the left.</p>

<ul>
	<li>A board that redirects the ball to the right spans the top-left corner to the bottom-right corner and is represented in the grid as <code>1</code>.</li>
	<li>A board that redirects the ball to the left spans the top-right corner to the bottom-left corner and is represented in the grid as <code>-1</code>.</li>
</ul>

<p>We drop one ball at the top of each column of the box. Each ball can get stuck in the box or fall out of the bottom. A ball gets stuck if it hits a &quot;V&quot; shaped pattern between two boards or if a board redirects the ball into either wall of the box.</p>

<p>Return <em>an array </em><code>answer</code><em> of size </em><code>n</code><em> where </em><code>answer[i]</code><em> is the column that the ball falls out of at the bottom after dropping the ball from the </em><code>i<sup>th</sup></code><em> column at the top, or <code>-1</code><em> if the ball gets stuck in the box</em>.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/26/ball.jpg" style="width: 500px; height: 385px;" /></strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]]
<strong>Output:</strong> [1,-1,-1,-1,-1]
<strong>Explanation:</strong> This example is shown in the photo.
Ball b0 is dropped at column 0 and falls out of the box at column 1.
Ball b1 is dropped at column 1 and will get stuck in the box between column 2 and 3 and row 1.
Ball b2 is dropped at column 2 and will get stuck on the box between column 2 and 3 and row 0.
Ball b3 is dropped at column 3 and will get stuck on the box between column 2 and 3 and row 0.
Ball b4 is dropped at column 4 and will get stuck on the box between column 2 and 3 and row 1.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[-1]]
<strong>Output:</strong> [-1]
<strong>Explanation:</strong> The ball gets stuck against the left wall.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1],[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1]]
<strong>Output:</strong> [0,1,2,3,4,-1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 100</code></li>
	<li><code>grid[i][j]</code> is <code>1</code> or <code>-1</code>.</li>
</ul>

class Solution {
public:
    vector<int> findBall(vector<vector<int>>& G) {
        int M = G.size();    // Number of rows in the grid
        int N = G[0].size(); // Number of columns in the grid
        vector<vector<int>> memo(M, vector<int>(N, -2)); // Memoization table

        vector<int> result(N);
        for (int j = 0; j < N; ++j) {
            result[j] = dfs(0, j, G, memo);
        }

        return result;
    }

private:
    int dfs(int row, int col, vector<vector<int>>& G, vector<vector<int>>& memo) {
        // Base case: if we are out of bounds
        if (col < 0 || col >= G[0].size()) {
            return -1;
        }
        if (row == G.size()) {
            return col; // If we are at the bottom row, return the column index
        }
        if (memo[row][col] != -2) {
            return memo[row][col]; // Return memoized result if available
        }

        int nextCol = col;
        // Check if the ball can move to the right
        if (G[row][col] == 1) {
            if (col == G[0].size() - 1 || G[row][col + 1] == -1) {
                return memo[row][col] = -1; // Ball falls out of bounds or into a trap
            } else {
                nextCol = col + 1;
            }
        } 
        // Check if the ball can move to the left
        else {
            if (col == 0 || G[row][col - 1] == 1) {
                return memo[row][col] = -1; // Ball falls out of bounds or into a trap
            } else {
                nextCol = col - 1;
            }
        }

        // Move to the next row
        return memo[row][col] = dfs(row + 1, nextCol, G, memo);
    }
};

17200