4982: Stepping on the grid view submission statistics Total time limit: 1000ms memory limit: 65536kB description
There is a grid matrix, and the matrix boundary is infinitely far away. Let us make the following assumptions:
A. Each step, you can only move one block from the current square and walk to an adjacent square;
B. The passing of the lattice immediately collapsed can no longer go the second time;
C. Only go north, east and west in three directions;
Excuse me: If we allow n steps on the grid matrix, how many different schemes are there? 2 ways to go is considered a different scheme as long as there is a difference.
Input of the number of steps N (n <= 20) output allowed to walk on the squares sample input
2
Sample output
7
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int f[30];
int n,i,j;
int main ()
{
scanf ("%d", &n);
F[0]=1;
f[1]=3;
for (i=2;i<=n;i++)
f[i]=f[i-1]+f[i-2]*2+ (f[i-1]-f[i-2]);
cout<<f[n];
} F[i-1] said to go north, F[i-2] said that the previous step is to go north, then this time things can go, (f[i-1]-f[i-2]) that the previous step is from the direction of things come over, then there is only one way to go east or west
PS: The magical idea of a great God uses three variables to indicate eastward and northward, with an initial value of 1. East and west of the way to the next two options, and north of the next step there are three options, the last three ways to sum.