poj 1185 炮兵阵地 状压DP
来源:互联网 发布:java jsonobject jar 编辑:程序博客网 时间:2024/06/01 08:28
//poj 1185
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=0xfffffff;
char map[20];
int cur[110];
int ant[70];
int dp[110][70][70];
int n,m;
int top;
bool ok(int x)
{
if(x&(x<<1)) return 0;
if(x&(x<<2)) return 0;
return 1;
}
void find()
{
for(int i=0;i<(1<<m);i++)
if(ok(i))
ant[++top]=i;
}
int count1(int x)
{
int cnt=0;
while(x)
{
x&=(x-1);
cnt++;
}
return cnt;
}
int main( )
{
//freopen("1.txt","r",stdin);
while(scanf("%d %d",&n,&m)!=EOF)
{
memset(cur,0,sizeof(cur));
for(int i=1;i<=n;i++)
{
scanf("%s",map);
for(int j=0;j<=m;j++)
if(map[j]=='H')
cur[i]=cur[i]|(1<<j);
}
top=0;
find();
memset(dp,-1,sizeof(-1));
for(int i=1;i<=top;i++)
{
if(!(ant[i]&cur[1]))
dp[1][1][i]=count1(ant[i]);
}
for(int i=2;i<=n;i++)
{
for(int t=1;t<=top;t++) //i行
{
if(cur[i]&ant[t]) continue;
for(int k=1;k<=top;k++) //i-1行
{
if(ant[t]&ant[k]) continue; //i-2行
for(int j=1;j<=top;j++)
{
if(ant[j]&ant[t]) continue;
dp[i][k][t]=max(dp[i][k][t],dp[i-1][j][k]+count1(ant[t]));
}
}
}
}
int ans=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=top;j++)
for(int k=1;k<=top;k++)
ans=max(ans,dp[i][j][k]);
printf("%d\n",ans);
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=0xfffffff;
char map[20];
int cur[110];
int ant[70];
int dp[110][70][70];
int n,m;
int top;
bool ok(int x)
{
if(x&(x<<1)) return 0;
if(x&(x<<2)) return 0;
return 1;
}
void find()
{
for(int i=0;i<(1<<m);i++)
if(ok(i))
ant[++top]=i;
}
int count1(int x)
{
int cnt=0;
while(x)
{
x&=(x-1);
cnt++;
}
return cnt;
}
int main( )
{
//freopen("1.txt","r",stdin);
while(scanf("%d %d",&n,&m)!=EOF)
{
memset(cur,0,sizeof(cur));
for(int i=1;i<=n;i++)
{
scanf("%s",map);
for(int j=0;j<=m;j++)
if(map[j]=='H')
cur[i]=cur[i]|(1<<j);
}
top=0;
find();
memset(dp,-1,sizeof(-1));
for(int i=1;i<=top;i++)
{
if(!(ant[i]&cur[1]))
dp[1][1][i]=count1(ant[i]);
}
for(int i=2;i<=n;i++)
{
for(int t=1;t<=top;t++) //i行
{
if(cur[i]&ant[t]) continue;
for(int k=1;k<=top;k++) //i-1行
{
if(ant[t]&ant[k]) continue; //i-2行
for(int j=1;j<=top;j++)
{
if(ant[j]&ant[t]) continue;
dp[i][k][t]=max(dp[i][k][t],dp[i-1][j][k]+count1(ant[t]));
}
}
}
}
int ans=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=top;j++)
for(int k=1;k<=top;k++)
ans=max(ans,dp[i][j][k]);
printf("%d\n",ans);
}
return 0;
}
0 0
- POJ 1185--炮兵阵地(状压dp)
- poj 1185 炮兵阵地 状压DP
- POJ 1185 炮兵阵地 (状压DP)
- POJ 1185 炮兵阵地(状压dp)
- 炮兵阵地 - POJ 1185 状压dp
- POJ 1185 炮兵阵地(状压DP)
- poj 1185 炮兵阵地 状压DP
- POJ 1185-炮兵阵地(状压DP)
- POJ 1185 炮兵阵地 (状压DP)
- POJ 1185 炮兵阵地(状压dp)
- poj 1185 炮兵阵地 状压dp
- poj 1185 炮兵阵地 【状压DP】
- poj 1185 炮兵阵地(状压DP)
- POJ 1185 炮兵阵地 状压DP
- poj 1185 炮兵阵地(状压dp)
- POJ 1185 炮兵阵地 (状压dp)
- poj 1185 炮兵阵地 状压dp
- 【POJ 1185】炮兵阵地(状压DP)
- echo 命令
- POJ 3253 fence Repair 一个很有趣的贪心解法
- winform数字软键盘
- http://acm.hdu.edu.cn/showproblem.php?pid=1385
- 【未完】11条数据库设计准则
- poj 1185 炮兵阵地 状压DP
- Party All the Time
- JSON-lib框架,转换JSON、XML不再困难
- runInstaller-- you do not have sufficient permissions to access the inventory
- javaBean为什么要实现Serializable接口?
- Efficiently factor N when e and d become known to you.
- 数据结构5:线性表中把属于la中而不属于lb中的元素放到la中
- 使用typedef定义函数指针
- xStream完美转换XML、JSON