炮兵阵地

Language:Default
炮兵阵地
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 34008 Accepted: 13083

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

分析

由于会影响两行,所以需要在行数以外增加两维状态。

预处理出单行的合法状态,大概在60左右,然后枚举转移即可。

时间复杂度\(o(n 60^3)\),由于地形的因数,状态量又会减少,上界很松。

#include<iostream>
#include<cstring>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using std::max; int dp[101][77][77];
int sg[101];
int n,m,idx,s[77],cnt0[77];
int get_one(int x){
int cnt=0;
while(x) x&=x-1,++cnt;
return cnt;
}
bool ok(int x){
return x&x<<1||x&x<<2?0:1;
}
void init(){
for(int i=0;i<1<<m;++i)if(ok(i)){
s[idx]=i,cnt0[idx++]=get_one(i);
}
}
bool valid(int i,int x){
return sg[i]&x?0:1;
}
int solve(){
int ans=0;
memset(dp,-1,sizeof dp);
dp[0][0][0]=0;
for(int i=0;i<idx;++i)if(valid(1,s[i])){
dp[1][i][0]=cnt0[i];
ans=max(ans,dp[1][i][0]);
}
for(int i=2;i<=n;++i)
for(int j=0;j<idx;++j)if(valid(i,s[j]))
for(int k=0;k<idx;++k)if(valid(i-1,s[k])&&(s[j]&s[k])==0){
int last=0;
for(int l=0;l<idx;++l)if(dp[i-1][k][l]!=-1&&(s[l]&s[j])==0)
last=max(last,dp[i-1][k][l]);
dp[i][j][k]=max(dp[i][j][k],last+cnt0[j]);
if(i==n) ans=max(ans,dp[i][j][k]);
}
return ans;
}
int main(){
// freopen(".in","r",stdin),freopen(".out","w",stdout);
read(n),read(m);
for(int i=1;i<=n;++i){
static char s[11];
scanf("%s",s);
for(int j=0;j<m;++j)
if(s[j]=='H') sg[i]|=1<<j;
}
init();
printf("%d\n",solve());
return 0;
}

Mondriaan's Dream

Language:Default
Mondriaan's Dream
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 21909 Accepted: 12282

Description

Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series' (where he had to use his toilet paper to draw on, for all of his paper was filled with squares and rectangles), he dreamt of filling a large rectangle with small rectangles of width 2 and height 1 in varying ways.



Expert as he was in this material, he saw at a glance that he'll need a computer to calculate the number of ways to fill the large rectangle whose dimensions were integer values, as well. Help him, so that his dream won't turn into a nightmare!

Input

The input contains several test cases. Each test case is made up of two integer numbers: the height h and the width w of the large rectangle. Input is terminated by h=w=0. Otherwise, 1<=h,w<=11.

Output

For each test case, output the number of different ways the given rectangle can be filled with small rectangles of size 2 times 1. Assume the given large rectangle is oriented, i.e. count symmetrical tilings multiple times.

Sample Input

1 2
1 3
1 4
2 2
2 3
2 4
2 11
4 11
0 0

Sample Output

1
0
1
2
3
5
144
51205

Source

分析

网上一些题解都是什么鬼,轮廓线、插头DP都冒出来了。

用\(F[i,j]\)表示前\(i\)行,第\(i\)行的放置状态为\(j\)的方案数,把特殊的\(1\times 2\)矩形的上半部分的状态定为1。

这样\(j\)能转移到\(k\),当且仅当

  1. \(j\&k=0\),显然的。
  2. \(j|k\)的每一段连续的0的数量必须有偶数个,这体现了\(2\times 1\)矩形的要求。

预处理出那些状态转移合法,大力转移即可。

时间复杂度\(O(n 2^{2m})\)

