不同路径 II

题目描述:一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。

机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。

现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?

示例说明请见LeetCode官网。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/unique-paths-ii/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:递归法

首先,经过分析可知,到达任意一个单元格子的最后一步,可以从这个格子的左边过来,也可以从这个格子的上边过来,所以到达任意一个格子的步数是到它左边的步数加上到它上面格子的步数之和,所以可以用递归的方法求解,具体过程如下:

  • 如果m等于1或者n等于1,直接返回1;
  • 如果上面的条件不满足,则递归调用该方法求解uniquePaths(m - 1, n) + uniquePaths(m, n - 1)。

说明:和LeetCode-062-不同路径的区别在于,当左边或者上面的走法为0(即走不通的时候),则只用继续往一个方向递归。

解法二:迭代法

首先记录第一行的格子的走法columns,从第一个元素开始判断,如果第一个元素的值为1(即有障碍物),则为0,然后给columns的后面的元素赋值,赋值时需要同时判断前面一个元素的值和当前位置是否有障碍物。然后根据columns迭代获取下面每一行相应的走法,迭代过程如下:

  • 首先根据上一行第一个元素的值和当前行第一个元素是否有障碍物获取columns[0]的值;
  • 然后重复上面的过程,给columns的后面的元素赋值,赋值时需要同时判断前面一个元素的值和当前位置是否有障碍物。

最后返回columns最后一个元素的值即为最终的走法。

说明:解决过程类似 LeetCode-062-不同路径,特别注意当第一个元素为1时,则走不通;当只有一个元素时,且为0时,返回1也就是有一种走法,而不是返回0。

  1. public class LeetCode_063 {
  2. /**
  3. * 递归法
  4. *
  5. * @param obstacleGrid
  6. * @return
  7. */
  8. public static int uniquePathsWithObstacles(int[][] obstacleGrid) {
  9. int m = obstacleGrid.length, n = obstacleGrid[0].length;
  10. if (m == 1 && n == 1) {
  11. if (obstacleGrid[m - 1][n - 1] == 1) {
  12. return 0;
  13. } else {
  14. return 1;
  15. }
  16. }
  17. if (obstacleGrid[m - 1][n - 1] == 1) {
  18. return 0;
  19. }
  20. return uniquePathsWithObstacles(obstacleGrid, obstacleGrid.length - 1, obstacleGrid[0].length - 1);
  21. }
  22. private static int uniquePathsWithObstacles(int[][] obstacleGrid, int x, int y) {
  23. if (obstacleGrid[x][y] == 1) {
  24. return 0;
  25. }
  26. if (x == 0 && y == 0) {
  27. if (obstacleGrid[x][y] == 1) {
  28. return 0;
  29. } else {
  30. return 1;
  31. }
  32. }
  33. if (x > 0) {
  34. if (y > 0) {
  35. if (obstacleGrid[x - 1][y] == 1) {
  36. if (obstacleGrid[x][y - 1] == 1) {
  37. return 0;
  38. } else {
  39. return uniquePathsWithObstacles(obstacleGrid, x, y - 1);
  40. }
  41. } else {
  42. if (obstacleGrid[x][y - 1] == 1) {
  43. return uniquePathsWithObstacles(obstacleGrid, x - 1, y);
  44. } else {
  45. return uniquePathsWithObstacles(obstacleGrid, x - 1, y) + uniquePathsWithObstacles(obstacleGrid, x, y - 1);
  46. }
  47. }
  48. } else {
  49. if (obstacleGrid[x - 1][y] == 1) {
  50. return 0;
  51. } else {
  52. return uniquePathsWithObstacles(obstacleGrid, x - 1, y);
  53. }
  54. }
  55. } else {
  56. if (obstacleGrid[x][y - 1] == 1) {
  57. return 0;
  58. } else {
  59. return uniquePathsWithObstacles(obstacleGrid, x, y - 1);
  60. }
  61. }
  62. }
  63. /**
  64. * 迭代法
  65. *
  66. * @param obstacleGrid
  67. * @return
  68. */
  69. public static int uniquePathsWithObstacles2(int[][] obstacleGrid) {
  70. int m = obstacleGrid.length, n = obstacleGrid[0].length;
  71. int[] columns = new int[n];
  72. if (obstacleGrid[0][0] == 1) {
  73. columns[0] = 0;
  74. } else {
  75. columns[0] = 1;
  76. }
  77. // 初始化第一行
  78. for (int i = 1; i < n; i++) {
  79. if (columns[i - 1] == 0) {
  80. columns[i] = 0;
  81. continue;
  82. }
  83. if (obstacleGrid[0][i] == 1) {
  84. columns[i] = 0;
  85. } else {
  86. columns[i] = 1;
  87. }
  88. }
  89. // 迭代过程
  90. for (int i = 1; i < m; i++) {
  91. // 第一个值
  92. if (columns[0] == 0) {
  93. for(int x = 1; x < n; x++) {
  94. if(obstacleGrid[i][x] == 1) {
  95. columns[x] = 0;
  96. } else {
  97. columns[x] = columns[x - 1] + columns[x];
  98. }
  99. }
  100. continue;
  101. }
  102. if (obstacleGrid[i][0] == 1) {
  103. columns[0] = 0;
  104. } else {
  105. columns[0] = 1;
  106. }
  107. for (int j = 1; j < n; j++) {
  108. if (obstacleGrid[i][j] == 1) {
  109. columns[j] = 0;
  110. } else {
  111. columns[j] = columns[j] + columns[j - 1];
  112. }
  113. }
  114. }
  115. return columns[n - 1];
  116. }
  117. public static void main(String[] args) {
  118. int[][] obstacleGrid = new int[][]{{0}, {1}};
  119. System.out.println(uniquePathsWithObstacles(obstacleGrid));
  120. System.out.println(uniquePathsWithObstacles2(obstacleGrid));
  121. }
  122. }

【每日寄语】 机会不会等你,错过以后可能不会再有。