博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2663 Tri Tiling (状压dp+多米诺骨牌问题+滚动数组反思)
阅读量:6950 次
发布时间:2019-06-27

本文共 1780 字,大约阅读时间需要 5 分钟。

本来直接一波状压dpAC的

#include
#include
#include
#define REP(i, a, b) for(int i = (a); i < (b); i++)#define _for(i, a, b) for(int i = (a); i <= (b); i++)using namespace std;typedef long long ll;ll dp[50][10];int path[15][2], p, n, m = 3;void dfs(int l, int now, int pre){ if(l > m) return; if(l == m) { path[p][0] = pre; path[p++][1] = now; return; } dfs(l + 1, (now << 1) | 1, pre << 1); dfs(l + 1, now << 1, (pre << 1) | 1); dfs(l + 2, (now << 2) | 3, (pre << 2) | 3);}int main(){ dfs(0, 0, 0); while(~scanf("%d", &n) && n != -1) { memset(dp, 0, sizeof(dp)); dp[0][(1 << m) - 1] = 1; _for(i, 1, n) REP(j, 0, p) dp[i][path[j][1]] += dp[i-1][path[j][0]]; printf("%lld\n", dp[n][(1 << m) - 1]); } return 0;}

然后闲着无聊想用滚动数组优化一下,虽然说对于这道题完全没必要

然后就发现了问题

每次使用的时候要清空这一行的值

因为这道题的状态转移是+=, 以前都是=,所以值可以直接覆盖。

还有一定要i++之后t再改,我一开始是写j++后t就改,然后就连样例都过不了,调了好久才发现

#include
#include
#include
#define REP(i, a, b) for(int i = (a); i < (b); i++)#define _for(i, a, b) for(int i = (a); i <= (b); i++)using namespace std;typedef long long ll;ll dp[2][10];int path[15][2], p, n, m = 3;void dfs(int l, int now, int pre){ if(l > m) return; if(l == m) { path[p][0] = pre; path[p++][1] = now; return; } dfs(l + 1, (now << 1) | 1, pre << 1); dfs(l + 1, now << 1, (pre << 1) | 1); dfs(l + 2, (now << 2) | 3, (pre << 2) | 3);}int main(){ dfs(0, 0, 0); while(~scanf("%d", &n) && n != -1) { memset(dp, 0, sizeof(dp)); int t = 0; dp[t][(1 << m) - 1] = 1; t ^= 1; _for(i, 1, n) { REP(j, 0, p) dp[t][path[j][1]] = 0; //这一行是关键 REP(j, 0, p) dp[t][path[j][1]] += dp[t^1][path[j][0]]; t ^= 1; } printf("%lld\n", dp[t^1][(1 << m) - 1]); } return 0;}

 

转载于:https://www.cnblogs.com/sugewud/p/9819322.html

你可能感兴趣的文章
【小练习】“表单”制作及答案
查看>>
Android应用程序支持安装到SD卡
查看>>
我的软件工程课目标
查看>>
MYSQL 连接数据库命令收藏
查看>>
C#基础篇六飞行棋
查看>>
汇编语言(王爽)第一章基础知识
查看>>
在创业型软件公司的收获
查看>>
Build SSH for Development on Windows Subsystem for Linux
查看>>
学习:数学----容斥原理
查看>>
WebSite And WebApplication
查看>>
Georgia Tech Online Master of Science in Computer Science 项目经验分享
查看>>
字王珐琅体系列,初稿ok
查看>>
浏览网上资源,了解编译原理就是什么?学习编译原理有什么好处?不学有什么损失?如何学习编译原理?...
查看>>
LeetCode 226. Invert Binary Tree
查看>>
空虚、寂寞、无聊
查看>>
基础学习笔记之opencv(1):opencv中facedetect例子浅析
查看>>
JS中属性/方法调用
查看>>
iOS 7 需要再和 Android 比什么
查看>>
8-Images
查看>>
Python字节码与解释器学习
查看>>