Openjudge tread the square.

Source: Internet
Author: User
Tags time limit
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.

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.