[Like] BZOJ1087 pressure dp

Analysis
to see the title, had a knock on the burst search, the results would go ... dfs sample needs to be improved ...

After careful analysis found that, although the board may seem small, only 9 * 9, but very much the number of states, so the violence was impossible before.
So this should be a topic dp title.

dp dp how should it?
If we know how the line is placed, we will know how many pieces there are a row before i. So the next line we can launch it.
This is dp transfer of this question.

So, we came to the part of the focus of this article: the state of compression.

Compressed state, by definition, is compressed to the state (flower chicken).

Reader: You're not nonsense it? It is simply a joke.
I: Well, well, no kidding, the state of compression means that the state becomes a complex number.

For chestnuts, In this problem, the state very much very complex, if we use an array to record the state, we want to record, which used this line in the end which never used our array will become very ugly, state when the transfer is also a lot of trouble (knock on a lot of words)

For example this question, we will record a line is how placed, we array to open in this way:
DP [I] [J] [A] [B] [C] [D] [E] [F] [G ] [H] [k]
? ? ?
Is not looked after I want to address him I go to Le.
And this way, then, to determine the legality of placing the time necessary (hand! Move! Judgment! Off!)

Then we can not change a way to save the placing of a state?
Obviously, in this question, we can be expressed in binary state.

Because a total of only nine, then the binary state maximum will not exceed 512.

Our state of the binary 10101010 (2), 1 means put a pawn, 0 means no place.
It is judged valid when: For the j-th state and k-th state:
SIT [k] & SIT [j] // vertical
(SIT [j] <<. 1) & SIT [k]
SIT [j] & (SIT [k ] << 1) // diagonally
it is not legitimate. Well understood.

So we can start dfs pre-travel state.
Dp then when we meet to determine whether the column.

Set DP [i] [s] [j] represents the i-th row before put a total of s piece, the current state is the i-th row j-th case.

Then dp [i] [s] [ j] + = dp [i-1] [s-num [j]] [k].
Wherein NUM [j] represents a j-th state how many pieces of a row can be put, k shows a state on the line.

Statistics last answer when we enumerate what the last line is how to put on it.

So the question then done.

#include <bits/stdc++.h>
using namespace std;
long long dp[10][100][1000];
long long sit[1000],num[1000];
int n,K,cnt = 0;
void dfs(int stg,long long sum,int now)
{
if(now>=n)
{
sit[++cnt] = stg;
num[cnt] = sum;
return;
}
dfs(stg,sum,now+1);
dfs(stg+(1<<now),sum+1,now+2);
}
int main(http://www.my516.com)
{
scanf("%d%d",&n,&K);
dfs(0,0,0);
for(int i=1;i<=cnt;i++) dp[1][num[i]][i] = 1;
for(int i=2;i<=n;i++)
{
for(int j=1;j<=cnt;j++)
{
for(int k=1;k<=cnt;k++)
{
if(sit[k]&sit[j]) continue;
if((sit[k]<<1)&sit[j]) continue;
if(sit[k]&(sit[j]<<1)) continue;
for(int s=K;s>=num[j];s--) dp[i][s][j] += dp[i-1][s-num[j]][k];
}
}
}
long long ans = 0;
for(int i=1;i<=cnt;i++) ans += dp[n][K][i];
printf("%lld\n",ans);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

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

Guess you like

Origin www.cnblogs.com/hyhy904/p/11109108.html