2015年2月10日 星期二

HDUOJ 1693 Eat the Trees

卡輸出格式卡一陣子==


#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

long long dp[12][12][1<<13];
int n, m;
int tab[12][12];

int main(){
    int c = 1;
    int T; scanf("%d", &T);
    while(T--){
        memset(tab, 0, sizeof(tab));
        scanf("%d %d", &n, &m);
        for(int lx = 0;lx < n;lx++)
            for(int ly = 0;ly < m;ly++)
                scanf("%d", &tab[lx][ly]);
        memset(dp, 0, sizeof(dp));
        dp[0][0][0] = 1;
        for(int lx = 0;lx <= n;lx++){
            if(lx){
                for(int sts = 0;sts < 1<<(m);sts++)
                    dp[lx][0][sts] = dp[lx-1][m][sts];
            }
            for(int ly = 0;ly < m;ly++){
                //printf("(%d, %d):\n", lx, ly);
                for(int sts = 0;sts < 1<<(m+1);sts++){
                    //printf("\t sts %d = %lld\n", sts, dp[lx][ly][sts]);
                    int lf = sts&(1<<m);
                    int up = sts&(1<<ly);
                    long long val = dp[lx][ly][sts];
                    if(tab[lx][ly] == 0){
                        if(lf == 0 and up == 0)
                            dp[lx][ly+1][sts] += val;
                        continue;
                    }
                    if(lf&&up) dp[lx][ly+1][sts^lf^up] += val;
                    else if(lf||up) dp[lx][ly+1][sts] += val, 
                        dp[lx][ly+1][sts^(1<<m)^(1<<ly)] += val;
                    else dp[lx][ly+1][sts^(1<<m)^(1<<ly)] += val;
                }
            }
        }
        printf("Case %d: There are %lld ways to eat the trees.\n", c, dp[n][m][0]);
        c++;
    }
    return 0;
}

沒有留言:

張貼留言