HDU 4529 狀態壓縮DP

2021-08-07 02:20:42 字數 1554 閱讀 9747

題意:

problem description

input

輸入第一行為乙個整數t(1<=t<=8),表示有t組測試資料;

每組資料首先是乙個整數n(1<=n<=10),表示要擺n個騎士上去;

接下來是乙個8

∗ 8的矩陣來描述乙個棋盤,』.』表示這個位置是空的,′∗

′表示這個位置上已經放了皇后了;

資料中的初始棋盤保證是乙個合法的八皇后擺法。

output

對每組資料,請在一行內輸出乙個整數,表示合法的方案數。

sample input

2

1*.......

....*...

.......*

.....*..

..*.....

......*.

.*......

...*....

2*.......

....*...

.......*

.....*..

..*.....

......*.

.*......

...*....

sample output

56

1409

思路:

​ 就像前乙個鄭廠長系列一樣,棋盤的狀態需要壓縮。不同的是這裡不是求出最多放幾個馬,而是給出馬的數量問有多少種在限制條件下放的可能數。顯然之前的三維已經無法滿足這個狀態,需要增加一維馬的數量去遍歷每一行的所有狀態。

​ 定義:dp

[r][

numb

er][

i][j

] 為0~r行一共放置了number個馬滿足的所有情況,第r行狀態為i,r-1行狀態為j。

當算到第r行狀態為i的時候要加上r-1行所有的滿足的狀態j的個數,便是所有此狀態情況,然後遍歷第r行狀態,最後求和既可。

#include 

#include

#include

using

namespace

std;

const

int maxn = 260;

int num[maxn],pos;

intmap[10];

int dp[8][12][maxn][maxn];

void init()

void state_num()

num[pos++] = number;

}}int main()

for(int number = 0;number <= k; number++) }}

for(int r = 2;r < 8; r++) }}

}}

int ans = 0;

for(int i = 0;i < pos; i++)

}printf("%d\n",ans);

}return

0;}

hdu 4529 鄭廠長系列故事 N騎士問題

題目 狀態壓縮dp 好開心,終於會寫簡單的狀態dp了,雖然一開始用long long mle了 雖然第二次陣列開小了,開成dp 8 8 1 8 1 8 了 雖然二進位製用的還很戳,比如求1的個數 雖然.include include includeusing namespace std int t,...

hdu4529鄭廠長系列故事 N騎士問題

include include include include include include include using namespace std const int row 9 const int col 8 const int knight 10 1 其實上限 const int state...

Hdu 3811 狀態壓縮 DP

題意 給定數n n 17 求乙個n位數,每位可選1 n中的乙個,且每位不同,並且滿足給定的條件,求方案數。給定條件為m個 x y 表示第x位為y的滿足要求。滿足m中的乙個條件即正確。演算法 dp 狀態壓縮 分析 首先不考慮條件的總方案數為n 我們先算不滿足條件的方案,因為m個條件之間是或者 關係,有...