"State compression DP" bzoj1087-[scoi2005) non-aggression King

Source: Internet
Author: User

"The main topic"

Put K Kings in NxN's chessboard, so that they do not attack each other, there are many kinds of layout plan. The king can attack it up and down, as well as the left upper left lower right up to the bottom right in the next eight directions in the vicinity of a grid, a total of 8 squares.

Ideas

The feasible state of each row is preprocessed first (i.e. there is no adjacent 1 in a single line) and is stored in the usable.

Then the preprocessing usable between 22 can be converted to each other and stored in the map.

F[i][j][k] line I, has been used to J King, the current line status of Usable[k] situation, the four-cycle violence recursion can be!

Here with the usable the truth should be faster, but with POPOQQQ speed is the same? However, this optimization should be effective!

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6typedefLong Longll;7 Const intmaxn= +;8 Const intmaxk= -;9 Const intmaxm= the;Ten intn,m; One intMAP[MAXN][MAXN]; All F[MAXM][MAXK][MAXN];//F[i][j][k] line I, has been used to J King, the current line state is K - intUSABLE[MAXN]; - intDIGIT[MAXN]; the   - intUsable (intx) - { -     if(x<<1&x | | X>>1&AMP;X)return 0;Else return 1; +     //cannot have adjacent 1 - } +   A intGet_digit (intx) at { -     intret=0; -      while(x) ret+=x&1, x>>=1; -     returnret; - } -   in intJudge (intXinty) - { to     if(X&y | | (x<<1) &y | | (x>>1) &y)return 0;Else return 1; +     /*There are three types of cases that are not feasible*/ - } the   * voidInit () $ {Panax Notoginsengmemset (Usable,0,sizeof(usable)); -memset (Map,0,sizeof(map)); the      for(intI=0;i<1<<n;i++) +         if(Usable (i)) usable[++usable[0]]=i; A     /*pre-processing of a viable state (no adjacent 1 can be left and right)*/ the      for(intI=1; i<=usable[0];i++) +          for(intj=1; j<=usable[0];j++) -         {  $             if(Judge (Usable[i],usable[j])) map[i][j]=1; $         }  -     /*The state of being able to be converted in a feasible state of preprocessing*/ -      for(intI=1; i<=usable[0];i++) digit[i]=get_digit (Usable[i]); the     /*the number of 1 in each feasible state is preprocessed*/ - }Wuyi   the ll DP () - { WuMemset (F,0,sizeof(f)); -f[0][0][1]=1; About      for(intI=1; i<=n;i++) $          for(intj=0; j<=m;j++) -              for(intk=1; k<=usable[0];k++) -                 if(digit[k]<=j) -                 { A                      for(intL=1; l<=usable[0];l++) +                     { the                         if(Map[l][k] && digit[l]+digit[k]<=j) -f[i][j][k]+=f[i-1][j-Digit[k]] [l]; $                     } the                 } thell ret=0; the      for(intI=1; i<=usable[0];i++) ret+=F[n][m][i]; the     returnret; - } in   the intMain () the { Aboutscanf"%d%d",&n,&m); the init (); theCOUT&LT;&LT;DP () <<Endl; the     return 0;  +}

"State compression DP" bzoj1087-[scoi2005) non-aggression King

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.