package com.ljp.test.leetcode;
/**
* <b>63. 不同路徑 II
*
*
* 一個機器人位于一個m x n網格的左上角 (起始點在下圖中標記為 “Start” )。
*
* 機器人每次只能向下或者向右移動一步。機器人試圖達到網格的右下角(在下圖中標記為 “Finish”)。
*
* 現在考慮網格中有障礙物。那么從左上角到右下角將會有多少條不同的路徑?
*
* 網格中的障礙物和空位置分別用 1 和 0 來表示。
*
*
* 示例 1:
*
*
* 輸入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
* 輸出:2
* 解釋:3x3 網格的正中間有一個障礙物。
* 從左上角到右下角一共有 2 條不同的路徑:
* 1. 向右 -> 向右 -> 向下 -> 向下
* 2. 向下 -> 向下 -> 向右 -> 向右
* 示例 2:
*
*
* 輸入:obstacleGrid = [[0,1],[0,0]]
* 輸出:1
*
* 提示:
*
* m == obstacleGrid.length
* n == obstacleGrid[i].length
* 1 <= m, n <= 100
* obstacleGrid[i][j] 為 0 或 1
*
* 來源:力扣(LeetCode)
* 鏈接:<a >63. 不同路徑 II
* 著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
*
* @author luojunping
* @since 2023-03-09
*/
public class Number0063{
? ? public static void main(String[] args) {
? ? ? ? int[][] nums1 ={{0, 0, 0}, {0, 1, 0}, {0, 0, 0}};
? ? ? ? int[][] nums2 ={{0, 1}, {0, 0}};
? ? ? ? System.out.println(DynamicPlanning.uniquePathsWithObstacles(nums1));
? ? ? ? System.out.println(DynamicPlanning.uniquePathsWithObstacles(nums2));
? ? ? ? System.out.println("---------------------------------------------");
? ? ? ? System.out.println(DynamicPlanning.uniquePathsWithObstaclesSpaceBetter(nums1));
? ? ? ? System.out.println(DynamicPlanning.uniquePathsWithObstaclesSpaceBetter(nums2));
? ? }
? ? private static class DynamicPlanning{
? ? ? ? public static int uniquePathsWithObstacles(int[][] nums) {
? ? ? ? ? ? int m = nums.length;
? ? ? ? ? ? int n = nums[0].length;
? ? ? ? ? ? int[][] dp =new int[m][n];
? ? ? ? ? ? for (int i =0; i < m; i++) {
? ? ? ? ? ? ? ? if (nums[i][0] ==1) {
? ? ? ? ? ? ? ? ? ? dp[i][0] =0;
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? dp[i][0] =1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? for (int i =0; i < n; i++) {
? ? ? ? ? ? ? ? if (nums[0][i] ==1) {
? ? ? ? ? ? ? ? ? ? dp[0][i] =0;
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? dp[0][i] =1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? for (int i =1; i < m; i++) {
? ? ? ? ? ? ? ? for (int j =1; j < n; j++) {
? ? ? ? ? ? ? ? ? ? if (nums[i][j] ==1) {
? ? ? ? ? ? ? ? ? ? ? ? dp[i][j] =0;
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? dp[i][j] = dp[i -1][j] + dp[i][j -1];
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return dp[m -1][n -1];
? ? ? ? }
? ? ? ? public static int uniquePathsWithObstaclesSpaceBetter(int[][] nums) {
? ? ? ? ? ? int m = nums.length;
? ? ? ? ? ? int n = nums[0].length;
? ? ? ? ? ? int[] dp =new int[n];
? ? ? ? ? ? for (int i =0; i < n; i++) {
? ? ? ? ? ? ? ? if (nums[0][i] ==1) {
? ? ? ? ? ? ? ? ? ? dp[i] =0;
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? dp[i] =1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? for (int i =1; i < m; i++) {
? ? ? ? ? ? ? ? for (int j =0; j < n; j++) {
? ? ? ? ? ? ? ? ? ? if (nums[i][j] ==1) {
? ? ? ? ? ? ? ? ? ? ? ? dp[j] =0;
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? if (j ==0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? dp[j] =1;
? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? dp[j] += dp[j -1];
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return dp[n -1];
? ? ? ? }
? ? }
}