HDU 1693 Eat the Trees (plug DP)

Source: Internet
Author: User

Test instructions: To a n*m matrix, for 1 o'clock to represent the empty lattice, for the 0 o'clock for the barrier lattice, ask if not through the barrier lattice, can draw one to many circles, how many kinds of programs? (N<12,M<12)

Ideas:

The DP transfer is performed in a non-barrier lattice, so a scrolling array can be used. Only need to save the state of M+1 plug, one of which is the right plug, the other is the next plug, if there is a plug, the bit is 1, otherwise 0, the initial time is 0.

It should be considered that (1) if the two edges are plugs, then they must be connected, (2) If only one side is the plug, then the continuation of the plug, there can be two continuation direction (lower and right), (3) If there is no plug, then you must open another two new plug (new connected components).

For example, the status of the record is: 101111. Because the state is saved by row, the first lattice needs special consideration, moving all States to the left one, and the last one is the edge of the right direction. Assuming that the uplink has a lower plug, then the initial line is 011111, you can see the leftmost is 0, indicating no right plug, note: I was saved according to 111110, that is, the lowest bit is the leftmost.

  

The initial lattice dp[0][0]=1, and the answer is dp[cur][0], there must be no plug presence of the state, all the circles are complete.

1#include <bits/stdc++.h>2 #definePII pair<int,int>3 #defineINF 0x3f3f3f3f4 #defineLL Long Long5 using namespacestd;6 Const intn= -;7 intg[n][n], cur;8LL dp[n][1<<N];9 Ten voidClear () One { Acur^=1; -memset (Dp[cur],0,sizeof(Dp[cur])); - } the  -LL Cal (intNintm) - { -dp[0][0]=1;//there was no plug at the beginning . +      for(intI=0; i<n; i++)//enumeration Lattice -     { + Clear (); A          for(intk=0; k< (1&LT;&LT;M); k++) dp[cur][k<<1]+=dp[cur^1][K];//The highest bit is automatically ignored at          for(intj=0; j<m; J + +) -         { -             intR= (1&LT;&LT;J), d= (1<< (j+1));//R and D are equivalent to right and bottom - Clear (); -              for(intk=0; k< (1<< (m+1)); k++)//Enumeration State -             { in                 if(G[i][j])//Space -                 { to                     if((K&r) && (k&d))//plug on both sides: Connect, no plug. +dp[cur][k^r^d]+=dp[cur^1][k]; -                     Else if(K&r | | k&d)//one side has a plug: can be turned two directions the                     { *dp[cur][k]+=dp[cur^1][k]; $dp[cur][k^r^d]+=dp[cur^1][k];Panax Notoginseng                     } -                     Else                        //No plug: Open two new plugs separately thedp[cur][k|r|d]=dp[cur^1][k]; +                 } A                 Else       //Barrier Lattice the                 { +                     if( ! (K&AMP;R) &&! (k&d)) -dp[cur][k]=dp[cur^1][k]; $                 } $             } -         } -     } the     returndp[cur][0]; - }Wuyi  the intMain () - { Wu     //freopen ("Input.txt", "R", stdin); -     intN, M, T, case=0; AboutCin>>T; $      while(t--) -     { -scanf"%d%d",&n,&m); -Memset (G,0,sizeof(g)); AMemset (DP,0,sizeof(DP)); +          for(intI=0; i<n; i++) the              for(intj=0; j<m; J + +) -scanf"%d",&g[i][j]); $printf"Case %d:there is%lld ways to eat the trees.\n", ++Case , Cal (N,m)); the     } the     return 0; the}
AC Code

HDU 1693 Eat the Trees (plug 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.