"The Problem" the fifth edition of Online programming contest month Two: Walk lattice

Source: Internet
Author: User

Topic details

We have a two-line N-Lege chessboard that you can start from any location. Each time you can walk up and down and diagonally in the direction of a grid (can't go out), how many possible Hamilton road? (That is, all the squares are only once in a way.) )

For example:

A b C

D E F

One possible path is b,f,c,e,d,a.

Input format:

Multiple sets of data, 1 rows per set of data, containing a positive integer n representing the number of columns. (n <= 1000)

Output format:

Each set of data output row contains an integer, possibly the number of path bars. Result is larger, output to 10^9 + 7 result

Answer Instructions

Input sample

1

2

3

Sample output:

2

24

96

This problem needs to abstract the topic, then the abstract model of recursive summary of the law, and finally used the same remainder theorem.

First of all, the 2 x-n squares are asked to find out how many Hamiltonian paths there are. Interestingly, you can walk up and down the left and right and diagonal directions in a grid.
Simulate it, suppose there is a villain from a starting point, not looking at the longitudinal direction, only look at the horizontal direction, then for 2 x N such a square, the placement will have n choice, assuming that in the left to the right from the first lattice group, then there will be 2 ways of placement (above or placed below) so left or right to move, There are also two ways to move (for example, to the right, to move to the right or to move to the lower right two ways)
In other words, for squares of 2 x N, traversing all the squares will be 2 of the n-th.
To facilitate the resolution of the problem, we need to block out the number of moving possibilities, and then do this abstraction:
The 2 x n squares are abstracted into an n-dimensional vector, defining the meaning of the number within the I dimension of the vector as follows: A few times from the left-to-right group I have been passed.
In the initial case the vector must be a 0 vector, and the element in the vector must be a nonnegative integer less than or equal to 2.
Consider the sample:
N=1, there are 2 options, this sample can be ignored
N=2, there are 24 options, we divide 24 by 2 two to get 6,6 that is the number of paths within our abstract model.
N=3, there are 96 options, we divide 96 by 2 three to get 12,12 is the number of paths within the model we abstracted.

N=1 is a one-dimensional vector, so you don't have to think too much
N=2 is a two-dimensional vector and we need to discuss it further.
If you place the move pointer initial point at the 1 position, then these three paths are:
[1, 0]-[2, 0], [2,1], [2,2]
[1, 0], [1,1], [2, 1], [2,2]
[1, 0], [1,1], [1,2], [2, 2]
Because it is symmetrical, the move pointer initial point is placed in 2 position, also three path, do not repeat.
Looking closely at these three paths, we can divide them into two categories: one end at 2, the other focusing on the original position.
Let's generalize, for an n-dimensional vector:
First Class:
[1, 0,... 0]-[2, 0,..., 0]->[2,1, 0,..., 0]-&gt, ....
[1, 0,..., 0], [1,1, 0,..., 0], [2, 1,0,..., 0], [2,2, 0,..., 0], [2, 2,1, 0,..., 0] ...
Second Category:
[1, 0,..., 0], [1,1, 0,..., 0], [1, 1,1, 0,..., 0] (,...,1], [,..., 1,2], [,..., 1,2, 2]-[2,..., 2]
This is the only way to go, because in the beginning of the pause in situ, is the first class of the first case, in the middle of the pause or backward, will not meet the requirements of the topic.

We assume that there is a function f (n), meaning an n-dimensional vector, if the starting point is in the 1th position, then the number of possible paths.
So we can summarize a recursive formula like this:
f (n) = f (n-1) + f (n-2) + 1
Of course this is the border situation, if the starting point is non-boundary?
Then the move selection may be left or right only and not persisted. Whether you choose to left or right, you have to go back to the starting point, consider the first type of movement and the second type of movement, whether the original move to the left or to the right, this half of the number of paths is 1 (above the abstract model case). Then the number of movement of the remaining lattice is similar to the boundary condition, in other words, the initial moving point is a non-boundary condition can be obtained by the two initial moving point boundary conditions.
So for the starting point for the left-to-right (I! = 1 and I! = N), you can get the formula:
G (i) = f (i-1) + f (n-i)

To verify authenticity, verify by n=3.
N=3
F (1) = 1;
F (2) = 3;
F (3) = f (1) + F (2) + 1 = 5
There are two boundaries and an intermediate point, and the middle point evaluates to
G (2) = f (1) + f (1) = 2
Then the final result is 2 + 5 * 2 = 12
Then 12 times 2 of three squares: 12 * 8 = 96
Problem solving.

The implementation code is as follows:

#include <iostream>#define MOD (i) (i)% 1000000007Long Longborder[1010] = {0};intBvalid =0;Long Longmodindex[1010] = {0};intMvalid =0;voidInit () {border[0] =0; border[1] =1; border[2] =3; border[3] =5; Bvalid =3; modindex[0] =1; modindex[1] =2; modindex[2] =4; modindex[3] =8; Mvalid =3;}Long LongGetbordermodvalue (inti) { while(Bvalid < i) border[++bvalid] = MOD (border[bvalid-1]+border[bvalid-2]+1);returnBorder[i];}Long LongGetindexmodvalue (inti) { while(Mvalid < i) modindex[++mvalid] = MOD (modindex[mvalid-1]*2);returnModindex[i];}intMain () {using namespace STD; Init ();intN while(Cin>> N) {Long LongGmidvalue =0; for(intI=1; i<=n;i++) {if(i = =1|| i = = N) gmidvalue = MOD (Gmidvalue + getbordermodvalue (n));ElseGmidvalue = mod (gmidvalue + mod (getbordermodvalue (i-1) + Getbordermodvalue (n-i)); }cout<< MOD (Gmidvalue * getindexmodvalue (n)) << Endl; }return 0;}

"The Problem" the fifth edition of Online programming contest month Two: Walk lattice

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.