zoukankan      html  css  js  c++  java
  • POJ 3254 状态压缩 DP

    B - Corn Fields

    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.

    题意:

    有一片地,有的地方可以种草,有的地方不能,并且相邻的两块不能同时种草,问总共有多少种种草的方法。

    思路:

    由于一行的状态可由前一行的状态推出,dp[i][j]+=dp[i-1][k],k,j表示此行的状态。第一道状压DP,虽然是照着别人的博客写出来的但确实学到了很多。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int MOD = 1e8;
    int n, m, f[13][1 << 13], mp[13], g[13][1 << 13];
    int main()
    {
        while (~scanf("%d %d", &n, &m)) {
            for (int i = 0; i < n; ++i) {
                for (int j = m - 1, x; j >= 0; --j) {
                    scanf("%d", &x);
                    if (x) mp[i] |= (1 << j);
                }
            }
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < (1 << m); ++j) {
                    g[i][j] = 0;
                    if (j & (j <<1)) continue;
                    if (~mp[i] & j) continue;
            //原来第i行的状态取反与j状态相与为1说明j状态在没草的地方放牛,所以不合法即~mat[i]&j
            //判断j状态是否有相邻的两个1,若有即不合法,用j&(j<<1)判断,j<<1即j右移1位
            //eg:01101--->11010,相与为1,即可判断出有相邻的1,不合法。
                    g[i][j] = 1;
                }
            }
            for (int j = 0; j < (1 << m); ++j) f[0][j] = g[0][j];
            for (int i = 1; i < n; ++i) {
                for (int j = 0; j < (1 << m); ++j) {
                    f[i][j] = 0;
                    if (!g[i][j]) continue;
                    for (int k = 0; k < (1 << m); ++k) {
                        if (!g[i - 1][k]) continue;
                        if (j & k) continue; //i+1行的状态与i行的状态不冲突
                        f[i][j] = (f[i][j] + f[i - 1][k]) % MOD;
                    }
                }
            }
            int ans = 0;
            for (int j = 0; j < (1 << m); ++j) {
                ans = (ans + f[n - 1][j]) % MOD;
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    用弦截法求解方程的根
    Fibonacci_array
    爱你所爱,行你所行
    Visual Studio安装Visual Assist的办法(兼容VS2010至VS2017)
    对集合类型属性的实体类的查询集的封装
    简单购物选择案例--纯js代码
    静态json数据表格渲染展示
    js之全选,反选,全不选案例
    常见IO流简介
    JDBC一般流程
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/5734493.html
Copyright © 2011-2022 走看看