"Bzoj 1087" [SCOI2005] non-aggression king

Source: Internet
Author: User

Description

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.

Input

Only one row, containing two numbers n,k (1 <=n <=9, 0 <= K <= n * N)

Output

Number of scenarios.

Sample Input3 2Sample Output -The classmate had already played the watch before, Orzthe positive solution pressure Dp,f[i][s][k] indicates that the state of the line on the first row is S, with K selectedpre-processing all the possible states, DP.
1#include <cstdio>2#include <cstring>3 #definell Long Long4 using namespacestd;5 intgs[520],a[520][ One];6 BOOLpd[520];7 intn,k;8ll ans,f[ One][520][ -];9 BOOLOkintXinty) {Ten      for(intI=1; i<=9; i++)if(a[x][i]&& (a[y][i]| | a[y][i-1]|| a[y][i+1]))return 0; One     return 1; A } -   - intMain () { thescanf"%d%d",&n,&K); -memset (PD,1,sizeof(PD)); -      for(intI=1; I<= (1<<n)-1; i++){ -         intTmp=i,cnt=0; +          while(TMP) { -a[i][++cnt]=tmp&1; gs[i]+=1&tmp; +tmp>>=1; A         } at          for(intj=1; j<=n;j++)if(a[i][j]==1&&a[i][j+1]==1){ -pd[i]=0; Break; -         } -         if(Pd[i]) f[1][i][gs[i]]+=1; -     } -f[1][0][0]=1; in      for(intI=2; i<=n;i++) -          for(intj=0; J<= (1<<n)-1; j + +){ to             if(!pd[j])Continue; +              for(intk=0; K<= (1<<n)-1; k++){ -                 if(OK (j,k)) { the                      for(intl=gs[k];l<=k;l++) *f[i][k][l]+=f[i-1][j][l-Gs[k]]; $                 }Panax Notoginseng             } -         } the      for(intI=0; I<= (1<<n)-1; i++)if(Pd[i]) ans+=F[n][i][k]; +printf"%lld", ans); A}

"Bzoj 1087" [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.