结果填空:铺瓷砖
跟算法竞赛进阶指南的位运算一题几乎一样。
状态压缩+DP+DFS
DP[i][j]为前i行状态为j的方案数
code:
#include <iostream>
using namespace std;
typedef long long ll;
const int N = (1 << 10);
ll dp[12][1 << 11];
void dfs(int row, int column, int now, int pre_state)
{
if(column == 10){
dp[row + 1][now] += dp[row][pre_state];
return;
}
if(((1<<column) & pre_state)) //此位置铺了
dfs(row, column + 1, now, pre_state);
if(!((1<<column) & pre_state)) //此位置铺了竖着的
dfs(row, column + 1, now ^ (1<<column), pre_state);
if(column<=8 && !((1<<column)&pre_state) && !((1<<(column+1))&pre_state)) ////此位置和后面的位置都没有铺砖 铺一块横着的砖
{
dfs(row, column + 2, now, pre_state);
}
}
int main()
{
dp[1][0] = 1;
for(int k = 1; k <= 10; ++k)
for(int j = 0; j < N; ++j)
if(dp[k][j])
dfs(k, 0, 0, j);
cout<<dp[11][0];
}