POJ 3254 Corn Fields [DP]
题意:略。
思路:第一次做状态压缩的dp。
在这里说一下状态压缩的原则。因为每一行只有最多12个格子,每个格子只有1(可放牛)和0(不可放牛)两种状态,这总共是2^12种状态,直接用一个int整型变量从0枚举到2^12 - 1。对于每一个数,将其转换成二进制,先判断格子为0时该数二进制对应位上是不是1,若是则该状态不可行。其次再判断该数相邻的两位有没有同时为1的情况,如果有,该状态也不可行。
递推公式就是:dp[i][k] = dp[j][k-1]。
其中dp[i][k]表示在牧场第k行状态为i时可行解的数量。它等于上一层所有可行状态的可行解数量之和。
在dp过程中,在判断该层的上一层某状态是否可行时,需要判断两层状态有没有上下相邻的位同为1的情况,如果有,则上一层的状态不可行,跳过。
此外在计算可行解数量和时别忘了取模。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define pri 100000000
using namespace std;
int m, n;
int farm[][];
int dp[<<][];
bool judge(int i,int pos)//判断状态i在牧场第pos行是否合法
{
int now = n;
int last = ;
while (now)
{
int num = i % ;
if (!farm[pos][now] && num || (last && num)) return ;
last = num;
i /= ;
now--;
}
return ;
}
bool judge_state(int i,int j)//判断状态i和j能否放在牧场上下相邻的两行
{
int now = n;
while (now)
{
if ((i % ) && (j % )) return ;
i /= ;
j /= ;
now--;
}
return ;
}
int getdp()
{
memset(dp, , sizeof(dp));
for (int i = ; i < (<<n); i++)//初始化第一行
if (judge(i, )) dp[i][] = ;
for (int i = ; i <= m; i++)//枚举牧场剩下的每一行
for (int j = ; j < (<<n); j++) if (judge(j, i))//枚举该行可行的状态
for (int k = ; k < (<<n); k++) if (dp[k][i-])//枚举上一行可行的状态
if (judge_state(j, k))
{
dp[j][i] += dp[k][i-];
dp[j][i] %= pri;
}
int res = ;
for (int i = ; i < (<<n); i++)
{
res += dp[i][m];
res %= pri;
}
return res; }
int main()
{
scanf("%d%d", &m, &n);
for (int i = ; i <= m; i++)
for (int j = ; j <= n; j++)
scanf("%d", &farm[i][j]);
printf("%d", getdp());
return ;
}
POJ 3254 Corn Fields [DP]的更多相关文章
- 状压DP POJ 3254 Corn Fields
题目传送门 /* 状态压缩DP:先处理硬性条件即不能种植的,然后处理左右不相邻的, 接着就是相邻两行查询所有可行的种数并累加 写错一个地方差错N久:) 详细解释:http://www.tuicool. ...
- poj - 3254 - Corn Fields (状态压缩)
poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...
- poj 3254 Corn Fields
http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 3254 Corn Fields(状压DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13732 Accepted: 7216 Desc ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- poj 3254 Corn Fields 国家压缩dp
意甲冠军: 要在m行n陆行,有一些格您可以种树,别人做不到的.不相邻的树,我问了一些不同的共同拥有的法律. 分析: 从后往前种,子问题向父问题扩展,当种到某一格时仅仅有他和他后面的n-1个格子的情况对 ...
- [ACM] POJ 3254 Corn Fields(状态压缩)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8062 Accepted: 4295 Descr ...
- POJ 3254 - Corn Fields - [状压DP水题]
题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...
随机推荐
- day10 消息队列,多进程和多线程以及协程,异步IO,事件驱动等
回顾一下线程和进程 线程与进程的区别 守护线程: 队列: 两种方式: 先进先出 # 后入先出 #卖水果,后来的来的是新的 生产者消费者模型: 生产包子, 吃包子 事件 event: 红绿灯模型 ...
- loj2035 「SDOI2016」征途
学了斜率优化这题就能一气呵成地做出来啦qwqqwq #include <iostream> #include <cstdio> using namespace std; typ ...
- leetcode 【 Best Time to Buy and Sell Stock 】python 实现
思路: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- leetcode 【 Set Matrix Zeroes 】python 实现
题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cl ...
- Monkey官方帮助翻译&介绍
都说想学好就看原文,英文不好为了翻译这个可费了大劲了.表格从GOOGLE官网复制到WORD里编辑,结果贴上来格式全乱了,只得不弄表格了.表格中官网的事件不是最新的,比最新的少2个,具体我会另发一篇文章 ...
- jquery左右滑动菜单
<div class="mini-container" style="position:relative;height:100%;"> <di ...
- 理解机器为什么可以学习(一)---Feasibility of learning
主要讲解内容来自机器学习基石课程.主要就是基于Hoeffding不等式来从理论上描述使用训练误差Ein代替期望误差Eout的合理性. PAC : probably approximately corr ...
- mvcs项目搭建
项目结构 下载链接:http://pan.baidu.com/s/1hsGtShA
- 【转】lightmap
Shader "Diffuse Lightmap" { Properties { _MainTex ("Texture 1", 2D) = "whit ...
- response contentType
response.setContentType(MIME)的作用是使客户端浏览器,区分不同种类的数据,并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据. 例如web浏览器就是通过MI ...