|
| 1 | +package com.bytecode.leetcode.dp; |
| 2 | + |
| 3 | +/** |
| 4 | + * 63. Unique Paths II |
| 5 | + * <p> |
| 6 | + * A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). |
| 7 | + * <p> |
| 8 | + * The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below). |
| 9 | + * <p> |
| 10 | + * Now consider if some obstacles are added to the grids. How many unique paths would there be? |
| 11 | + * <p> |
| 12 | + * An obstacle and empty space is marked as 1 and 0 respectively in the grid. |
| 13 | + * <p> |
| 14 | + * Note: m and n will be at most 100. |
| 15 | + * <p> |
| 16 | + * Example 1: |
| 17 | + * <p> |
| 18 | + * Input: |
| 19 | + * [ |
| 20 | + * [0,0,0], |
| 21 | + * [0,1,0], |
| 22 | + * [0,0,0] |
| 23 | + * ] |
| 24 | + * Output: 2 |
| 25 | + * Explanation: |
| 26 | + * There is one obstacle in the middle of the 3x3 grid above. |
| 27 | + * There are two ways to reach the bottom-right corner: |
| 28 | + * 1. Right -> Right -> Down -> Down |
| 29 | + * 2. Down -> Down -> Right -> Right |
| 30 | + * <p> |
| 31 | + * <a href="https://leetcode.com/problems/unique-paths-ii/">63. Unique Paths II</a> |
| 32 | + * <p> |
| 33 | + * <a href="https://leetcode-cn.com/problems/unique-paths-ii/">63. 不同路径 II</a> |
| 34 | + * Created by vencial on 2019-09-10. |
| 35 | + */ |
| 36 | +public class UniquePathsII { |
| 37 | + |
| 38 | + /** |
| 39 | + * 1. 设dp为每格可能路径的总数, 如果无路障则第一行和第一列均为1, 否则为0 |
| 40 | + * 2. 如果obstacleGrid[i][j]是路障, 则dp[i][j]为0, 否则dp[i][j]则为dp[i - 1][j] 加上dp[i][j - 1]的总和 |
| 41 | + * <p> |
| 42 | + * 1. let dp is the sum of possible unique paths, as we know dp of the first row and column is 1 if there is no obstacles |
| 43 | + * 2. if obstacleGrid[i][j] is obstacles, dp[i][j] should be 0, else dp[i][j] is the sum of left dp and the top dp, that is dp[i - 1][j] + dp[i][j - 1] |
| 44 | + * |
| 45 | + * @param obstacleGrid obstacleGrid |
| 46 | + * @return int |
| 47 | + */ |
| 48 | + public int uniquePathsWithObstacles(int[][] obstacleGrid) { |
| 49 | + if (obstacleGrid.length == 0) return 0; |
| 50 | + if (obstacleGrid[0][0] == 1) { |
| 51 | + return 0; |
| 52 | + } |
| 53 | + int row = obstacleGrid.length; |
| 54 | + int column = obstacleGrid[0].length; |
| 55 | + int dp[][] = new int[row][column]; |
| 56 | + dp[0][0] = 1; |
| 57 | + for (int i = 1; i < row; i++) { |
| 58 | + if (obstacleGrid[i][0] != 1 && dp[i - 1][0] == 1) { |
| 59 | + dp[i][0] = 1; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + for (int i = 1; i < column; i++) { |
| 64 | + if (obstacleGrid[0][i] != 1 && dp[0][i - 1] == 1) { |
| 65 | + dp[0][i] = 1; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + for (int i = 1; i < row; i++) { |
| 70 | + for (int j = 1; j < column; j++) { |
| 71 | + if (obstacleGrid[i][j] == 1) { |
| 72 | + dp[i][j] = 0; |
| 73 | + continue; |
| 74 | + } |
| 75 | + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; |
| 76 | + } |
| 77 | + } |
| 78 | + return dp[row - 1][column - 1]; |
| 79 | + } |
| 80 | +} |
0 commit comments