[Bzoj 1087] [scoi2005] King

Source: Internet
Author: User
Description

Put K kings in the n × n board so that they do not attack each other. The king was able to attack it, from top to bottom, and from top to left, from top to bottom, right to bottom, and from top to bottom, a grid near each other. There were 8 grids in total.

Input

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

Output

Number of solutions.

Sample input3 2
Sample output16
Hint

Source

At the beginning, I thought it was a violent search of eight queens and was almost pitted. I didn't expect that I could put multiple chess pieces on one line and the game rules were different. I relied on them...

After reading the question solution, we can find that it is DP, but there are too many States (only one row can have a maximum of 2 ^ 9-1). We certainly cannot use a common DP, however, you can obtain a large number of useless States for each row from the question. Therefore, you can consider pressing the DP and first remove all useless States for each row, leave only useful states (obviously this step is done with DFS), and the number of States is much less, resulting in considerable computing complexity, during DP preprocessing, the number of solutions for processing the known status and the number of known chess games for each row is 1. Then, the number of DP solutions is accumulated once and all the solutions are collected once.

What's worse is that bzoj's question seems to have only one point as small-scale data, and the others are all in a large scope. I mistakenly thought that the output result would not exceed int, but it eventually exceeded, wa once, but I had no choice but to change the long int to eventually get rid of it. I figured it was possible to exceed the int.

// DFS pre-processing + pressure DP: compresses 2 ^ 9-1 states into M feasible states, greatly reducing the number of cycles # include <stdio. h> # define maxn 100 long int f [maxn] [maxn] [600], ans; // F [I] [J] [k] = number of solutions in which row I is in K State, ans = number of final solutions int stay [maxn]; // stay [I] = int map [maxn] [maxn], CNT [maxn]; // CNT [I] = number of pawns corresponding to the I state int N, K, M = 0; void pre_dfs (INT X, int POs, int now) // DFS pre-processing lists the conflicting states in the same row. X indicates the number of removed pawns, N indicates the position of the currently released pawns, and now = the status of the current row {int I; stay [++ m] = now; // adds a feasible state CNT [m] = x; If (x> = (n + 1)/2 | x> = k) return; // if the number of pieces already placed exceeds half of the grid, it is obviously not allowed. Exit for (I = POS + 2; I <= N; I ++) pre_dfs (x + 1, I, now + (1 <(I-1 ))); // enumerate the position where the next piece is placed} void pre_map () {int I, j; for (I = 1; I <= m; I ++) // The status of the first row for (j = 1; j <= m; j ++) // status of the second row {map [I] [J] = map [J] [I] = (stay [I] & stay [J]) | (stay [I]> 1) & stay [J]) | (stay [I] <1) & stay [J])?; // When a point in the first row and a point in the second row is diagonal or the same column, the two rows conflict} for (I = 1; I <= m; I ++) // DP preprocessing f [1] [CNT [I] [I] = 1;} int main () {int I, j, now, h; scanf ("% d", & N, & K); pre_dfs (0,-); // preprocessing lists all feasible solutions for the same row, reduce the number of DP loops pre_map (); // pre-process conflicts between top, bottom, and left, and DP initialization for (I = 2; I <= N; I ++) // I-row for (j = 0; j <= K; j ++) // J pawns for (now = 1; now <= m; now ++) {If (CNT [now]> J) continue; // The number of currently placed pawns is less than the number of pawns in the current row. It is obviously not consistent with the meaning of the question and skipped for (H = 1; h <= m; H ++) // enumerate the status of the previous row if (Map [H] [now] & CNT [H] + CNT [now] <= J) f [I] [J] [now] + = f [I-1] [J-CNT [now] [H]; // meets the conditions, add the number of feasible solutions} for (I = 1; I <= m; I ++) ans + = f [N] [k] [I]; // statistical answer printf ("% LLD \ n", ANS); Return 0 ;}

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.