POJ 3254 & POJ 1185(状压DP入门)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16773 | Accepted: 8860 |
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 <cstdio>
#include <cstring>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ;;
const int M = 1e5;
const int mod = ;
const int mo=;
//const double pi= acos(-1.0);
//typedef pair<int,int>pii;
int n,m,cas;
int sta[N],a[M],dp[N][M];
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
for(int j=,x;j<m;j++){
scanf("%d",&x);
if(!x)sta[i]+=(<<j);
}
}
int cnt=;
for(int i=;i<(<<m);i++){
if(i&(i<<))continue;
a[cnt++]=i;
if(i&sta[])continue;
dp[][cnt-]++;
}
for(int i=;i<n;i++){
for(int j=;j<cnt;j++){
if(a[j]&sta[i])continue;
for(int k=;k<cnt;k++){
if(a[j]&a[k]||a[k]&sta[i-])continue;
dp[i][j]+=dp[i-][k];
}
}
}
int ans=;
for(int i=;i<cnt;i++){
ans+=dp[n-][i];
ans%=;
}
printf("%d\n",ans);
return ;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 29369 | Accepted: 11377 |
Description

如果在地图中的灰色所标识的平原上部署一支炮兵部队,则图中的黑色的网格表示它能够攻击到的区域:沿横向左右各两格,沿纵向上下各两格。图上其它白色网格均攻击不到。从图上可见炮兵的攻击范围不受地形的影响。
现在,将军们规划如何部署炮兵部队,在防止误伤的前提下(保证任何两支炮兵部队之间不能互相攻击,即任何一支炮兵部队都不在其他支炮兵部队的攻击范围内),在整个地图区域内最多能够摆放多少我军的炮兵部队。
Input
接下来的N行,每一行含有连续的M个字符('P'或者'H'),中间没有空格。按顺序表示地图中每一行的数据。N <= 100;M <= 10。
Output
Sample Input
5 4
PHPP
PPHH
PPPP
PHPP
PHHP
Sample Output
6
Source
#include <cstdio>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#include <set>
#include <queue>
#include <string>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,m,cas;
char str[];
int sta[],a[M],dp[][M][M];
int sum[M];
int getsum(int x){
int ret=;
for(int i=;i<m;i++){
if(x&(<<i))ret++;
}
return ret;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%s",str);
for(int j=;j<m;j++){
if(str[j]=='H')sta[i]+=(<<j);
}
}
int cnt=;
for(int i=;i<(<<m);i++){
if(i&(i<<)||i&(i<<))continue;
a[cnt++]=i;
if(i&sta[])continue;
dp[][cnt-][]=getsum(i);
}
for(int i=;i<cnt;i++){
sum[i]=getsum(a[i]);
if(a[i]&sta[])continue;
int res=;
for(int j=;j<cnt;j++){
if(a[i]&a[j])continue;
dp[][i][j]= dp[][j][] + getsum(a[i]);
}
}
for(int i=;i<n;i++){
for(int j=;j<cnt;j++){
if(a[j]&sta[i])continue;
for(int k=;k<cnt;k++){
if(a[j]&a[k]||a[k]&sta[i-])continue;
for(int l=;l<cnt;l++){
if(a[j]&a[l]||a[l]&sta[i-]||a[k]&a[l])continue;
dp[i][j][k]=max(dp[i-][k][l]+sum[j],dp[i][j][k]);
}
}
} }
int ans=;
for(int i=;i<cnt;i++){
for(int j=;j<cnt;j++){
ans=max(ans,dp[n-][i][j]);
}
}
printf("%d\n",ans);
return ;
}
POJ 3254 & POJ 1185(状压DP入门)的更多相关文章
- poj 3254 Corn Fields 状压dp入门
题目链接 题意 在\(M\times N\)的\(0,1\)格子上放东西,只有标记为\(1\)的格子可以放东西,且相邻的格子不能同时放东西.问有多少种放法. 思路 参考:swallowblank. \ ...
- POJ 3254 Corn Fields (状压dp)
题目链接:http://poj.org/problem?id=3254 给你n*m的菜地,其中1是可以种菜的,而菜与菜之间不能相邻.问有多少种情况. 状压dp入门题,将可以种菜的状态用一个数的二进制表 ...
- POJ 3254 - Corn Fields - [状压DP水题]
题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...
- [ An Ac a Day ^_^ ] POJ 3254 Corn Fields 状压dp
题意: 有一块n*m的土地 0代表不肥沃不可以放牛 1代表肥沃可以放牛 且相邻的草地不能同时放牛 问最多有多少种放牛的方法并对1e8取模 思路: 典型的状压dp 能状态压缩 能状态转移 能状态压缩的题 ...
- Poj - 3254 Corn Fields (状压DP)(入门)
题目链接:https://vjudge.net/contest/224636#problem/G 转载于:https://blog.csdn.net/harrypoirot/article/detai ...
- poj 3254 状压dp入门题
1.poj 3254 Corn Fields 状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...
- POJ:1185-炮兵阵地(状压dp入门)
炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组 ...
- POJ 1684 Corn Fields(状压dp)
描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ ...
- POJ 2923 Relocation(状压DP)题解
题意:有2辆车运货,每次同时出发,n(<10),各自装货容量c1 c2,问最少运几次运完. 思路:n比较小,打表打出所有能运的组合方式,用背包求出是否能一次运走.然后状压DP运的顺序. 代码: ...
- poj3254状压DP入门
G - 状压dp Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:65536KB 64bit ...
随机推荐
- ArraySizeHelper解析
[ArraySizeHelper解析] 以下代码用于获取一个数组的元素个数,例如 int table[100],以下宏返回100. template <typename T, size_t N& ...
- ASP.NET中的另类控件
首先看一个aspx文件里的部分内容: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- C#(.net)水印图片的生成
/* * * 使用说明: * 建议先定义一个WaterImage实例 * 然后利用实例的属性,去匹配需要进行操作的参数 * 然后定义一个WaterImageManage实例 * 利用WaterI ...
- NameValuePair方式传参数
今天工作中联调外部的一个接口用post方式传输,我按照文档封装参数成Jason字符串传入,但是对方一直接受参数为空,折腾了半天也没找到问题.很苦恼,检查代码都没有错误,可是为什么对方接受参数为空呢?然 ...
- HDU 1686 Oulipo (KMP 可重叠)
题目链接 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La dispariti ...
- python概念-Socket到底有多骚
Socket究竟是什么呢? 简单来说Socket就是用来完成客户端与服务器之间的通信 例如浏览器访问网页,例如网络游戏等一切基于客户端服务器来实现的C/S架构程序 Socket是基于互联网OSI七层协 ...
- Macaca(一) - 环境配置
Macaca是阿里提供的一套自动化测试框架,目前已开源. 花了两三个小时研究了一下Macaca的实现原理.因为很好奇它与appium.selenium有啥区别. 实现原理本质上与selenium的we ...
- 用Nginx分流绕开Github反爬机制
用Nginx分流绕开Github反爬机制 0x00 前言 如果哪天有hacker进入到了公司内网为所欲为,你一定激动地以为这是一次蓄谋已久的APT,事实上,还有可能只是某位粗线条的员工把VPN信息泄露 ...
- 关于注入抽象类报could not autowire field的问题
昨天工作中遇到了一个很奇葩的问题,之前一直都没考虑过抽象类这块,一直用的注入接口实现类: 先看下错误: 因为在类中注入了一个抽象类,之前只有一个继承子类,所以没问题,这里要说一下抽象类的实例化: 抽象 ...
- Pool thread stack traces: Thread[C3P0PooledConnectionPoolManager[identityToken->原因解决办法
今天本地连接测试库测试,发现早上还是好的,下午就崩了,报这个错,使用的是c3po连接池: , 纠结了好久,发现是数据库连接用光了,很多人都在连,果断换了本地库,好使了,看百度说把macPoolSizz ...