CCI9.2 Robot Mobile path

Source: Internet
Author: User
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];    }
   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.