poj 2663 Tri Tiling 【简单数位DP】

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[n][7]);
    }
    return 0;
}

 

posted @ 2013-04-08 11:58  aiiYuu  阅读(298)  评论(0编辑  收藏  举报