POJ 1038 Bugs Integrated, Inc. 状态压缩DP
来源:互联网 发布:java性能分析工具 编辑:程序博客网 时间:2024/06/03 19:10
题目来源:1038 Bugs Integrated, Inc.
题意:最多能放多少个2*3的矩形
思路:状态压缩DP啊 初学 看着大牛的代码搞下来的 总算搞懂了 接下来会更轻松吧
3进制代表前2行的状态(i行和i-1行)1代表i-1行占位 2代表i行占位 i-1不管有没有占位都不会影响的0代表i行和i-1行都空闲
然后枚举状态dfs更新状态 话说就不能没写深搜了 有点不会了
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int dp[2][60000];int a[155][12];int b[115][12];int n, m;int get(int k){int ans = 0;for(int i = m-1; i >= 0; i--){ans *= 3;ans += b[k][i];}return ans;}int get2(int x, int k){for(int i = 0; i < m; i++){b[k][i] = x%3;x /= 3;}}void dfs(int l, int i, int x){dp[l][get(1)] = max(dp[l][get(1)], x);//printf("***%d", dp[l][get(1)]);if(i >= m)return;if(i < m-2 && !b[1][i] && !b[1][i+1] && !b[1][i+2]){//puts("sfa");b[1][i] = b[1][i+1] = b[1][i+2] = 2;dfs(l, i+3, x+1);b[1][i] = b[1][i+1] = b[1][i+2] = 0;}if(i < m-1 && !b[1][i] && !b[1][i+1] && !b[0][i] && !b[0][i+1]){//puts("dfs");b[1][i] = b[1][i+1] = 2;dfs(l, i+2, x+1);b[1][i] = b[1][i+1] = 0;}dfs(l, i+1, x);}int main(){int T;scanf("%d", &T);while(T--){memset(a, 0, sizeof(a));int k;scanf("%d %d %d", &n, &m, &k);int s = 1;for(int i = 0; i < m; i++)s *= 3;for(int i = 0; i < k; i++){int x, y;scanf("%d %d", &x, &y);a[--x][--y] = 1;}memset(dp, -1, sizeof(dp));for(int i = 0; i < m; i++){b[0][i] = a[0][i] + 1;}dp[1][get(0)] = 0;int l = 1, r = 0;for(int i = 1; i < n; i++){l ^= 1;r ^= 1; memset(dp[l], -1, sizeof(dp[l]));for(int j = 0; j < s; j++){if(dp[r][j] == -1)continue;get2(j, 0);for(int t = 0; t < m; t++){if(a[i][t])b[1][t] = 2;elseb[1][t] = b[0][t]-1;if(b[1][t] < 0)b[1][t] = 0;}dfs(l, 0, dp[r][j]);}}int ans = 0;for(int i = 0; i < s; i++)ans = max(ans, dp[l][i]);printf("%d\n", ans);}return 0;}
0 0
- poj 1038 - Bugs Integrated, Inc.(状态压缩dp)
- 状态压缩dp POJ 1038 Bugs Integrated, Inc.
- POJ 1038 Bugs Integrated, Inc. 状态压缩DP
- CEOI 2002, POJ 1038 Bugs Integrated, Inc. 状态压缩 DP
- poj 1038 Bugs Integrated, Inc. 状态压缩dp
- 1038 Bugs Integrated, Inc.//状态压缩DP
- POJ--1038--Bugs Integrated, Inc.--状态DP
- 【DP,状态压缩】PKU-1038-Bugs Integrated, Inc.
- poj 1038 Bugs Integrated, Inc.(动态规划状态压缩)
- poj 1038 Bugs Integrated, Inc. __dp状态压缩
- poj 1038 Bugs Integrated, Inc. __dp状态压缩
- POJ 1038 Bugs Integrated, Inc. 三进制压缩DP
- POJ 1038 Bugs Integrated, Inc. 状态压缩dp+3进制枚举
- poj 1038 Bugs Integrated, Inc. 状态DP+四进制+DFS
- POJ 1038 - Bugs Integrated, Inc. 三进制状态DP
- poj 1038 Bugs Integrated, Inc. 状压dp
- [状态压缩 dp] [CEOI 2002] Bugs Integrated, Inc.
- poj 1038 Bugs Integrated, Inc.
- VBScripts and UAC elevation(visa以后的系统)
- HttpURLConnection跨域、跨应用访问
- LeetCode: Partition List [086]
- GDI+学习笔记----前言
- C++教程网 第26章
- POJ 1038 Bugs Integrated, Inc. 状态压缩DP
- 最小生成树算法汇总 (普里姆 && 克鲁斯卡尔与并查集结合)
- 矩阵链乘法2(15章:动态规划)。。。2014.5.30
- Zero Copy I: User-Mode Perspective (1)
- PCA(Principal Component Analysis)之个人理解
- poj2196
- 思维框架
- 打印1到最大的n位数(循环方法)
- 经典旅游01背包