Sgu 223. Little Kings

Source: Internet
Author: User

Time Limit: 0.25 s

Space limit: 4 m

Question:

Place K (k <= N * n) kings (eight adjacent grids can be attacked) on the N * n (n ≤ 10) chessboard ), number of solutions that make them unable to attack each other.

 

 

 

 

 

 

Solution:

State compression is used, and K-bit (k <= N) binary number 1 is used to represent the position of the king placed on the board.

First, use preprocessing to find out the solution that meets the requirements when statu [I] only considers one row. C [I] is the number of pieces placed in the statu [I] State.

 

F [I] [J] [p] indicates the number of solutions for placing J pawns when row I is put in the P state.

 

State transition equation: F [I] [J] [p] = Σ f [I-1] [J-C [J] [PP], (the PP and P meeting the I-1 line do not conflict)

 

Determine if the conflict between P and PP is met

(P & PP) = 0 & (P <1 & PP) = 0 & (PP <1 & P) = 0

 

Finally, the output ans = Σ f [N] [k] [Pi]

 

Reference code:

 

 

#include <iostream>#include <cstdio>#define LL long longusing namespace std;int powT[11];int statu[1 << 10], c[1 << 10];LL f[11][111][1 << 10];int main() {int n, k, tol = 0, t;scanf ("%d %d", &n, &k);for (int i = 0; i <= ( (1 << n) - 1); i++) {if ( ( (i & (i >> 1) ) == 0) && ( (i & (i << 1) ) == 0) ) {statu[++tol] = i;for (t = i; t > 0; t >>= 1)if (t & 1) c[i]++;}}for (int i = 1; i <= tol; i++)f[1][c[statu[i]]][statu[i]] = 1;for (int i = 2; i <= n; i++) {for (int j = 0; j <= k; j++)for (int pu = 1; pu <= tol; pu++)for (int pv = 1; pv <= tol; pv++) {int p1 = statu[pu], p2 = statu[pv];if (j >= c[p1] && (p1 & p2) == 0 && ( p1 << 1 & p2) == 0 && ( p2 << 1 & p1) == 0)f[i][j][p1] += f[i - 1][j - c[p1]][p2];}}LL ans = 0;for (int i = 1; i <= tol; i++)ans += f[n][k][statu[i]];printf ("%lld", ans);}

 

  

 

 

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.