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
  • 相关阅读:
    步步为营 .NET三层架构解析 六、BLL的设计
    js/jquery 获取url参数
    C# 生成XML
    Microsoft Ajax Minifier 使用综合
    公司面试题目之取出数据库中重复的记录
    关于下拉菜单(dropdownlist)一些用法心得
    数据库事务日志已满的解决办法
    Asp.Net[C#] 使用GridView 分页,编辑,删除,导出XML完整页面
    SQL Update
    如何安装ASPAJAXExtSetup.msi
  • 原文地址:https://www.cnblogs.com/whywhy/p/4663613.html
Copyright © 2011-2022 走看看