"Shanghai Jiaotong University OJ" Smooth toothpick bag (state compression DP)

Source: Internet
Author: User

1383. The smooth Toothpick Bag title description

Chang Chang said: "You all say that the brain watt, I think you are the brain watt--"

In order to prevent us from exposing his chǐ again, Chang Chang tried to destroy our display, he took out his own collection of toothpick bag, the size of a 1x2 rectangle, he wanted to use the way to cover the display of the secret. Display size is WxH rectangle, Chang Chang the bag on the back of the glue, began a piece glued to the display, the request can not have a bag overlap, there is no display of a part of the screen is not covered, packaging bags placed in only two cases: horizontal and vertical put.

Smooth for his careful planning when the brain is also Watt, he wants you to help him calculate how many different secret paving methods for each display.

Description
    1. Think that the display is a direction, that is, symmetrical dense paving method to count the repeat count, such as 2x2 display of the scheme has two, one is 2 bags are horizontal, the other is two bags are vertically placed.

    2. The output is 0 if it is not covered with a secret.

    3. The toothpick bag is enough for the pack.

Input format

The input includes several lines, each with two integers Wi and Hi, indicating the size of the display, the input data to wi=hi=0 as the end, each display size 1<=hi,wi<=11.

Output format

For each line of input, an integer is output each time to indicate the total number of schemes that are covered by the display. No output is required when wi=hi=0.

Sample Input
1 21 31 42 22 32 42 114 110 0
Sample Output
10123514451205
Tips

Chang Chang said that if the Wixhi is odd, the number of viable programs is 0.

Chang Chang reasoning, think that the behavior unit to fill a row of rows, while using state compression to represent the fill status of each row, 0 means that empty 1 is filled, such as a line with a length of 3 has 000,001,010,011,100,101,110,111 of such state representations.

Chang Chang felt that the dynamic planning is too difficult, only violent search.

Chang Chang said 30% test points only one monitor, 70% test points only no more than 10 monitors, 100% test points only no more than 121 monitors

Chang Chang said the answer is very small, long long enough

The problem of paving bricks. Each behavior at a stage, a maximum of 2^11 state, then enumerate the state of the line is good, in the case of the state of this row, enumerate the state of the previous row, check whether it is compatible (if the previous line is 0, there is no shop, this line corresponds to the position must be 1, if the previous row is 1, this line can be 0 However, it should be noted that if the 1, the two lines of this position is tiled, or there must be one of 0 (the next line to spread), then because it is tiled, the next position of the two rows are 1, otherwise the state is incompatible), if compatible plus the corresponding number.

The complexity is h* (2^w) * (2^W). Can also be a point to move the return, this has not been achieved, temporarily do not watch.

Code:

1#include <iostream>2#include <cstring>3 using namespacestd;4  5 intw,h;6 Long Longdp[ One][1<< One];7 Long LongFirst_line (intN)8 {9     intt =1;Ten      for(inti =1; I <= w;++i,t <<=1) One     { A         if(T &N) -         { -             if(I==w | |! (n& (t<<1)))return 0; the             Else -             { -i++; -T <<=1; +             } -         } +     } A     return 1; at } - BOOLMatchintNintl) - { -     intt =1; -      for(inti =1; I <= w;++i,t<<=1) -     { in         if(T &N) -         { to             if(T & L)//both lines are 1 +             { -                 if(I==w | |! (N & (t<<1)) && (L & (t<<1))) )return 0; the                 Else *                 { $i++;t<<=1; Panax Notoginseng                 } -             } the         } +         Else if(! (T & L))return 0; A     } the     return 1; + } - intMain () { $   $      while(1) -     { -Cin>>w>>h; the         if(h==0&& w==0) Break; -         if((H*W) &1)//edge length is oddWuyi         { thecout<<0<<Endl; -             Continue; Wu         } -         if(H&LT;W)//Reduction of complexity About         { $H = h^W; -W = h^W; -H = h^W; -         } AMemset (DP,0,sizeof(DP)); +          for(inti =0; I <1<<w;++i) dp[0][i] = First_line (i);//Initialize the          for(inti =1; I < h;++i) -              for(intj =0; J <1&LT;&LT;W;++J)//Current row State enumeration $                  for(intK =0; k <1&LT;&LT;W;++K)//previous line State enumeration the                 { the                     if(Match (J,K)) dp[i][j] + = dp[i-1][k]; the                 } thecout<<dp[h-1][(1&LT;&LT;W)-1]<<Endl; -     } in   the     return 0; the}
View Code

"Shanghai Jiaotong University OJ" Smooth toothpick bag (state compression DP)

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.