[Careercup 8.2] robot path

Source: Internet
Author: User

Imagine a robot sitting on the upper left hand corner of an nxn grid the robot can only move in two directions ctions: right and down how many possible paths are there for the robot?

A robot can only go down or to the right in the upper left corner of a nxn square. How many paths are there in the lower right corner?

Analysis: there are only two options for a robot to go down or to the right in a square. We use F (m, n) to represent the number of paths that the robot goes down or down to the bottom right corner of an mxn rectangle. We can easily obtain the recursive formula: F (m, n) = f (m-1, n) + f (M, n-1 ). F (m-1, n) indicates when the robot moves down, and F (M, n-1) indicates when the robot moves down to the right. Note that F (1, N) = f (M, 1) = 1.

Based on this, we can easily write recursiveCode:

// Number of paths that the robot only follows to the right or down on a m * n rectangle

Int get_paths (int m, int N)
{
// For a rectangle of 1 * or * 1, there is only one path.
If (M = 1 | n = 1)
Return 1;
// Walk down + walk right
Return get_paths (m-1, n) + get_paths (m, n-1 );
}

// Number of paths that the robot only follows to the right or down on an N * n Square
Int get_paths (int n)
{
Return get_paths (N, N );
}

However, we find that such Code involves a lot of repeated computations. We can store recursive results in an array. If we find that there is no such result, we will calculate it recursively. If so, we will use it directly.

In addition, we can use dp (the idea of Dynamic Planning) to solve this problem. We can see from recursive writing that the results of problems often depend on the results of subproblems. The idea of DP is that, no matter whether it is useful or not, we calculate the values of all sub-problems, and then the sub-problems constitute the final problems we require. The Code is as follows:

// The number of paths that the robot walks to the right or down on a n * n Square. The DP solves the problem.
Int get_paths_dp (int n)
{
Int ** A = new int * [N];
Int I, J;
For (I = 0; I <n; I ++)
A [I] = new int [N];
// Initialization. For a rectangle of 1 * or * 1, there is only one path.
For (I = 0; I <n; I ++)
{
A [0] [I] = 1;
A [I] [0] = 1;
}

for (I = 1; I for (j = 1; j A [I] [J] = A [I-1] [J] + A [I] [J-1];
J = A [n-1] [n-1];
for (I = 0; I Delete [] a [I];
Delete [] A;
return J;
}

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.