• Corn Fields【状态压缩dp】


    题目: 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.

    题意: 农民有一个矩阵草原养牛,为1的方块才能养,为0的不能养,而且每头牛不能相邻,现在给你一个草原,问你农名最多有多少种养牛方案.

    input

    2 3
    1 1 1
    0 1 0
    

    output

    9

    分析

    ​ 每块地只有是牧场和不是牧场两种状态,每个牧场有放牛和不放牛两种状态,可以用0 1表示,以此状态压缩

    有两个限制条件

    1. 只能在牧场放牛
    2. 相邻牧场不能同时放牛

    现在我们来看怎样用位运算处理这两种情况

    1.假如 1 表示是牧场,0表示不是

    第i行: 0 0 1 0 1 0 1

    放牛情况: 1 0 0 1 0 0 1

    按位与 1 0 0 0 0 0 0 ---> ≠ 0

    结论a> 若在不是牧场的地方放了牛,&的结果不为0

    第i行 1 0 1 1 0 0 1

    放牛情况 1 0 1 1 0 0 1

    放牛<<1 0 1 1 0 0 1 0

    按位与 0 0 1 0 0 0 0 ≠  0

    结论b> 若在相邻的牧场放了牛,放牛情况<<1后与牧场&的值不为0

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    #include<cmath>
    using namespace std;
    const int mod = 1e8;
    int n, m;
    int mn[15], dp[15][1<<13], vis[15][1<<13];
    //mn[i]存储第 i 行最大的牧场状态,dp[i][k]表示第i行的状态为k时,前i行的最大方案数。,vis[i][j]表示第 i 行的 j 状态是否合法 
    int main(){
        scanf("%d%d", &n, &m);
        for(int i=0; i<n; i++){
            int ans = 0;
            for(int j=0; j<m; j++){
                int a; scanf("%d", &a);
                ans = (ans<<1) + a;//ans左移一位,空出的一位记录当前是否有牧场
            }
            mn[i] = ans;
            for(int j=0; j<=mn[i]; j++){//枚举第i行的状态j
                if((j & mn[i]) != j) continue;//在没有牧场的地方放了牛
                if(j & (j<<1)) continue;//在相邻的牧场放了牛
                vis[i][j] = 1;//第i行的j状态合法
            }
        }
        for(int i=0; i<n; i++){//枚举行
            for(int j=0; j<=mn[i]; j++){//枚举状态
                if(!vis[i][j]) continue;//不合法则跳过
                if(i == 0) dp[i][j] = 1;//第一行任何合法的方案都可以
                else{
                    for(int k=0; k<=mn[i-1]; k++){//枚举上一行的状态
                        if(!vis[i-1][k]) continue;//不合法则跳过
                        if((j & k) != 0) continue;//在竖直方向上的相邻位置放了牛
                        dp[i][j] = (dp[i][j]%mod + dp[i-1][k]%mod)%mod;//当前总共的方案数
                    }
                }
            }
        }
        int ans = 0;
        for(int i=0; i<=mn[n-1]; i++){//枚举第n行上一行的状态
            if(!vis[n-1][i]) continue;//不合法则跳过
            ans = (ans%mod +dp[n-1][i]%mod)%mod;//求最终的方案和
        }
        printf("%d
    ", ans);
        return 0;
    }
    
  • 相关阅读:
    js动态添加CSS
    LINUX命令行回滚SVN版本
    LINUX文件名批量修改
    LINUX下文件编码转换 iconv
    ubuntu下eclipse的svn插件使用javahl
    HTML5学习笔记 本地数据库
    正则表达式
    我的第一篇博客
    【分布式锁的演化】分布式锁居然还能用MySQL?
    【分布式锁的演化】终章!手撸ZK分布式锁!
  • 原文地址:https://www.cnblogs.com/hzoi-poozhai/p/12694182.html
Copyright © 2020-2023  润新知