poj 3254 状压dp

E - Corn Fields
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 3254

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.
转自:http://blog.csdn.net/lenleaves/article/details/7974849
第一道状态压缩DP,虽然不难,但是。。。
递推方程 dp[s][i] =Σ dp[s'][i-1]   dp[s][i]表示第i行状态为s时一共有多少种方案,从第一行开始向下递推。
这里要注意 可以用一个数组先把每一行中符合要求的情况全部记录,这样可以有效减少状态,和循环。另外题目要求我们取模,这里要注意。

 

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 
 10 #define N 15
 11 #define M 15
 12 #define mod 100000000
 13 #define mod2 100000000
 14 #define ll long long
 15 #define maxi(a,b) (a)>(b)? (a) : (b)
 16 #define mini(a,b) (a)<(b)? (a) : (b)
 17 
 18 using namespace std;
 19 
 20 int n,m;
 21 int a[N];
 22 int dp[N][ (1<<12)+10];
 23 int ans;
 24 
 25 void ini()
 26 {
 27     int i,j;
 28     int re;
 29     ans=0;
 30     memset(dp,0,sizeof(dp));
 31     memset(a,0,sizeof(a));
 32     for(i=1;i<=n;i++){
 33         for(j=1;j<=m;j++){
 34             scanf("%d",&re);
 35             a[i]=(a[i]<<1) + re; //把二进制压缩成一个十进制数字
 36         }
 37     }
 38 
 39     //for(i=1;i<=n;i++){
 40     //    printf(" %d\n",a[i]);
 41     //}
 42 }
 43 
 44 int ok(int i,int j)
 45 {
 46     if( (a[i] & j) !=j) return 0;  //判断下那一行种是否能够找出flag这个状态
 47     while(j)
 48     {
 49         if(j%2==1 && (j/2)%2==1 ) return 0;
 50         j/=2;
 51     }
 52     return 1;
 53 }
 54 
 55 void solve()
 56 {
 57     int i,j;
 58     i=1;
 59     for(j=0;j<=a[i];j++){
 60         if(ok(i,j)==0) continue;
 61         dp[i][j]++;
 62     }
 63     for(i=2;i<=n;i++){
 64         for(j=0;j<=a[i];j++){
 65             if(ok(i,j)==0) continue;
 66             for(int k=0;k<=a[i-1];k++){
 67                 if(ok(i-1,k)==0) continue;
 68                 if( (j&k)!=0) continue;
 69                 dp[i][j]+=dp[i-1][k];
 70                 dp[i][j]%=mod;
 71             }
 72         }
 73     }
 74 
 75     //for(i=1;i<=n;i++){
 76     //    for(j=0;j<=a[i];j++){
 77     //        printf(" %d %d dp=%d\n",i,j,dp[i][j]);
 78     //    }
 79    // }
 80     for(j=0;j<=a[n];j++){
 81         ans+=dp[n][j];
 82         ans%=mod;
 83         //printf(" %d %d %d\n",j,dp[n][j],ans);
 84     }
 85 }
 86 
 87 int main()
 88 {
 89     //freopen("data.in","r",stdin);
 90    // scanf("%d",&T);
 91    // for(int cnt=1;cnt<=T;cnt++)
 92     //while(T--)
 93     while(scanf("%d%d",&n,&m)!=EOF)
 94     {
 95         ini();
 96         solve();
 97         printf("%d\n",ans);
 98     }
 99 
100     return 0;
101 }
View Code

 

转载于:https://www.cnblogs.com/njczy2010/p/3930118.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546204.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第一弹!安利10个让你爽到爆的IDEA必备插件!

大家好&#xff0c;此篇文章中我会介绍10个非常不错的IDEA插件以及它们常见功能的使用方法。这一期内容搞 Gif 动态图花了很久&#xff0c;很多Gif图片上传到微信还提示过大&#xff0c;所以很多地方重新又录制了一遍Gif图。概览&#xff1a;IDE Features Trainer—IDEA交互式教…

C程序对整数中设置为1的位数进行计数

Problem statement: Write a C program to count number of bits set to 1 in an Integer. 问题陈述&#xff1a;编写一个C程序来计算Integer中设置为1的位数 。 Solution: We can use bitwise operator here to solve the problem. 解决方案&#xff1a;我们可以在这里使用按…

String性能提升10倍的几个方法!(源码+原理分析)

这是我的第 54 篇原创文章。String 类型是我们使用最频繁的数据类型&#xff0c;没有之一。那么提高 String 的运行效率&#xff0c;无疑是提升程序性能的最佳手段。我们本文将从 String 的源码入手&#xff0c;一步步带你实现字符串优化的小目标。不但教你如何有效的使用字符串…

日期getTime()方法以及JavaScript中的示例

JavaScript Date getTime()方法 (JavaScript Date getTime() method) getTime() method is a Dates class method and it is used to get the time in milliseconds from 1st January 1970. getTime()方法是Date的类方法&#xff0c;用于获取从1970年1月1 日开始的时间(以毫秒为…

制作openstack-centos镜像

一、准备工作我在计算节点上面制作镜像&#xff0c;计算节点为centos6.3 64位系统1.安装底层支持包yum groupinstall Virtualization "Virtualization Client"yum install libvirt2.下载或从本地上传进去一个完整的系统镜像mkdir /openstack-p_w_picpathcd /openstac…

一文彻底搞懂Java中的值传递和引用传递!

关于Java中方法间的参数传递到底是怎样的、为什么很多人说Java只有值传递等问题&#xff0c;一直困惑着很多人&#xff0c;甚至我在面试的时候问过很多有丰富经验的开发者&#xff0c;他们也很难解释的很清楚。我很久也写过一篇文章&#xff0c;我当时认为我把这件事说清楚了&a…

g++默认参数_C ++默认参数| 查找输出程序| 套装1

g默认参数Program 1: 程序1&#xff1a; #include <iostream>using namespace std;int sum(int X, int Y 20, int Z 30){return (X Y Z);}int main(){int A 0, B 0;A sum(5, 10);B sum(10);cout << A << " " << B;return 0;}Output…

c语言指针灵活性管窥

最近看到mit的[urlhttp://pdos.csail.mit.edu/6.828/2010/]操作系统课程网站[/url],其[urlhttp://pdos.csail.mit.edu/6.828/2010/labs/lab1/]实验一[/url] 中练习四&#xff08;exercise 4&#xff09;中有一个关于指针使用的代码&#xff1a;#include <stdio.h>#includ…

近100个Spring/SpringBoot常用注解汇总!

作者 | Guide来源 | JavaGuide&#xff08;微信公众号&#xff09;毫不夸张地说&#xff0c;这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景。对于每一个注解我都说了具体用法&#xff0c;掌握搞懂&#xff0c;使用 SpringBoot 来开发项…

虚拟化之vmware-vsphere (web) client

两种客户端 vsphere client 配置》软件》高级设置里的变量 uservars.supressshellwarning1 vsphere web client 安装完vSphere Web Client后&#xff0c;在浏览器地址栏输入https://localhost:<9443 或者你选择的其他端口>/admin-app/就可以访问vSphere Web Client管理工…

用贪婪算法解决背包问题_解决主要算法问题的贪婪策略

用贪婪算法解决背包问题Introduction: 介绍&#xff1a; Lets start the discussion with an example that will help to understand the greedy technique. If we think about playing chess, when we make a move we think about the consequences of the move in future st…

HashMap 的 7 种遍历方式与性能分析!(强烈推荐)

这是我的第 56 篇原创文章随着 JDK 1.8 Streams API 的发布&#xff0c;使得 HashMap 拥有了更多的遍历的方式&#xff0c;但应该选择那种遍历方式&#xff1f;反而成了一个问题。本文先从 HashMap 的遍历方法讲起&#xff0c;然后再从性能、原理以及安全性等方面&#xff0c;来…

BBcode 相关资源索引

VeryCD社区BBCode使用指南 BBcode Reference BBcodewikipedia

Why is HttpContext.Current null after await?

今天在对项目代码进行异步化改进的时候&#xff0c;遇到一个奇怪的问题&#xff08;莫笑&#xff0c;以前没遇过&#xff09;&#xff0c;正如标题一样&#xff0c;HttpContext.Current 在 await 异步执行之后&#xff0c;就会变为 null。 演示代码&#xff1a; public async T…

c ++产生不同的随机数_C ++程序生成随机密码

c 产生不同的随机数Problem Statement: 问题陈述&#xff1a; Write a menu driven program to generate password randomly 编写菜单驱动程序以随机​​生成密码 constraint: 约束&#xff1a; password should consist of 密码应包含 lowercase Alphabet - a to zUpperC…

如何选择c语言学习书籍

C语言作为一个简洁精巧的语言&#xff0c;在计算机业中仍有非常广泛的应用。而在最近的编程语言流行度排名 中&#xff0c;C语言仍然位居第二的宝座。 通常在学习一门编程语言之前我们都会有一定的缘由&#xff1a;可能是为了应付某项专业考试&#xff0c;也可能是提高自己的专…

WEB平台架构之:LAMP(Linux+Apache+MySQL+PHP)

WEB平台架构之&#xff1a;LAMP(LinuxApacheMySQLPHP) 从业界来看&#xff0c;最主流的web平台架构就当属LAMP了。LAMP架构可以说是一切web平台的基础架构&#xff0c;所有一切的所谓大型架构无非就是通过一些负载均衡技术&#xff0c;集群技术&#xff0c;缓存技术等结合LAMP…

numpy zeros矩阵_零矩阵使用numpy.zeros()| 使用Python的线性代数

numpy zeros矩阵Zeros Matrix - When all the entries of a matrix are one, then it is called a zeros matrix. It may be of any dimension (MxN). 零矩阵 -当矩阵的所有条目均为1时&#xff0c;则称为零矩阵。 它可以是任何尺寸( MxN )。 Properties: 特性&#xff1a; T…

图解TCP三次握手和四次挥手!(简单易懂)

哈喽&#xff1a;亲爱的小伙伴&#xff0c;首先祝大家五一快乐~本来打算节日 happy 一下就不发文了&#xff0c;但想到有些小伙伴可能因为疫情的原因没出去玩&#xff0c;或者劳逸结合偶尔刷刷公众号&#xff0c;所以今天就诈尸更新一篇干货&#xff0c;给大家解解闷~前言不管面…

《c程序设计语言》练习1-12

c程序设计语言练习1-12&#xff1a;编写一个程序&#xff0c;以每行一个单词的形式打印其输入。 此处单词是指除空格&#xff0c;TAB键&#xff0c;换行字符和文件结束符号&#xff08;EOF&#xff09;之外的其他字符。 我的代码如下&#xff1a; 而《the c answer book》中的代…