P1896 [SCOI2005] Non-aggression king

Source: Internet
Author: User

Title 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/output format

Input format:

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

Output format:

Number of programmes received

Input and Output Sample input example # #:
3 2
Sample # # of output:
16

Puzzle: State compression motion rules, with an integer binary representation of the Board on the case of a row, put the pawn is 1, not put as 0, you can preprocess all possible states, using a bitwise operation to determine whether two states can coexist in two adjacent rows. (The bit arithmetic is a bit blind).
#include <iostream>#include<cstdio>using namespacestd;intn,k,i,j,k1,all,f[9][ -][ +],cnt[ +],p;BOOLf1[ +],f2[ +][ +];Long Longans;voidxx () {intt,x;  for(; i<all;i++)        if((i& (i>>1))==0)        {             for(t=0, x=i;x;x/=2) t+=x&1; Cnt[i]=T; F1[i]=true; }     for(i=0; i<all;i++)      if(F1[i]) for(j=0; j<all;j++)          if(F1[j])if((i&j) = =0&& (i& (j>>1))==0&& (j& (i>>1))==0) F2[i][j]=true;}intMain () {scanf ("%d%d",&n,&k); if(K> (n+1)/2* (n+1)/2) {cout<<0<<Endl; return 0; } All=1<<N;    XX ();  for(i=0; i<all;i++) f[0][cnt[i]][i]=1;  for(i=1; i<n;i++)         for(j=0; j<all;j++)          if(F1[j]) for(k1=0; k1<all;k1++)                if(F2[j][k1]) for(P=cnt[j];p +cnt[k1]<=k;p++) F[i][p+cnt[k1]][k1]+=f[i-1][p][j];  for(i=0, n--;i<all;i++) ans+=F[n][k][i]; cout<<ans; return 0;}
pressure DP




P1896 [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.