首页 > 代码库 > HDU 4804 Campus Design(插头DP)

HDU 4804 Campus Design(插头DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4804

题意:给定一个图,0是不能放的,然后现在有1X1和1X2方块,最后铺满该图,使得1X1使用次数在C到D之间,1X2次数随便,问有几种放法

思路:插头DP的变形,只要多考虑1X1的情况即可,然后DP多开一维表示使用1X1的个数

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;

const int MOD = 1000000007;
int n, m, c, d, pre = 0, now = 1;
long long dp[2][25][1222];
char g[105][15];

int main() {
	while (~scanf("%d%d%d%d", &n, &m, &c, &d)) {
		int maxs = (1<<m);
		memset(dp[now], 0, sizeof(dp[now]));
		dp[now][0][maxs - 1] = 1;
		for (int i = 0; i < n; i++)
			scanf("%s", g[i]);
  		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				swap(pre, now);
				memset(dp[now], 0, sizeof(dp[now]));
				int tmp = g[i][j] - ‘0‘;
				if (tmp) {
					for (int k = 0; k <= d; k++) {
						for (int s = 0; s < maxs; s++) {
							if (k && (s&1<<j))
								dp[now][k][s] = (dp[now][k][s] + dp[pre][k - 1][s]) % MOD;//放1X1
							if (j && !(s&1<<(j-1)) && (s&1<<j))
								dp[now][k][s|1<<(j-1)] = (dp[now][k][s|1<<(j-1)] + dp[pre][k][s]) % MOD;//横放1X2
  							dp[now][k][s^1<<j] = (dp[now][k][s^1<<j] + dp[pre][k][s]) % MOD;//竖放1X2
						}
    				}
    			}
    			else {
    				for (int k = 0; k <= d; k++) {
    					for (int s = 0; s < maxs; s++) {
    						if ((s&1<<j))
								dp[now][k][s] = (dp[now][k][s] + dp[pre][k][s]) % MOD;//不能放的情况,和放1X1类似
         				}
        			}
       			}
   			}
  		}
  		long long ans = 0;
  		for (int i = c; i <= d; i++)
  			ans = (ans + dp[now][i][maxs - 1]) % MOD;
  		printf("%lld\n", ans);
 	}
	return 0;
}