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
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 第一道状态压缩dp,水一水
题目大意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的(用1标记),农夫可以在这些格子里放牛,其他格子则不能放牛(用0标记),并且要求不可以使相邻格子都有牛。
现在输入数据给出这块地的大小及可否放牧的情况,求该农夫有多少种放牧方案可以选择(注意:任何格子都不放也是一种选择,不要忘记考虑!
//2016.8.8
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int mod = ;
int arr[][];
int dp[][<<];//dp[i][j]表示第i行第j种状态时的方法数。
int state[<<];//记录可行的状态,即两两不相邻,以减少枚举次数。
int cur[];//根据输入记录该行的状态。
int len;//可行状态总数 bool ok(int sta)//判断状态两两不相邻
{
return (sta&(sta<<))==?true:false;
} void init(int m)//初始化可行状态
{
len = ;
for(int i = ; i < (<<m); i++)
if(ok(i))state[len++] = i;
} bool fit(int sta, int k)//判断状态sta是否满足第k行的状态要求
{
return (sta&cur[k])==?true:false;
} int main()
{
int n, m;
while(cin>>n>>m)
{
init(m);
for(int i = ; i < n; i++)
{
int feifa = ;
for(int j = ; j < m; j++)
{
scanf("%d", &arr[i][j]);
if(arr[i][j]==)feifa += (<<(m-j-));//0为不可以放牛,反向存为1
}
cur[i] = feifa;
}
memset(dp, , sizeof(dp));
for(int i = ; i < len; i++)//初始化第一行dp值
if(fit(state[i], ))
dp[][i] = ;
for(int i = ; i < n; i++)
{
for(int j = ; j < len; j++)
{
if(fit(state[j], i))//如果状态state[j]满足该行i的要求,把上一行可行状态的方法数加起来
{
for(int k = ; k < len; k++)//枚举上一行(第i-1行)的状态
{
if(!fit(state[k], i-))continue;//排除不满足i-1行要求的状态
if(state[k]&state[j])continue;//排除不满足状态state[j]的状态
dp[i][j] = (dp[i][j]+dp[i-][k])%mod;
}
}
}
}
int ans = ;
for(int i = ; i < len; i++)//答案是最后一行填各个状态的方法数之和
ans = (ans+dp[n-][i])%mod;
cout<<ans<<endl;
} return ;
}
POJ3254(入门状态压缩dp)的更多相关文章
- poj3254(状态压缩DP)
poj3254 题意 给出一个01矩阵,1表示当前这个位置可以放牛,要求放牛的方案保证牛不能左右或上下相邻,求方案数. 分析 dp[S][i]: 表示到 i 行时的状态S(用二进制数表示),那么状态转 ...
- POJ3254 - Corn Fields(状态压缩DP)
题目大意 给定一个N*M大小的土地,土地有肥沃和贫瘠之分(每个单位土地用0,1来表示贫瘠和肥沃),要求你在肥沃的单位土地上种玉米,如果在某个单位土地上种了玉米,那么与它相邻的四个单位土地是不允许种玉米 ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- POJ1185 - 炮兵阵地(状态压缩DP)
题目大意 中文的..直接搬过来... 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平 ...
- 『最短Hamilton路径 状态压缩DP』
状压DP入门 最短Hamilton路径 Description 给定一张 n(n≤20) 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamilton路径. Hamil ...
- poj 3311 floyd+dfs或状态压缩dp 两种方法
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6436 Accepted: 3470 ...
- 状态压缩dp poj 3254 hdu5045
近来感觉状态压缩dp的强大性(灵活利用了二进制运算非常关键). . . 于是做了俩提来看看..毕竟队友是专业的dp.我仅仅是管中窥豹下而已.. 日后有机会再与之玩耍玩耍...ps:假设上天再给我一次机 ...
- 状态压缩DP(大佬写的很好,转来看)
奉上大佬博客 https://blog.csdn.net/accry/article/details/6607703 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的 ...
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
随机推荐
- Moocryption
Moocryption 题目描述 Unbeknownst to many, cows are quite fond of puzzles, particularly word puzzles. Far ...
- PAT (Advanced Level) 1080. Graduate Admission (30)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- MYSQL同步--主从同步问题集锦
1 同步停止,报错误: Could not find first log file name in binary log index file 数据库主从出错: Slave_IO_Running: ...
- ArcEngine部分工作总结
Arcengine工作总结地物点查询本部分可以在一个窗体中实现,也可以在两个窗体中实现.由于工作要求本人是在两个窗体中实现的:弹出窗体的名称为FormQuery主窗体单机查询时间的代码FormQuer ...
- tinyxml2库的使用--MFC工程
在编写应用程序的时候,经常需要动态加载某些数据,这种情况下微软的ini文件是蛮好的选择,但是平台的通用性比较差,使用xml的话就比较强一点,但是解析比较复杂,型号有牛人已经开发出了直接读写xml的库, ...
- ZOJ 3927 Programming Ability Test
水题,判断一下加起来是否大于等于80 #include<cstdio> #include<cstring> #include<cmath> #include< ...
- IOS_FMDB有关字典、数组存储及获取问题
http://blog.csdn.net/betterbb/article/details/25984455 FMDB存储字典或数组时会变成字符串存入sqlite里,但如果不将其转换成json格式存储 ...
- 用OpenSSL生成自签名证书在IIS上搭建Https站点(用于iOS的https访问)
前提: 先安装openssl,安装有两种方式,第一种直接下载安装包,装上就可运行:第二种可以自己下载源码,自己编译.这里推荐第一种. 安装包:http://slproweb.com/products/ ...
- java中基本类型占用字节数
之前一直使用c/c++开发c中各种类型占用的位数和java还是有区别的,特地找了篇文章过来对比下. 在处理网络协议的时候需要注意 在Java中一共有8种基本数据类型,其中有4种整型,2种浮点类型,1种 ...
- Mysql导入zabbix的sql语句时报错:ERROR 1045 (28000)
#Warning: Using a password on the command line interface can be insecure.#ERROR 1045 (28000): Access ...