Luogu-P1896 [SCOI2005] non-aggression

 

topic

Topic Link

 

Test score:  100

 

 

Main Algorithm: shaped pressure DP (binary optimization)

 

 

Casual working:

   Board shaped pressure DP

 

analysis

  1. This question is for the king, somewhat similar to the eight queens problem, but fewer restrictions at this point, simply put, is less a screening point, it will lead to time-out search

  2. Can it multi-dimensional DP it? For each line, we know that it only related to the previous line with its own, to meet the after-effects of the principle of non-DP, for the answer, the answer is a summary of the front, the popular talk is to satisfy the principle of sub-optimal structure, yongDP

  3. The number of the king but for too many values ​​DP state transition needed, the front row of the state and the state of the line there should be selected

  4. For state record tedious, but for only 01 states two options, put the hold, so it can be viewed as a binary string of 01 numbers, then each state corresponds to a single decimal number, which is the core idea of ​​this theme like pressure

  5. Well, the code

  Code

#include<stdio.h>
#include<stdlib.h>
#define LL long long
#define FORa(i,s,e) for(LL i=s;i<=e;i++)
#define FORs(i,s,e) for(LL i=s;i>=e;i--)

using namespace std;

const LL N = 10, SUM = 1024, TRUES = 144; / * Each row Knights distribution lawful state and not so much, I calculated a maximum of only 144 legal status 
But it does not matter to open Ha, new to it, but I still cheap hand changed 
*/
LL n,k;
LL I ans, whether the [true + 1], s [true + 1 ];
LL f[N+1][TRUES+1][N*N+1];
/* Legitimate program f [i] [j] [k] represents the state before the i-th row and i-th row is the number j && Knight arrangement when k is j originally a one-dimensional array of n elements, represents the state of the i-th row, but for only 01 states two options, put and hold So it can be viewed as a binary string of 01 numbers, then each state corresponds to a single decimal number, which is the core idea like this title pressure 
*/ void PreS () {LL CNT; for (I = 0 LL, n-in = <<. 1; I <in; I ++) // enumerate all states, selected in a legal state {if (i & (i << 1)) continue; // if two together, then 1, 0 = direct filtered CNT ; for (LL J = 0; J <n-; J ++) // this state the accumulated number of 1 occurs, which is placed in this state Knight number of IF (I & (<<. 1 J)) CNT ++ ; S [SUM ++] = I, NUM [SUM] = CNT; // store }} void the Solve () {F [0] [. 1] [ 0] = 1; // 0 represents the first row and hold when choices 1, why the second subscript is 1, because the S [1] is a length n of a string with only Fora 0 ( i, 1 , the n-) Fora (now, 1, SUM) // enumerate the current state of this line FORa (pre, 1, sum) // this line because now the state as to the relevant line (the principle of optimal substructure and no principle aftereffect), the line enumeration state if (! (s [now] & s [pre])) // if there is no column number x between two upper and lower rows are 1, continued Fora ( tot, num [now], k ) if ((! (s [now] & (s [pre] << 1))) && (! (s [now] & (s [pre] >> 1))) ) // determines the diagonal meets the requirements of F [I] [now] [TOT] + = F [. 1-I] [pre] [tot- NUM [now]]; Fora (I,. 1, SUM) = + ANS f [n] [i] [ k]; // accumulating answer the printf ( "% LLD" , AN s);k); Pre(),Solve(); return 0; }

 

to sum up:

  To determine the status of 1.DP

 

Guess you like

Origin www.cnblogs.com/SeanOcean/p/11354535.html