The camera has a robot sitting in the upper left corner of X * Y. He can only move right and down. How many ways does a robot go from (0, 0) to (X, Y? Advanced hypothesis: some points are "Restricted Area", and robots are not enough. Design an algorithm to find a path for the robot to move from the upper left corner to the lower right corner. This question is related to UniquePaths and UniquePathsI on LeetCode.
The camera has a robot sitting in the upper left corner of X * Y. He can only move right and down. How many ways does a robot go from (0, 0) to (X, Y? Advanced hypothesis: some points are "Restricted Area", and robots are not enough. Design an algorithm to find a path for the robot to move from the upper left corner to the lower right corner. This question is related to the Unique Paths and Unique Paths I on LeetCode.
The camera has a robot sitting in the upper left corner of the X * Y mesh, and can only move right and down. How many ways does a robot go from (0, 0) to (X, Y?
Advanced
Assume that some points are "restricted area" and robots cannot step on. Design an algorithm to find a path for the robot to move from the upper left corner to the lower right corner.
This is the same as the Unique Paths and Unique Paths II on LeetCode.
Unique Paths
A robot is located at the top-left corner of a m X n grid (marked 'start' in the dimo-below ).
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 thr grid (marked 'finish 'in the dimo-below ).
How many possible unique paths are there?
NOTE: m and n will be at most 100.
Unique Paths II
Follow up for "Unique Paths ".
Now consider if some obstacles are added to the grids. How many unique paths wocould there be?
An obstacle and empty space is marked as 1 and 0 respectively in the grid.
For example,
There is one obstacle in the middle of a 3*3 grid as partitioned strated below.
[ [0,0,0], [0,1,0], [0,0,0]]
The total number of Unique paths is 2.
NOTE: m and n will be at most 100.
Solution:
Unique Paths
Public int uniquePaths (int m, int n) {// The DP solution is used here, this solution can avoid integer out-of-bounds to the maximum extent. int [] [] memo = new int [m] [n]; for (int I = 0; I
Unique Paths II
Here, we use a one-dimensional array instead of a two-dimensional array.
public int uniquePathsWithObstacles(int[][] obstacleGrid) { int m = obstacleGrid.length; if(m == 0) return 0; int n = obstacleGrid[0].length; if(obstacleGrid[0][0] == 1) return 0; int[] table = new int[n]; table[0] = 1; for(int i=0; i
0) table[j] = table[j-1] + table[j]; } } return table[n-1]; }