Problem description in an infinitely large two-dimensional plane, we make the following assumptions: <br>1, can only move one grid at a time; <br>2,   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, 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)