Rokua 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

#include <cstdio>#include<cstring>#include<iostream>using namespacestd;intN,k,i,j,w,m;//N*n's chessboard, K kings.intt[1010];intf[ -][522][ the];//F[i][j][k] Indicates the number of schemes for the K-Kings of the I-row J-State placementintCheckintXintY//x on, y under, 1 to meet condition, 0 for not satisfied{    if((x&y)! =0)return 0;//if x and Y are both up and down in 1 cases, return 0;    if((x& (x<<1)) >0)return 0;//if x turns into binary, there is an adjacent 1 return 0;    if((y& (y<<1)) >0)return 0;//if y turns into binary, there is an adjacent 1 return 0;    if((y& (x<<1)) >0)return 0;//if X is in conflict with the lower left of Y, return 0;    if((x& (y<<1)) >0)return 0;//if X is in conflict with the lower right of Y, return 0;    return 1;//Otherwise, it returns 1 .}intMain () {CIN>>n>>K; M=(1<<n)-1;//2^k-1     for(i=1; i<=m;i++)//ask for a T arrayT[i]=t[i>>1]+ (i&1);//I turn to 2 after the number of 1, why use bit arithmetic: Save time! f[0][0][0]=1;//Initial value     for(i=1; i<=n;i++)//pieces held       for(j=0; j<=m;j++)//enumerating the bank's state         for(w=0; w<=m;w++)//enumerate the previous row state            if(check (w,j) = =1)//If this placement method is legal            {                 for(intkkk=t[j]+t[w];kkk<=k;kkk++)//T[j]+t[w] For the kings of these two lines, K for the number of kings to be placed altogetherf[i][j][kkk]=f[i][j][kkk]+f[i-1][W][KKK-T[J]/*minus the number of kings, the rest of us.*/];//add it in .            }    intsum=0;  for(i=0; i<=m;i++) Sum+=F[N][I][K];//Statisticsprintf"%d", sum);//Output    return 0;}

Rokua 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.