zoukankan      html  css  js  c++  java
  • POJ

    线上题目:

    Corn Fields
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 6936   Accepted: 3697

    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.
     
      题意:在一个N*M的矩阵里面有的格子是正常的,有的是有坏掉的,现在需要在好的格子上面放牛,放牛的原则是在相邻的格子上不能同时放牛,求不同的方案数目(不同的牛的数目的放牛方案数以及不放牛也是一种方案)。
      第一次做状态压缩的题目,其实现在也还是暂时不能很好地解决这一类题目。
      说一下做法我们需要分析一下,对于某一列来说,如果全部的格子都是完好的话,那我们可以放最多的牛的方法数只有两种,因为需要隔开放。同时如果我们需要用二进制表示某一行的所有状态的话状态的数目不会很多,用一个int32即可,我们这里只需要用一个数组res[]记录前i行到达当前行的某一种状态的最大值,从上一行某一个状态转移到当前状态的条件是两行的放牛方案没有冲突。
     
    上代码:
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #define MAX 2048
     5 #define LL long long
     6 #define MOD 100000000
     7 using namespace std;
     8 
     9 LL dp[15][MAX];
    10 LL in[MAX],state[MAX];
    11 int m,n,cnt;
    12 
    13 void init(){
    14     cnt=0;
    15     for(int i=0;i<(1<<n);i++) if( (i&(i<<1)) == 0 ) state[cnt++]=i;
    16 }
    17 
    18 int main()
    19 {
    20     //freopen("data.txt","r",stdin);
    21     while(scanf("%d %d",&m,&n)!=EOF){
    22         init();
    23         for(int i=0;i<m;i++){
    24             int o;
    25             in[i]=0;
    26             for(int j=0;j<n;j++){
    27                 scanf("%d",&o);
    28                 in[i]=in[i]|(o<<j);
    29             }
    30         }
    31         memset(dp,0,sizeof(dp));
    32         for(int i=0;i<cnt;i++){
    33             if( (in[0]&state[i]) == state[i] ) dp[0][i]=1;
    34         }
    35         for(int i=1;i<m;i++){
    36             for(int j=0;j<cnt;j++){
    37                 if( (in[i]&state[j]) == state[j] )
    38                     for(int k=0;k<cnt;k++){
    39                         if( (state[j]&state[k]) == 0) dp[i][j]=(dp[i][j]+dp[i-1][k])%MOD;
    40                     }
    41             }
    42         }
    43         LL sum=0;
    44         for(int i=0;i<cnt;i++) sum=(sum+dp[m-1][i])%MOD;
    45         cout<<sum<<endl;
    46     }
    47     return 0;
    48 }
    3254
  • 相关阅读:
    Java Properties 类读配置文件保持顺序
    mysql在增加列前进行判断该列是否存在
    java中用jdom创建xml文档/将数据写入XML中
    JavaFX 简介
    ActiveMQ详细入门使用教程
    Jquery Pagination分页插件使用
    Jquery选择器总结
    自己4月份面试的一些总结
    Java面试题全集(下)转载
    Java面试题全集(上)转载
  • 原文地址:https://www.cnblogs.com/sineatos/p/3845465.html
Copyright © 2011-2022 走看看