zoukankan      html  css  js  c++  java
  • POJ3254(入门状态压缩dp)

    Corn Fields

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 13203   Accepted: 6921

    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

    第一道状态压缩dp,水一水
    题目大意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的(用1标记),农夫可以在这些格子里放牛,其他格子则不能放牛(用0标记),并且要求不可以使相邻格子都有牛。
    现在输入数据给出这块地的大小及可否放牧的情况,求该农夫有多少种放牧方案可以选择(注意:任何格子都不放也是一种选择,不要忘记考虑!
     1 //2016.8.8
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 
     6 using namespace std;
     7 
     8 const int mod = 1000000000;
     9 int arr[13][13];
    10 int dp[13][1<<13];//dp[i][j]表示第i行第j种状态时的方法数。
    11 int state[1<<13];//记录可行的状态,即两两不相邻,以减少枚举次数。
    12 int cur[13];//根据输入记录该行的状态。
    13 int len;//可行状态总数
    14 
    15 bool ok(int sta)//判断状态两两不相邻
    16 {
    17     return (sta&(sta<<1))==0?true:false;
    18 }
    19 
    20 void init(int m)//初始化可行状态
    21 {
    22     len = 0;
    23     for(int i = 0; i < (1<<m); i++)
    24       if(ok(i))state[len++] = i;
    25 }
    26 
    27 bool fit(int sta, int k)//判断状态sta是否满足第k行的状态要求
    28 {
    29     return (sta&cur[k])==0?true:false;
    30 }
    31 
    32 int main()
    33 {
    34     int n, m;
    35     while(cin>>n>>m)
    36     {
    37         init(m);
    38         for(int i = 0; i < n; i++)
    39         {
    40           int feifa = 0;
    41           for(int j = 0; j < m; j++)
    42           {
    43             scanf("%d", &arr[i][j]);
    44             if(arr[i][j]==0)feifa += (1<<(m-j-1));//0为不可以放牛,反向存为1
    45           }
    46           cur[i] = feifa;
    47         }
    48         memset(dp, 0, sizeof(dp));
    49         for(int i = 0; i < len; i++)//初始化第一行dp值
    50           if(fit(state[i], 0))
    51             dp[0][i] = 1;
    52         for(int i = 1; i < n; i++)
    53         {
    54             for(int j = 0; j < len; j++)
    55             {
    56                 if(fit(state[j], i))//如果状态state[j]满足该行i的要求,把上一行可行状态的方法数加起来
    57                 {
    58                     for(int k = 0; k < len; k++)//枚举上一行(第i-1行)的状态
    59                     {
    60                         if(!fit(state[k], i-1))continue;//排除不满足i-1行要求的状态
    61                         if(state[k]&state[j])continue;//排除不满足状态state[j]的状态
    62                         dp[i][j] = (dp[i][j]+dp[i-1][k])%mod;
    63                     }
    64                 }
    65             }
    66         }
    67         int ans = 0;
    68         for(int i = 0; i < len; i++)//答案是最后一行填各个状态的方法数之和
    69           ans = (ans+dp[n-1][i])%mod;
    70         cout<<ans<<endl;
    71     }
    72 
    73     return 0; 
    74 }
  • 相关阅读:
    [读史思考] 田单火牛阵是真实的吗?
    Alink漫谈(十二) :在线学习算法FTRL 之 整体设计
    Alink漫谈(十一) :线性回归 之 L-BFGS优化
    [笔记整理] 一维搜索
    Alink漫谈(十) :线性回归实现 之 数据预处理
    [记录点滴]编译安装luarocks、luacheck、luautf8
    [源码解析]Oozie来龙去脉之内部执行
    [记录点滴]OpenResty 支持http v2的问题
    Alink漫谈(九) :特征工程 之 特征哈希/标准化缩放
    [记录点滴]Ionic编译过程的研究
  • 原文地址:https://www.cnblogs.com/Penn000/p/5751098.html
Copyright © 2011-2022 走看看