Bzoj 1087: [SCOI2005] Non-invasive king (pressure DP)

Source: Internet
Author: User

Simple-like pressure dp ...

The DP (x, H, s) represents the current X line, uses H king, and the current row has a state of S.

Consider transfer: DP (x, h, s) =∑dp (X-1, h-cnt_1 (s), S ') (s and S ' two rows do not conflict, cnt_1 (s) indicates how many King s states are used)

I have all sorts of preprocessing, so code's equation is a little different.

----------------------------------------------------------------------------------

#include <cstdio>

#include <cstdlib>#include <cstring>#include <algorithm>#include <vector>#include <iostream>#define CLR (x, C) memset (x, C, sizeof (x))#define REP (i, n) for (int i = 0; i < n; ++i)#define B (i) (1 << (i))using namespace std;typedef long Long ll;const int MAXN = ten;const int maxstate = +;vector< int > state;vector< int > cnt;vector< int > ok[maxstate];int n, K;ll d[MAXN] [MAXN * MAXN] [maxstate];void init () {state.clear (), Cnt.clear ();CLR (d,-1);CLR (d[0], 0);cin >> n >> K;Rep (s, b (n)) if (! (S & (S << 1))) {State.push_back (s);int t = 0;for (int h = s; h; h >>= 1) T + = h & 1;Cnt.push_back (t);d[0 [t] [State.size ()-1] = 1;}Rep (i, State.size ())ok[i].clear ();Rep (i, State.size ())For (int j = i; J < State.size (); j + +)if (! (state[j] & state[i]) &&! ((state[j] << 1) & state[i]) &&! ((state[i] << 1) & state[J])) {ok[I].push_back (j);if (i! = j) ok[J].push_back (i);    }}ll DP (int x, int h, int t) {if (H < 0) return 0;ll &ans = d[x [h] [t];if (ans! =-1) return ans;ans = 0;if (H < cnt[T]) return ans;For (vector< int >:: iterator it = ok[T].begin (); it! = ok[T].end (); it++)ans + = DP (x-1, h-cnt[T], *it);return ans;}void Work () {ll ans = 0;Rep (i, State.size ())ans + = DP (n-1, k, i);cout << ans << "\ n";}int main () {//freopen ("test.in", "R", stdin);//freopen ("Test.out", "w", stdout);init ();Work ();return 0;} 

----------------------------------------------------------------------------------

1087: [SCOI2005] Non-aggression king Time Limit:Ten Sec Memory Limit:162 MB
Submit:1974 Solved:1171
[Submit] [Status] [Discuss] 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 Input 3 2Sample Output -HINT

Source

Bzoj 1087: [SCOI2005] Non-invasive king (pressure DP)

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.