标签:des style blog io os 使用 ar for 数据
Description
Input
Output
Sample Input
2 1 *....... ....*... .......* .....*.. ..*..... ......*. .*...... ...*.... 2 *....... ....*... .......* .....*.. ..*..... ......*. .*...... ...*....
Sample Output
56 1409 思路:设dp[i][j][p][q]表示第i-1行状态为p,第i行状态为q,并且一共使用j个骑士的状态数,那么我们就可以利用滚动数组用第i-1行和第i行推第i+1行,详细见代码#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn = 1<<8; const int N = 10; int n, dp[2][N*2][maxn][maxn]; int g[maxn], tot[maxn]; int f1[maxn][maxn]; int f2[maxn][maxn]; void init() { for (int i = 0; i < maxn; i++) { for (int j = 0; j < 8; j++) { if (i & (1<<j)) tot[i]++; } for (int j = 0; j < maxn; j++) { if (((i >> 2) & j) || ((j >> 2) & i)) f1[i][j] = 1; if (((i >> 1) & j) || ((j >> 1) & i)) f2[i][j] = 1; } } } void solve() { int cur = 0, next = 1; memset(dp, 0, sizeof(dp)); dp[cur][0][0][0] = 1; for (int i = 0; i < 8; i++) { cur ^= 1; for (int j = 0; j <= n; j++) { for (int p = 0; p < maxn; p++) { //第i-1行 for (int q = 0; q < maxn; q++) { //第i行 if (dp[cur^1][j][p][q] == 0) continue; for (int z = 0; z < maxn; z++) { //第i+1行 if ((z & g[i+1]) != z) continue; //可以摆才行 if (tot[z] + j > n) continue; if (i >= 1 && f1[q][z]) continue; if (i >= 2 && f2[p][z]) continue; dp[cur][tot[z]+j][q][z] += dp[cur^1][j][p][q]; } } } } memset(dp[cur^1], 0, sizeof(dp[cur^1])); } int ans = 0; for (int i = 0; i < maxn; i++) for (int j = 0; j < maxn; j++) ans += dp[cur][n][i][j]; printf("%d\n", ans); } int main() { int t; char str[N]; init(); scanf("%d", &t); while (t--) { memset(g, 0, sizeof(g)); scanf("%d", &n); for (int i = 1; i <= 8; i++) { scanf("%s", str); for (int j = 0; j < 8; j++) { g[i] <<= 1; if (str[j] == '.') g[i] |= 1; } } solve(); } return 0; }
HDU - 4529 郑厂长系列故事――N骑士问题 (状态压缩DP)
标签:des style blog io os 使用 ar for 数据
原文地址:http://blog.csdn.net/u011345136/article/details/39472957