HDU 1143 tri tiling (recurrence)

Source: Internet
Author: User
Tri Tiling Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 2118 accepted submission (s): 1211



Problem descriptionin How many ways can you tile a 3xn rectangle with 2x1 dominoes? Here is a sample tiling of a 3x12 rectangle.


 
Inputinput consists of several test cases followed by a line containing-1. Each test case is a line containing an integer 0 ≤ n ≤ 30.
 
Outputfor each test case, output one integer number giving the number of possible tilings.
 
Sample Input
2812-1
 
Sample output
31532131
 
Sourceuniversity of Waterloo local contest 2005.09.24
Recommendeddy | we have carefully selected several similar problems for you: 1133 1267 1207 1249

If n is an odd number, there is no solution.

When N is an even number, a more intuitive idea is to cut the large rectangle out of the left part with a vertical line and then solve it recursively, just like uva_10359, we can recursively obtain the F (n) recursive formula by directly considering how a small rectangle on the left is constructed.

Although this question seems to have been spelled out by many small rectangles at first glance, it is not intuitive to spell out a small rectangle that cannot be split by a vertical line. But if we draw one more painting on the paper, we cannot use a small rectangle split by a vertical line if the length of any horizontal side is X. If X is 2, there are obviously three spelling methods, if X is an even number greater than 2, there are only two spelling methods.

If there are only two ways to spell x> 2, we can try it out. First, there must be two conditions in the first column. The first is a horizontal column above, then a vertical column at the bottom left, and the second is a horizontal column below, then a vertical column is in the upper left corner. Because the two conditions are symmetric, we only discuss the first case.

Now we have already put two pieces together. If there is another vertical column under the horizontal bar, it will obviously become a small rectangle with x = 2, the final fight is not in line with the features we mentioned earlier that cannot be split by a vertical line. Therefore, the horizontal bottom is also the right side of the vertical line, and only two horizontal lines can be placed, after two horizontal la s are placed, we find that there is a small square area on the two horizontal sides. This area can only be wedge into one horizontal area. After the horizontal painting is complete, myGod, we will find an amazing fact. The current structure is the same as the one where we first put a vertical one! Therefore, if we want to continue to splice the rectangle that cannot be split by a vertical line to the right, we can only repeat the previous operation. Therefore, the rectangle with the final horizontal side length of X is uniquely determined.

In the first fight, we only obtained one of the two symmetric conditions. Therefore, if X> 2, there are only two methods to splice a rectangle that cannot be vertically cut.

Then the recursive formula will naturally have, F (n) = 3 * F (n-2) + 2 * F (n-4) +... + 2 * F (0), and then write the recursive formula F (n-2) after the two formula for the difference can be f (n) = 4 * F (n-2) -F (n-4), recursive boundary is F (0) = 1, F (2) = 4.


Code: 0 ms
#include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>using namespace std;#define M 31int f[M]={1,3};int main(){    int i,n;    for(i=2;i<M;i++) f[i]=4*f[i-1]-f[i-2];   while(cin>>n)    {        if(n<0) break;        if(n%2==0) cout<<f[n/2]<<endl;        else cout<<0<<endl;    }    return 0;}

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.