• Corn Fields poj3254(状态压缩DP)


    Corn Fields
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 6081   Accepted: 3226

    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.
     
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <math.h>
     5 #include <algorithm>
     6 using namespace std;
     7 #define mod 100000000
     8 int a[20],dp[20][1<<15];
     9 bool check(int x,int y)
    10 {
    11     int z=x|y;
    12     if(z>y)return 0;
    13     else return 1;
    14 }
    15 bool check2(int x)
    16 {
    17     int z=x&1;
    18     x>>=1;
    19     while(x)
    20     {
    21         if(z&(x&1))return 0;
    22         z=x&1;
    23         x>>=1;
    24     }
    25     return 1;
    26 }
    27 bool check1(int x,int y)
    28 {
    29     if(x&y)return 0;
    30     else return 1;
    31 }
    32 int main()
    33 {
    34     int m,n,i,j,x,k;
    35     cin>>m>>n;
    36     for(i=0; i<m; i++)
    37     {
    38         x=0,a[i]=0;
    39         for(j=0; j<n; j++)
    40         {
    41             scanf("%d",&x);
    42             a[i]=(a[i]<<1)+x;
    43         }
    44     }
    45     memset(dp,0,sizeof(dp));
    46     for(j=0; j<(1<<n); j++)
    47         if(check(j,a[0])&&check2(j))
    48     dp[0][j]=1;
    49     for(i=1; i<m; i++)
    50     {
    51         for(j=0; j<(1<<n); j++)
    52         {
    53             if(check(j,a[i])&&check2(j))
    54             {
    55                 for(k=0; k<(1<<n); k++)
    56                 {
    57                     if(check1(k,j))
    58                         dp[i][j]+=dp[i-1][k],dp[i][j]%=mod;
    59                 }
    60             }
    61         }
    62     }
    63     int sum=0;
    64     for(i=0;i<(1<<n);i++)sum+=dp[m-1][i],sum%=mod;
    65     cout<<sum<<endl;
    66 }
    View Code
  • 相关阅读:
    ArcPad 10 的安装部署
    各种机械键盘轴的差别,究竟什么轴好
    default argument given of parameter 的问题
    Quartz中时间表达式的设置-----corn表达式
    图像切割之(一)概述
    SMTP协议分析
    Android学习小Demo(19)利用Loader来实时接收短信
    qml动画控制器AnimationController
    httpclient 文件上传
    Java习题10.24
  • 原文地址:https://www.cnblogs.com/ERKE/p/3585157.html
Copyright © 2020-2023  润新知