POJ 3254. Corn Fields 状态压缩DP (入门级)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 9806 | Accepted: 5185 |
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
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
Sample Input
2 3
1 1 1
0 1 0
Sample Output
9
Hint
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.
Source
#include <stdio.h>
#define MOD 100000000
int row[], rec[], dp[][];
int main()
{
int x=<<, k=;
for(int i=; i<x; i++) //calculate all valid states
if(!(i&(i<<))) //is a valid row state
rec[k++]=i;
rec[k]=x; int M, N, t;
scanf("%d%d", &M, &N);
for(int i=; i<M; i++)
for(int j=; j<N; j++)
scanf("%d", &t), //t = Matrix[i][j]
row[i]=(row[i]<<)|!t; //reverse row state x=<<N;
for(int i=; rec[i]<x; i++) //process first row
if(!(row[]&rec[i]))
dp[][i]=;
for(int r=; r<M; r++) //for each row
for(int i=; rec[i]<x; i++) //for each valid state for last row
if(!(row[r-]&rec[i]))
for(int j=; rec[j]<x; j++) //for each valid state for this row
if(!(row[r]&rec[j]))
if(!(rec[i]&rec[j])) //the two states are not conflict
dp[r][j]=(dp[r][j]+dp[r-][i])%MOD; int r=M-;
for(int i=; rec[i]<x; i++) //process last row
dp[r][]=(dp[r][]+dp[r][i])%MOD;
printf("%d\n", dp[r][]); return ;
}
POJ 3254. Corn Fields 状态压缩DP (入门级)的更多相关文章
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- POJ 3254 Corn Fields (状态压缩DP)
题意:在由方格组成的矩形里面种草,相邻方格不能都种草,有障碍的地方不能种草,问有多少种种草方案(不种也算一种方案). 分析:方格边长范围只有12,用状态压缩dp好解决. 预处理:每一行的障碍用一个状态 ...
- POJ 3254 Corn Fields 状态压缩DP (C++/Java)
id=3254">http://poj.org/problem? id=3254 题目大意: 一个农民有n行m列的地方,每一个格子用1代表能够种草地,而0不能够.放牛仅仅能在有草地的. ...
- poj - 3254 Corn Fields (状态压缩dp入门)
http://poj.org/problem?id=3254 参考:http://blog.csdn.net/accry/article/details/6607703 农夫想在m*n的土地上种玉米, ...
- POJ 3254 Corn Fields状态压缩DP
下面有别人的题解报告,并且不止这一个状态压缩题的哦···· http://blog.csdn.net/accry/article/details/6607703 下面是我的代码,代码很挫,绝对有很大的 ...
- [ACM] POJ 3254 Corn Fields(状态压缩)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8062 Accepted: 4295 Descr ...
- poj 3254 Corn Fields 国家压缩dp
意甲冠军: 要在m行n陆行,有一些格您可以种树,别人做不到的.不相邻的树,我问了一些不同的共同拥有的法律. 分析: 从后往前种,子问题向父问题扩展,当种到某一格时仅仅有他和他后面的n-1个格子的情况对 ...
- POJ 3254 Corn Fields 状态压缩
这题对我真的非常难.实在做不出来,就去百度了,搜到了一种状压DP的方法.这是第一种 详细见凝视 #include <cstdio> #include <cstring> #in ...
- poj 3254 Corn Fields_状态压缩dp
感谢:http://www.cnblogs.com/ka200812/archive/2011/08/11/2135607.html 让我搞懂了. #include <iostream> ...
随机推荐
- quartz.net 时间表达式----- Cron表达式详解
序言 Cron表达式:就是用简单的xxoo符号按照一定的规则,就能把各种时间维度表达的淋漓尽致,无所不在其中,然后在用来做任务调度(定时服务)的quart.net中所认知执行,可想而知这是多么的天衣无 ...
- Laravel - 安装与配置
有多重途径可以安装Laravel,下面是通过composer安装laravel的方法.Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们.c ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(47)-工作流设计-补充
系列目录 补充一下,有人要表单的代码,这个用代码生成器生成表Flow_Form表的Index代码就可以 加上几个按钮就可以了 <div class="mvctool"> ...
- Android动画效果之初识Property Animation(属性动画)
前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...
- Memcache缓存系统构建一
在如今这个高效率的社会中,怎样将这个高效率应用到自己的程序中,是一个值得追寻和值得探讨的问题.因为这个memcache能够很好的提高检索速度,提升用户体验,而且重要的是减少数据库的访问.这就大大的提高 ...
- 个人随想:对于一个.Neter来说,如果一直想走技术路线,该怎么走下去
前言 首先我不是一个合格的.Neter,也许在这里我的技术算是很菜的,不过我也是有想法的人,下面罗列出我的想法和将要实现的技术路线图. 1.学习一门底层语言 比如学习C语言,学习C语言的最终目的我觉得 ...
- SQLServer学习笔记系列1
一.前言 一直自己没有学习做笔记的习惯,所以为了加强自己对知识的深入理解,决定将学习笔记写下来,希望向各位大牛们学习交流! 不当之处请斧正!在此感谢!这边就先从学习Sqlserver写起,自己本身对数 ...
- 跨域之URL
在介绍怎么跨域之前,先来弄清楚一个概念:URL.以下内容摘自维基百科. 统一资源定位符(或称统一资源定位器/定位地址.URL地址等,英语:Uniform / Universal Resource Lo ...
- 外边距塌陷之clearance
在一个BFC中,垂直方向上相邻的块级盒子产生外边距塌陷,本文要说一个特殊的外边距塌陷情况,即当垂直方向上,两个块级盒子之间有个浮动元素相隔时,这个时候会产生什么样的效果呢? .outer{ overf ...
- [占位-未完成]scikit-learn一般实例之十:核岭回归和SVR的比较
[占位-未完成]scikit-learn一般实例之十:核岭回归和SVR的比较