zoukankan      html  css  js  c++  java
  • (简单) POJ 3254 Corn Fields,状压DP。

    Description

    Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant.

    Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant.

      题目就是求方案数,一看数据范围,差不多就是状态压缩啥的,然后DP就好。

      dp[i][j] 表示前i列,第i列的分布状态为j时的方案数。

    代码如下:

    // ━━━━━━神兽出没━━━━━━
    //      ┏┓       ┏┓
    //     ┏┛┻━━━━━━━┛┻┓
    //     ┃           ┃
    //     ┃     ━     ┃
    //     ████━████   ┃
    //     ┃           ┃
    //     ┃    ┻      ┃
    //     ┃           ┃
    //     ┗━┓       ┏━┛
    //       ┃       ┃
    //       ┃       ┃
    //       ┃       ┗━━━┓
    //       ┃           ┣┓
    //       ┃           ┏┛
    //       ┗┓┓┏━━━━━┳┓┏┛
    //        ┃┫┫     ┃┫┫
    //        ┗┻┛     ┗┻┛
    //
    // ━━━━━━感觉萌萌哒━━━━━━
    
    // Author        : WhyWhy
    // Created Time  : 2015年07月21日 星期二 09时43分34秒
    // File Name     : 3254.cpp
    
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    const int mod=100000000;
    
    int N,M;
    int map1[15][15];
    int rem[15];
    long long dp[15][5000];
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
    
        long long ans;
    
        while(~scanf("%d %d",&N,&M))
        {
            for(int i=1;i<=N;++i)
                for(int j=1;j<=M;++j)
                    scanf("%d",&map1[i][j]);
    
            for(int i=1;i<=M;++i)
            {
                rem[i]=0;
    
                for(int j=1;j<=N;++j)
                    if(!map1[j][i])
                        rem[i]|=(1<<(j-1));
            }
    
            memset(dp,0,sizeof(dp));
    
            for(int j=0;j<(1<<N);++j)
                if(!(j&rem[1] || j&(j<<1)))
                    ++dp[1][j];
    
            for(int i=2;i<=M;++i)
                for(int j=0;j<(1<<N);++j)
                {
                    if(j&(j<<1)  || j&rem[i])
                        continue;
                    
                    for(int k=0;k<(1<<N);++k)
                    {
                        if(j&k)
                            continue;
    
                        (dp[i][j]+=dp[i-1][k])%=mod;
                    }
                }
    
            ans=0;
    
            for(int j=0;j<(1<<N);++j)
                (ans+=dp[M][j])%=mod;
    
            printf("%lld
    ",ans);
        }
        
        return 0;
    }
    View Code
  • 相关阅读:
    SpringBoot学习:整合shiro(身份认证和权限认证),使用EhCache缓存
    帝国备份王出错
    spring boot整合mybatis+mybatis-plus
    Druid连接池简介和配置
    thinkphp生成的验证码不显示问题解决
    分布式文件系统-FastDFS
    Spring Security OAuth2 Demo
    spring cloud-给Eureka Server加上安全的用户认证
    spring cloud 报错Error creating bean with name 'hystrixCommandAspect' ,解决方案
    分布式唯一ID极简教程
  • 原文地址:https://www.cnblogs.com/whywhy/p/4663613.html
Copyright © 2011-2022 走看看