[Dynamic planning] Pku2411--mondriaan ' s Dream

Source: Internet
Author: User
Tags first row
Mondriaan ' s Dream
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 3732 Accepted: 2156

Description squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his ' Toilet series ' (where he had to use his toilet paper to draw on, for all O F His paper is filled with squares and rectangles), he dreamt of filling a large rectangle with small rectangles of width 2 and Height 1 in varying ways.

Expert as he is in this material, he saw at a glance so he ' ll need a computer to calculate the number of ways to fill t He large rectangle whose dimensions were integer values, as well. Help him, so that he dream won ' t turn into a nightmare!

Input the input contains several test cases. Each test case was made up of the numbers:the height H and the width W of the large rectangle. Input is terminated by h=w=0. Otherwise, 1<=h,w<=11.

Output for each test case, output the number of different ways the given rectangle can is filled with small rectangles of Size 2 times 1. Assume the given large rectangle is oriented, i.e. count symmetrical tilings multiple times.

Sample Input

1 2
1 3
1 4
2 2
2 3
2 4
2 One
4
each 0 0

Sample Output

1
0
1
2
3
5
144
51205

Source ULM Local 2000 The main idea: give a n*m board, ask how many different schemes are completely covered with 1*2 dominoes. Analysis: Classic state-compression dynamic programming. Both N and M have a maximum of 11, so state compression can be used, i.e. the state of each row is represented by a binary number. The definition F[i,j] indicates the number of scenarios where the first i-1 row has been completely overwritten and the first row has a state of J. Then F[i,j]=∑f[i-1,k]. K can make the i-1 line completely covered by placing a domino, and the line I state is J. This state transfer can be implemented with DFS. Before you begin dynamic planning, you first use DFS to preprocess the first possible state of the row. For the first row, there are only two possible actions: 1, not put. -->dfs (X+1,now SHL 1); 2, Horizontal. -->dfs (X+2,now SHL 2 or 3), when x equals m+1, the description gets a viable State, Inc. (F[1,now]), and when X is greater than M, exit Dfs. The DFS process for state transitions is similar to this, with just one more vertical operation, using bit arithmetic, left to the reader to think, or refer to the program. Codes:const maxn= (1 shl 11)-1; var N,m:int64; I:longint; F:ARRAY[1..11,0..MAXN] of Int64; Procedure init (x,now:longint); Begin IF X=m+1 then Inc (F[1,now]); If X>m then exit; Init (X+1,now SHL 1); Init (X+2,now SHL 2 or 3); End Procedure Dfs (X,last,new:longint); Begin IF X=m+1 then Inc (F[i,new],f[i-1,last]); If X>m then exit; DFS (x+1,last SHL 1 or 1,new SHL 1); DFS (x+1,last shl 1,new SHL 1 or 1); DFS (x+2,last SHL 2 or 3,new SHL 2 or 3); End Procedure main; var t:longint; Begin if M>n then BEGIN t:=n; N:=m; m:=t; End Fillchar (F,sizeof (f), 0); Init (1, 0); For i:=2 to N do Dfs (1,0,0); Writeln (F[n, (1 SHL m)-1]); End Begin READLN (N,M); While N+m<>0 does begin main; READLN (N,M); End End.

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.