【动态规划】[HDU1693] Eat the Trees

dp[i][j][k] i , j 代表的是当前枚举到的坐标, 然后k代表当前的结束后分割的线的方案,这个分割线的上方是已经dp完了的内容这条分割线用01表示代表该位置是否有东西穿过, 递推就行了

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int MAXN = 12;
int MAXF, n, m;
int Map[MAXN+10][MAXN+10];
LL dp[MAXN+10][MAXN+10][(1<<MAXN)+10];
int main(){
    int K;
    scanf("%d", &K);
    for(int kase=0;kase<K;kase++){
    scanf("%d %d", &n, &m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%d", &Map[i][j]);
    int MAXF = 1 << (m+1);
    memset(dp, 0, sizeof dp);
    dp[0][m][0] = 1;
    for(int i=1;i<=n;i++){
        for(int k=0;k<(MAXF>>1);k++)
            dp[i][0][k<<1] = dp[i-1][m][k];
        for(int j=1;j<=m;j++){
            for(int k=0;k<MAXF;k++){
                bool left = k & (1 << (j-1)), up = k & (1 << j);
                if(Map[i][j]){
                    dp[i][j][k] = dp[i][j-1][k^(1<<j>>1)^(1<<j)];
                    if(left != up) dp[i][j][k] += dp[i][j-1][k];
                }else{
                    if(!left && !up) dp[i][j][k] = dp[i][j-1][k];
                    else dp[i][j][k] = 0;
                }
            }
        }
    }
    cout<<"Case "<<kase+1<<": There are "<<dp[n][m][0]<<" ways to eat the trees."<<endl;
    }

    return 0;
}

posted on 2015-04-11 16:03  JeremyGuo  阅读(136)  评论(0编辑  收藏  举报

导航