POJ 3254 Corn Fields 状态压缩DP (C++/Java)
id=3254">http://poj.org/problem? id=3254
题目大意:
一个农民有n行m列的地方,每一个格子用1代表能够种草地,而0不能够。放牛仅仅能在有草地的。可是相邻的草地不能同一时候放牛。 问总共同拥有多少种方法。
思路:
状态压缩的DP。
能够用二进制数字来表示放牧情况并推断该状态是否满足条件。
这题的限制条件有两个:
1.草地限制。
2.相邻限制。
对于草地限制,由于输入的时候1是能够种草地的。
以”11110“草地分析,就仅仅有最后一个是不能够种草的。
取反后得00001 。(为啥取反?不取反能够举出反例的)
如果有个状态10101 这个不相邻,可是10101 & 00001 !=0 表示有冲突。
对于相邻限制,又分为同一行的限制和上下两行的限制。
同一行限制能够一開始把相邻的情况都去掉,符合的存进数组,有助于降低状态数。 这样这个也攻克了。
上下两行相与就可以。
如(如果均可种草)
10101 & 00100!=0 也是有冲突的。
OK上代码。
C++:
#include<cstdio>
#include<cstring>
const int mod = 100000000;
const int MAXN = 1 << 12;
int map[20], status[MAXN], dp[20][MAXN];
int len; int main()
{
int n, m;
while (~scanf("%d%d", &n, &m))
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
int temp;
scanf("%d", &temp);
if (!temp)
map[i] = map[i] | (1 << (m - j - 1));
}
} len = 0;
int tot = 1 << m;
//全部状态
for (int i = 0; i < tot; i++)
{
//左移右移均可
if ((i &(i >> 1)) == 0) status[len++] = i;
}
//初始化第一行
memset(dp, 0, sizeof(dp));
for (int i = 0; i <len ; i++)
{
if ((status[i] & map[0]) == 0)
dp[0][i] = 1;
} for (int i = 1; i < n; i++)
{
for (int j = 0; j < len; j++)
{
if ((status[j] & map[i-1]) != 0) continue;
for (int k = 0; k < len; k++)
{
if ((status[k] & map[i]) != 0) continue;
if ((status[k] & status[j]) != 0) continue;
dp[i][k] = (dp[i][k] +dp[i - 1][j] )% mod;
}
}
}
int ans = 0;
for (int i = 0; i < len; i++)
ans = (ans + dp[n - 1][i]) % mod;
printf("%d\n", ans);
}
return 0;
}
JAVA:
import java.util.Scanner;
public class Main {
final static int mod = 100000000;
final static int MAXN = 1 << 12;
static int[] map = new int[20];
static int[] status = new int[MAXN];
static int[][] dp = new int[20][MAXN];
static int len;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n, m;
while (cin.hasNext()) {
n = cin.nextInt();
m = cin.nextInt();
init(n,m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
int temp = cin.nextInt();
if (temp ==0)
map[i] = (map[i] | (1 << (m - j - 1)));
}
int tot = 1 << m;
len = 0;
for (int i = 0; i < tot; i++)
if ((i & (i << 1)) == 0) {
status[len++] = i;
}
for (int i = 0; i < len; i++) {
if ((map[0] & status[i]) == 0)
dp[0][i] = 1;
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < len; j++) {
if ((map[i - 1] & status[j]) != 0)
continue;
for (int k = 0; k < len; k++) {
if ((map[i] & status[k]) != 0)
continue;
if ((status[j] & status[k]) != 0)
continue;
dp[i][k] = (dp[i][k] + dp[i - 1][j]) % mod;
}
}
}
int ans = 0;
for (int i = 0; i < len; i++) {
ans = (ans + dp[n - 1][i]) % mod;
}
System.out.println(ans);
}
}
public static void init(int n,int m)
{
int tot=1<<m;
for(int i=0;i<n;i++)
{
map[i]=0;
for(int j=0;j<tot;j++)
dp[i][j]=0;
}
}
}
POJ 3254 Corn Fields 状态压缩DP (C++/Java)的更多相关文章
- 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 (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- 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> ...
随机推荐
- [luogu]P4365[九省联考]秘密袭击coat(非官方正解)
题目背景 警告:滥用本题评测者将被封号 We could have had it all. . . . . . 我们本该,拥有一切 Counting on a tree. . . . . . 何至于此 ...
- Vue常用的GitHub项目
Vue常用的GitHub项目(Demo案例) 应用实例 https://github.com/pagekit/pa... pagekit-轻量级的CMS建站系统 https://github.com/ ...
- tortoiseGit怎么记住密码
tortoiseGit每次pull和push的时候都要输入git密码很是麻烦,下面是tortoiseGit记住密码的步骤 首先在你的项目界面右键 选择setting,这个步骤知识看一下你的名称和ema ...
- HDU——T 2119 Matrix
http://acm.hdu.edu.cn/showproblem.php?pid=2119 Time Limit: 5000/1000 MS (Java/Others) Memory Limi ...
- Ubuntu PostgreSql主从切换
主机:192.168.100.70 从机:192.168.100.71 通用配置(即主从都要配置) 修改/etc/postgresql/10/main/pg_hba.conf host all all ...
- jsoup抓取网页+具体解说
jsoup抓取网页+具体解说 Java 程序在解析 HTML 文档时,相信大家都接触过 htmlparser 这个开源项目.我以前在 IBM DW 上发表过两篇关于 htmlparser 的文章.各自 ...
- poj2965 The Pilots Brothers' refrigerator(直接计算或枚举Enum+dfs)
转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://poj.org/problem? id=2965 ---- ...
- Systemd启动图形界面过程
1 启动命令 systemctl isolate graphical.target 2 启动过程: 文件:/etc/systemd/system/graphical.target 来自:systemd ...
- Found conflicts between different versions of the same dependent assembly that could not be resolved
https://stackoverflow.com/questions/24772053/found-conflicts-between-different-versions-of-the-same- ...
- elementUI 易错点
1.element table里面添加单选时,如果存在下拉框的筛选功能,那么每次下拉框筛选条件变化时 都得清空之前选中的信息,如果不数据更新后如果更新后的数据跟之前选中的相同 则会无法选中