#include<iostream>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll; int n,m;
ll f[12][1<<11];
bool in_s[1<<11];
int main(){
// freopen(".in","r",stdin),freopen(".out","w",stdout);
while(read(n)|read(m)){
for(int i=0;i<1<<m;++i){
bool cnt=0,has_odd=0;
for(int j=0;j<m;++j){
if(i>>j&1) has_odd|=cnt,cnt=0;
else cnt^=1;
}
in_s[i]=has_odd|cnt?0:1;
}
f[0][0]=1;
for(int i=1;i<=n;++i)
for(int j=0;j<1<<m;++j){
f[i][j]=0;
for(int k=0;k<1<<m;++k)
if((j&k)==0&&in_s[j|k])
f[i][j]+=f[i-1][k];
}
printf("%lld\n",f[n][0]);
}
return 0;
}

POJ1185 炮兵阵地 和 POJ2411 Mondriaan's Dream的更多相关文章

  1. [poj1185]炮兵阵地_状压dp

    炮兵阵地 poj-1185 题目大意:给出n列m行,在其中添加炮兵,问最多能加的炮兵数. 注释:n<=100,m<=10.然后只能在平原的地方建立炮兵. 想法:第2到状压dp,++.这题显 ...

  2. POJ1185炮兵阵地【动态规划】

    炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26892   Accepted: 10396 Descriptio ...

  3. POJ2411 Mondriaan's Dream(状态压缩)

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15295   Accepted: 882 ...

  4. poj1185 炮兵阵地【状压DP】

    炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32802   Accepted: 12650 Descriptio ...

  5. poj2411 Mondriaan's Dream【状压DP】

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20822   Accepted: 117 ...

  6. POJ1185 炮兵阵地 —— 状压DP

    题目链接:http://poj.org/problem?id=1185 炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions ...

  7. [Poj2411]Mondriaan's Dream(状压dp)(插头dp)

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18096   Accepted: 103 ...

  8. poj2411 Mondriaan's Dream (轮廓线dp、状压dp)

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17203   Accepted: 991 ...

  9. [poj2411] Mondriaan's Dream (状压DP)

    状压DP Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One nigh ...

随机推荐

  1. Net Core 3 Mvc AliPay Demo

    AliPay - PC 钻研了几天的Webpack, 这几天回归了我的本行.Net, 跟随大佬的脚步, 开始做上了支付宝的支付. 创建项目 首先创建一基于.Net Core3.0的MVC项目, 然后引 ...

  2. Asp.Net Core中创建多DbContext并迁移到数据库

    在我们的项目中我们有时候需要在我们的项目中创建DbContext,而且这些DbContext之间有明显的界限,比如系统中两个DbContext一个是和整个数据库的权限相关的内容而另外一个DbConte ...

  3. Java静态分派和动态分派

    前言 动态分派和静态分派机制是Java多态实现的原理.本文将针对这两种机制进行浅析. 静态分派 静态分派机制最典型的代码示例如下 void test() { Father father = new S ...

  4. Python进阶:值传递,引用传递?不存在的,是赋值传递

    Python的变量及其赋值 c/c++/c#/java 里面都有值传递,引用传递的概念,在Python中是如何的? 例 a = 1 b = a print(id(a),id(b)) #14072334 ...

  5. go String接口方法

    该接口经常用于输出 struct 的值 或者记录struct数据日志 一个普遍存在的接口是 fmt 包中定义的 Stringer接口 发现 http://tour.studygolang.com/me ...

  6. Vue.js 2.x 混入

    Vue.js 2.x mixins 混入 混入(mixins)是一种分发vue组件中可复用功能的非常灵活的方式.混入对象可以包含任意组件选项.当组件使用混入对象时,所有混入对象的选项将被混入该组件本身 ...

  7. 『Python基础』第7节:基本运算符

    一. 基本运算符 运算按种类可以分为: 算数运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算. 今天我们只学习算数运算.比较运算.逻辑运算.赋值运算.成员运算 1.1 算数运算 以下假设 ...

  8. 递推问题 hdu 2046 与1143的比对

    2046 在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.例如n=3时,为2× 3方格,骨牌的铺放方案有三种,如下图:   Input 输入数据由多行组成,每行 ...

  9. Django2.0 分页的应用

    #分页例子from django.core.paginator import Paginatordef blog_list(request):      blog_all_list = models. ...

  10. java开发中常用语(词汇)含义

    DAO:Data Access Objects(数据存取对象): DTO:Data Transfer Object: AR:active record: