您的位置:首页 > 产品设计 > 产品经理

状压DP ( 预处理减少状态 )——炮兵阵地 (POJ 1185)

2016-08-08 16:20 639 查看
题目链接:

http://poj.org/problem?id=1185

题意:给出一个N*M的平面图,P代表平地,H代表山地,平地上可以放置炮兵,炮兵的攻击范围是上下左右各延伸2格。求在炮兵不会互相攻击到的情况下这个图中最多能放置多少个炮兵

分析:

这道题和POJ 3254很相似,首先我们先预处理一下在一行长度为M的情况下,炮兵不会互相攻击到的放置方法有多少种,并求出每一种放置方法放了多少个炮兵,记录下来:

int con[66];
int way[66];
int cnt;
void init()
{
cnt = 0;
for(int i=0;i<(1<<M);i++)
{
if( (i&(i<<1)) != 0)
continue;
if( (i&(i<<2)) != 0)
continue;
con[cnt] = Count(i);
way[cnt++] = i;
}
}


计算用i方法放置时候能放置多少个炮兵:

int Count(int x)//即计算x转换为二进制后含有多少个1
{
int ans = 0;
while(x)
{
x = x & (x-1);
ans++;
}
return ans;
}


然后我们再根据给出的平面图,判断第x行用y方法放置能否成功

int check(int x, int y)
{
if( (Map[x] & y) != y)
return 0;
if( (y&(y<<1)) !=0)
return 0;
if( (y&(y<<2)) !=0)
return 0;
return 1;
}


这样我们预处理得出了一个数组Legal[i][j],表示第i行用j方法可以放置多少个炮兵。

接下来我们开始写我们的转移方程:

因为炮兵的射程是上下也有2行,所以我们每次不仅要记录下当前行的炮兵放置状态,同时还需要记录上一行的炮兵放置状态,这样我们放置下一行的时候,就可以取出上一次存放的状态(即上一行和上上一行)进行比较判断当前的状态 j 能否放置,然后取最大值保留

int tmp = Legal[这次][j];
if( (way[j]&way[k]) == 0 && (way[j]&way[l]) == 0 )
DP[这次][j][k] = MAX(DP[这次][j][k], DP[上次][k][l]+tmp);


AC代码:

/*************************************************************************
> File Name: test.cpp
> Author: Akira
> Mail: qaq.febr2.qaq@gmail.com
************************************************************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <set>
#include <list>
#include <ctime>
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))
using namespace std;

#define MaxN 100000
#define MaxM MaxN*10
#define INF 0x3f3f3f3f
#define bug cout<<88888888<<endl;
#define MIN(x,y) (x<y?x:y)
#define MAX(x,y) (x>y?x:y)

template<typename _> inline void scan(_& t)
{
int c;
while((c = getchar()) < '0' || c > '9');
t = c - '0';
while((c = getchar()) >= '0' && c <= '9') t = t * 10 + c - '0';
}
template<typename _> inline void print(_ x)
{
int len = 0, p[20];
if(x < 0) putchar('-'), x = -x;
while(x) p[++len] = x % 10, x /= 10;
if(!len) p[++len] = 0;
while(len) putchar(p[len--] + '0');
}

int N,M;
int Map[101];
char str[10];
int cnt;
int way[66];
int num[66];
int Legal[101][66];
int DP[101][66][66];
int Count(int x)
{
int tmp = 0;
while(x)
{
tmp++;
x &= (x-1);
}
return tmp;
}
bool check(int a, int x)
{
if( (Map[a]&x) != x || (x&(x<<1)) != 0 || (x&(x<<2)) !=0 ) return 0;
else return 1;
}
void init()
{
cnt = 0;
for(int i=0; i< (1<<M); i++)
{
if( (i&(i<<1)) != 0 || (i&(i<<2)) !=0 ) continue;
way[cnt] = i;
num[cnt++] = Count(i);
}
for(int i=1;i<=N;i++)
{
for(int j=0;j<cnt;j++)
{
if(check(i,way[j])) Legal[i][j] = num[j];
}
}
}

void solve()
{
init();
int flag = 0;
for(int i=1;i<=N;i++)
{
//CLR(DP[flag^1]);
for(int j=0;j<cnt;j++)
{
int tmp = Legal[i][j];
for(int k=0;k<cnt;k++)
{
for(int l=0;l<cnt;l++)
{
if( (way[j]&way[k]) == 0 && (way[j]&way[l]) == 0 ) DP[flag][j][k] = MAX(DP[flag][j][k], DP[flag^1][k][l]+tmp);
}
}
}
flag^=1;
}
int ans = 0;
for(int j=0;j<cnt;j++)
{
for(int k=0;k<cnt;k++)
{
ans = MAX(ans, DP[flag^1][j][k]);
}
}
cout << ans <<endl;
}

int main()
{
scan(N);scan(M);
for(int i=1;i<=N;i++)
{
scanf("%s", str);
for(int j=0;j<M;j++)
{
int x;
if(str[j] == 'P') x = 1;
else x = 0;
Map[i] = (Map[i]<<1) + x;
}
}
solve();
system("pause");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  状压DPM 状态压缩 ACM