Class Solution

java.lang.Object
g0901_1000.s0980_unique_paths_iii.Solution

public class Solution extends Object
980 - Unique Paths III.<p>Hard</p> <p>You are given an <code>m x n</code> integer array <code>grid</code> where <code>grid[i][j]</code> could be:</p> <ul> <li><code>1</code> representing the starting square. There is exactly one starting square.</li> <li><code>2</code> representing the ending square. There is exactly one ending square.</li> <li><code>0</code> representing empty squares we can walk over.</li> <li><code>-1</code> representing obstacles that we cannot walk over.</li> </ul> <p>Return <em>the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once</em>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/08/02/lc-unique1.jpg" alt="" /></p> <p><strong>Input:</strong> grid = [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> We have the following two paths:</p> <ol> <li> <p>(0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)</p> </li> <li> <p>(0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)</p> </li> </ol> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/08/02/lc-unique2.jpg" alt="" /></p> <p><strong>Input:</strong> grid = [[1,0,0,0],[0,0,0,0],[0,0,0,2]]</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> We have the following four paths:</p> <ol> <li> <p>(0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)</p> </li> <li> <p>(0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)</p> </li> <li> <p>(0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)</p> </li> <li> <p>(0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)</p> </li> </ol> <p><strong>Example 3:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/08/02/lc-unique3-.jpg" alt="" /></p> <p><strong>Input:</strong> grid = [[0,1],[2,0]]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> There is no path that walks over every empty square exactly once.</p> <p>Note that the starting and ending square can be anywhere in the grid.</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 <= m, n <= 20</code></li> <li><code>1 <= m * n <= 20</code></li> <li><code>-1 <= grid[i][j] <= 2</code></li> <li>There is exactly one starting cell and one ending cell.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • uniquePathsIII

      public int uniquePathsIII(int[][] grid)