Corn Fields
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

Line 1: Two space-separated integers: M and N 
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

Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.

Sample Input

2 3
1 1 1
0 1 0

Sample Output

9

Hint

Number the squares as follows:

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

【题意】一块n*m的区域,有些地方草比较肥沃,有些地方贫瘠,现在要在肥沃的地方放羊,相邻的土地不能同时放羊,问有多少种放法,n,m<=12。
【分析】看到数据范围容易想到状压,然后枚举当前行和前一行状态 即可,dp[i][j]表示第i行使用j状态的总方法 数。

#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

司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队。一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用"P"表示),如下图。在每一格平原地形上最多可以布置一支炮兵部队(山地上不能够部署炮兵部队);一支炮兵部队在地图上的攻击范围如图中黑色区域所示: 

如果在地图中的灰色所标识的平原上部署一支炮兵部队,则图中的黑色的网格表示它能够攻击到的区域:沿横向左右各两格,沿纵向上下各两格。图上其它白色网格均攻击不到。从图上可见炮兵的攻击范围不受地形的影响。 
现在,将军们规划如何部署炮兵部队,在防止误伤的前提下(保证任何两支炮兵部队之间不能互相攻击,即任何一支炮兵部队都不在其他支炮兵部队的攻击范围内),在整个地图区域内最多能够摆放多少我军的炮兵部队。 

Input

第一行包含两个由空格分割开的正整数,分别表示N和M; 
接下来的N行,每一行含有连续的M个字符('P'或者'H'),中间没有空格。按顺序表示地图中每一行的数据。N <= 100;M <= 10。

Output

仅一行,包含一个整数K,表示最多能摆放的炮兵部队的数量。

Sample Input

5 4
PHPP
PPHH
PPPP
PHPP
PHHP

Sample Output

6

Source

【题意】在一个格子中放置炮弹将影响上下左右两格,两枚炮弹不能炸到彼此,问 最多可以放置多少炮弹。
【分析】跟上一题很像,需要枚举三行状态,d[i][j][k]表示第i行j状态,第i-1行k状态获得的最大值。

#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入门)的更多相关文章

  1. poj 3254 Corn Fields 状压dp入门

    题目链接 题意 在\(M\times N\)的\(0,1\)格子上放东西,只有标记为\(1\)的格子可以放东西,且相邻的格子不能同时放东西.问有多少种放法. 思路 参考:swallowblank. \ ...

  2. POJ 3254 Corn Fields (状压dp)

    题目链接:http://poj.org/problem?id=3254 给你n*m的菜地,其中1是可以种菜的,而菜与菜之间不能相邻.问有多少种情况. 状压dp入门题,将可以种菜的状态用一个数的二进制表 ...

  3. POJ 3254 - Corn Fields - [状压DP水题]

    题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...

  4. [ An Ac a Day ^_^ ] POJ 3254 Corn Fields 状压dp

    题意: 有一块n*m的土地 0代表不肥沃不可以放牛 1代表肥沃可以放牛 且相邻的草地不能同时放牛 问最多有多少种放牛的方法并对1e8取模 思路: 典型的状压dp 能状态压缩 能状态转移 能状态压缩的题 ...

  5. Poj - 3254 Corn Fields (状压DP)(入门)

    题目链接:https://vjudge.net/contest/224636#problem/G 转载于:https://blog.csdn.net/harrypoirot/article/detai ...

  6. poj 3254 状压dp入门题

    1.poj 3254  Corn Fields    状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...

  7. POJ:1185-炮兵阵地(状压dp入门)

    炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组 ...

  8. POJ 1684 Corn Fields(状压dp)

    描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ ...

  9. POJ 2923 Relocation(状压DP)题解

    题意:有2辆车运货,每次同时出发,n(<10),各自装货容量c1 c2,问最少运几次运完. 思路:n比较小,打表打出所有能运的组合方式,用背包求出是否能一次运走.然后状压DP运的顺序. 代码: ...

  10. poj3254状压DP入门

    G - 状压dp Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536KB     64bit ...

随机推荐

  1. 边框画的三角形给shadow

    本文地址:http://www.cnblogs.com/veinyin/p/8690882.html  要写一个对话气泡样式,我们首先想到的当然给是一个盒子,然后用边框画一个三角形定位过去. 如果不需 ...

  2. 守卫者的挑战(guard)

    problem Pro 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地.突然,眼前一道亮光闪过.“我,Nizem,是黑魔法圣殿的守卫者.如果你能通过 ...

  3. http、https 等 常用默认端口号

    ⑴. HTTP协议代理服务器常用端口号:80/8080/3128/8081/9080⑵. SOCKS代理协议服务器常用端口号:1080⑶. FTP(文件传输)协议代理服务器常用端口号:21⑷. Tel ...

  4. java后台中处理图片辅助类汇总(上传图片到服务器,从服务器下载图片保存到本地,缩放图片,copy图片,往图片添加水印图片或者文字,生成二维码,删除图片等)

    最近工作中处理小程序宝箱活动,需要java画海报,所以把这块都快百度遍了,记录一下处理的方法,百度博客上面也有不少坑! 获取本地图片路径: String bgPath = Thread.current ...

  5. 【codeforces】【比赛题解】#862 CF Round #435 (Div.2)

    这次比赛打得很舒服,莫名得了个Rank41,涨了219的Rating,就比较优秀.不过还是没有闫神厉害啊.题目链接::P. [A]MEX 题意: Evil博士把Mahmoud和Ehab绑架到了邪恶之地 ...

  6. 彻底搞懂字符编码(unicode,mbcs,utf-8,utf-16,utf-32,big endian,little endian...)[转]

    最近有一些朋友常问我一些乱码的问题,和他们交流过程中,发现这个编码的相关知识还真是杂乱不堪,不少人对一些知识理解似乎也有些偏差,网上百度, google的内容,也有不少以讹传讹,根本就是错误的(例如说 ...

  7. 4 - django-orm基本使用

    目录 1 数据库与ORM 2 orm的配置 2.1 引擎和配置 2.2 mysql驱动程序 3 orm 表模型 3.1 创建表对象 3.2 Django字段类型 3.3 常用字段参数说明 3.4 特殊 ...

  8. aarch64_g5

    gtkmm24-devel-2.24.5-2.fc26.aarch64.rpm 2017-02-11 18:17 620K fedora Mirroring Project gtkmm24-docs- ...

  9. getattr的使用

    from requests_html import HTMLSession class UrlGenerator(object): def __init__(self, root_url): self ...

  10. FastDFS集群部署

    之前介绍过关于FastDFS单机部署,详见博文:FastDFS+Nginx(单点部署)事例 下面来玩下FastDFS集群部署,实现高可用(HA) 服务器规划: 跟踪服务器1[主机](Tracker S ...