• 【POJ3254】Corn Fields


    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.

    Input

    Line 1: Two space-separated integers:M and N
    Lines 2..M+1: Line i+1 describes row i of the pasture with N space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)

    Output

    Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.

    Sample Input

    2 3

    1 1 1

    0 1 0

    Sample Output

    9

    Hint

    Number the squares as follows:

    1 2 3

      4 


    There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34), 1 way to plant on three squares (134), and one way to plant on no squares. 4+3+1+1=9.

    Source

    USACO 2006 November Gold

    好的,来个翻译啊

    农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地。John打算在牧场上的某几格里种上美味的草,供他的奶牛们享用。

    遗憾的是,有些土地相当贫瘠,不能用来种草。并且,奶牛们喜欢独占一块草地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边。

    John想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案)

    输入输出格式

    输入格式:

    第一行:两个整数M和N,用空格隔开。

    第2到第M+1行:每行包含N个用空格隔开的整数,描述了每块土地的状态。第i+1行描述了第i行的土地,所有整数均为0或1,是1的话,表示这块土地足够肥沃,0则表示这块土地不适合种草。

    输出格式:

    一个整数,即牧场分配总方案数除以100,000,000的余数。

    算法:

    状压DP

     

    分析:

    这道题一看就条件反射是动态规划。

    但是呢,假如用普通的dp写这道题可能会挂,毕竟状态这么多,每个格子取一种状态的话,我还不如用dfs。

    DP的要素有状态、阶段、决策、状态转移方程、边界情况、初始化,前几天学的树形DP呢,是从初始化(变成树状)和状态转移(记忆化搜索)的方面入手进行优化的。而今天既然状态特多而且千篇一律(都是0或1),那么我们可以通过状态压缩来优化动态规划。

     

    假如我将二进制位来保存每个状态的话,那么一行的状态就可以表示成一个十进制数,而这个十进制数就可以写成一个二进制的01串,每个位置就是相应的状态。

     

    这些都全靠状态的单一性和数量巨大,但是位数不能巨大,否则会爆。

     

    状态要初始的时候预存下来,后面再对比状态的可行性。

     

    这里巧用位运算是快速解决状压dp的关键。

     

    上代码:

     

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cctype>
     4 #define mod 100000000    
     5 #define C continue                                        //懒得打hhhhh
     6 using namespace std;
     7 
     8 int n,m,tot,state[1500],dp[15][1500],ans,cur[15];        //dp表示当前最大值,第一维是行数,第二维是状态数,cur是每行的情况,state是预存的可能状态
     9 
    10 inline int read()                                //读入优化
    11 {
    12     int x=0,f=1;
    13     char c=getchar();
    14     while (!isdigit(c))
    15         f=c=='-'?-1:1,c=getchar();
    16     while (isdigit(c))
    17         x=(x<<1)+(x<<3)+(c^48),c=getchar();
    18     return x*f;
    19 }
    20 
    21 inline bool fit(int x,int k)                            //判断当前状态是否符合当前行
    22 {
    23     return !(state[x]&cur[k]);
    24 }
    25 
    26 inline void init()                                //初始化
    27 {
    28     int sum=1<<n,i;            //列举可能状态,并预存
    29     for (i=0;i<sum;i++)
    30         if (!(i&(i<<1)))
    31             state[++tot]=i;
    32 }
    33 
    34 int main()
    35 {
    36     int i,j,k;
    37     m=read();
    38     n=read();
    39     init();
    40     for (i=1;i<=m;i++)
    41         for (j=1;j<=n;j++)
    42         {
    43             k=read();
    44             if (!k)
    45                 cur[i]+=(1<<(n-j));                    //此处注意,cur是不合法才是1
    46         }
    47     for (i=1;i<=tot;i++)                                //初始化第一行
    48         if (fit(i,1))
    49             dp[1][i]=1;
    50     for (i=2;i<=m;i++)                                //枚举行
    51         for (j=1;j<=tot;j++)                            //枚举当前状态
    52         {
    53             if (!fit(j,i))
    54                 C;
    55             for (k=1;k<=tot;k++)                //枚举上一层可行状态
    56             {
    57                 if (!fit(k,i-1))
    58                     C;
    59                 if (state[j]&state[k])
    60                     C;
    61                 dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;        //状态转移
    62             }
    63         }
    64     for (i=1;i<=tot;i++)
    65         ans=(ans+dp[m][i])%mod;
    66     printf("%d",ans);
    67     return 0;
    68 }

     

    其实只要普通的状态转移方程列出来了,优化就好做了。

     

    嗯,就这样了。

  • 相关阅读:
    [NOI2004]郁闷的出纳员
    【数论】[SDOI2010]古代猪文
    【2-SAT】[JSOI2010]满汉全席
    【数位DP】CF55D Beautiful numbers
    c语言编译的四个阶段
    转评:Python集合
    转评:Python字典
    Python iterators and generators:迭代器和生成器
    Python函数-具备特定功能的可以有参数和返回值的可重用的代码块儿
    Python-简版List和Tuple
  • 原文地址:https://www.cnblogs.com/Ronald-MOK1426/p/8451875.html
Copyright © 2020-2023  润新知