• POJ3254 Corn Fields


    Corn Fields
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 14326   Accepted: 7509

    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

    状压DP

    不能选相邻的格子。先预处理出所有可能的状态(其实没必要),枚举状态转移,累计方案数

     1 /*by SilverN*/
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 using namespace std;
     8 const int mod=1e8;
     9 int read(){
    10     int x=0,f=1;char ch=getchar();
    11     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    12     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    13     return x*f;
    14 }
    15 int m,n;
    16 bool mp[13][13];
    17 int f[13][1<<12];
    18 int t[1<<12],cnt=0;
    19 int ban[13];
    20 void init(){
    21     int ed=(1<<n)-1;
    22     for(int i=0;i<ed;i++){
    23         int j,tmp=1;
    24         for(j=0;j<n-1;j++){
    25             if((i&tmp) && (i&(tmp<<1)))break;
    26             tmp<<=1;
    27         }
    28         if(j!=n-1)continue;
    29         t[++cnt]=i;
    30     }
    31     return;
    32 }
    33 int main(){
    34     int i,j;
    35     m=read();n=read();
    36     for(i=1;i<=m;i++){
    37         for(j=0;j<n;j++){
    38             mp[i][j]=read();
    39             if(!mp[i][j])ban[i]|=(1<<j);
    40         }
    41     }
    42     init();
    43 //    for(i=1;i<=cnt;i++)printf("%d ",t[i]);printf("
    ");
    44     f[0][1]=1;
    45     int ed=(1<<n)-1;
    46     for(i=1;i<=m;i++){
    47         for(int p=1;p<=cnt;p++){
    48             if(t[p]&ban[i-1])continue;
    49             for(int r=1;r<=cnt;r++){
    50                 if(t[r]&ban[i])continue;
    51                 if(t[p]&t[r])continue;
    52                 f[i][r]=(f[i][r]+f[i-1][p])%mod;
    53             }
    54         }
    55     }
    56     int ans=0;
    57     for(i=1;i<=cnt;i++){
    58         ans=(ans+f[m][i])%mod;
    59     }
    60     printf("%d
    ",ans);
    61     return 0;
    62 }
  • 相关阅读:
    动态规划0-1背包问题
    在网页上加入运行代码的功能
    关于CSS基础框架的学习
    Hadoop综合大作业
    hive基本操作与应用
    用mapreduce 处理气象数据集
    熟悉常用的HBase操作,编写MapReduce作业
    爬虫大作业
    熟悉常用的HDFS操作
    数据结构化与保存
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6437945.html
Copyright © 2020-2023  润新知