• 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 }
  • 相关阅读:
    并发
    基础概念总结
    Tomcat总结
    JVM总结
    Spring事务管理
    数据结构和算法
    拦截器
    关于XML fragments parsed from previous mappers already contains value for错误的探索
    zookeeper比较好的学习地址
    关于idea中的maven打包
  • 原文地址:https://www.cnblogs.com/Penn000/p/5751098.html
Copyright © 2020-2023  润新知