您的位置:首页 > 其它

poj 2663 Tri Tiling 【简单数位DP】

2013-04-08 11:58 399 查看
http://poj.org/problem?id=2663

dp[i][j]表示前i-1行全部都填满的时候j对应的状态(如下)

/*
0 -- 000
1 -- 001
2 -- 010
3 -- 011
4 -- 100
5 -- 101
6 -- 110
7 -- 111
*/

然后因为情况数很少,所以就一一枚举出来了

状态转移方程:

dp[i][1] = dp[i-1][6];
dp[i][2] = dp[i-1][5];
dp[i][4] = dp[i-1][3];
dp[i][3] = dp[i-1][7] + dp[i-1][4];
dp[i][5] = dp[i-1][2];
dp[i][6] = dp[i-1][7] + dp[i-1][1];
dp[i][7] = dp[i][1] + dp[i][4] + dp[i-2][7];

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
template <class T> void checkmin(T &t,T x) {if(x < t) t = x;}
template <class T> void checkmax(T &t,T x) {if(x > t) t = x;}
template <class T> void _checkmin(T &t,T x) {if(t==-1) t = x; if(x < t) t = x;}
template <class T> void _checkmax(T &t,T x) {if(t==-1) t = x; if(x > t) t = x;}
typedef pair <int,int> PII;
typedef pair <double,double> PDD;
typedef long long ll;
#define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end ; it ++)
int dp[33][8];
/*
0 -- 000
1 -- 001
2 -- 010
3 -- 011
4 -- 100
5 -- 101
6 -- 110
7 -- 111
*/
void init() {
dp[0][7] = 1;
dp[1][3] = dp[1][6] = 1;
//dp[2][1] = dp[2][4] = 1;
//dp[2][7] = 3;
for(int i=2;i<=30;i++) {
dp[i][1] = dp[i-1][6];
dp[i][2] = dp[i-1][5];
dp[i][4] = dp[i-1][3];
dp[i][3] = dp[i-1][7] + dp[i-1][4];
dp[i][5] = dp[i-1][2];
dp[i][6] = dp[i-1][7] + dp[i-1][1];
dp[i][7] = dp[i][1] + dp[i][4] + dp[i-2][7];
}
}
int main() {
init();  int n;
while(~scanf("%d",&n) && n != -1) {
printf("%d\n" , dp
[7]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: