Problem P (square move)

Source: Internet
Author: User

Problem description in an infinitely large two-dimensional plane, we make the following assumptions: <br>1, &nbsp;&nbsp; can only move one grid at a time; <br>2, &nbsp;&nbsp You can't go backwards (assuming your destination is "up", you can go left, you can go right, you can walk up, but you can't go down); <br>3, &nbsp;&nbsp; pass the lattice immediately collapse can not go the second time; <br ><br> the number of different scenarios for n-Steps (2 methods are considered different if one step is not the same). <br> input first gives a positive integer c, which indicates that there is a C set of test data <br> the next C row, each line contains an integer n (n<=20), indicating that you want to walk n steps. <br> Output Please program the total number of different programs to go n steps;<br> the output of each group is one row. <br>

Sample Input212

Sample Output37

Idea: Nth step, f (n) = a (n) + b (n); a (n) represents the N-step upward approach, B (n) represents the Walk of N-step, the number of steps up only one option is the last step added: A (n) =a (n-1) +b (n-1) (Front (n-1) step up the number of steps to go up + (n-1) step to the left or right of the number of steps), and because the past can not return, so left or right to walk only one way, but the upward walk can be left upper and upper right two, so B (n) =2*a (n-1) +b (n-1), the reduction of F (n) =2*f (n-1) +f (n-2);

Code:

#include <iostream>
#include <string.h>
using namespace Std;
int f[20];
int main ()
{
int c = 0;
int n = 0;

memset (F,0,sizeof (f));
F[1] = 3;
F[2] = 7;
for (int i = 3;i <= 20;i++) {
F[i] = 2*f[i-1] + f[i-2];
}

CIN >> C;
while (c--) {
CIN >> N;
cout << F[n] << Endl;
}
return 0;
}

Problem P (square move)

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.