poj3254 poj1185 状态压缩DP

  刚做状态压缩DP。。思路和一般的DP一样,只是每次都对应一种状态。

  目前只会最简单的。。用一个数的二进制表示一种状态。


Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5337 Accepted: 2836

Description

Farmer John has purchased a lush new rectangular pasture composed of M byN (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.


  这个就是最简单的一个题,首先初始化,把一行里满足的情况用st数组记下(先假设图中的每个地方都可以种植物,st里的情况只保证没有相邻的植物),判断有没有相邻的是i&(i<<1)。接着再用数组a[i]记下来第i行不满足题意的情况,比如第2行010,a[2]=101,到时再把情况和a进行&运算,答案必须是0才成立。接下来就是DP,dp[i][j]代表第i行状态是j(st[j])的情况有多少种满足的,先初始化,如果第j个状态满足,那么dp[1][j]=1,假设第i-1行l状态和i行j状态不矛盾,那么dp[i][j]=dp[i][j]+dp[i-1][l],判断第i行和第i-1行是否矛盾也要用&运算(不能对应的位置都是1)。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#define INF 0x3f3f3f3f
#define MOD 100000000
using namespace std;
int M,N,num,ans,st[1000],a[13],dp[13][1000];
int fit(int a,int b)
{
    return a&b?0:1;
}
void init()
{
    int i,S;
    S=1<<N;
    for(i=0; i<S; i++)
    {
        if(i&(i<<1)) continue;
        st[num++]=i;
    }
}
void DP()
{
    int i,j,l;
    for(i=0; i<num; i++)
        if(fit(a[1],st[i])) dp[1][i]=1;
    for(i=2; i<=M; i++)
        for(j=0; j<num; j++)
        {
            if(!fit(a[i],st[j])) continue;
            for(l=0; l<num; l++)
            {
                if(!fit(a[i-1],st[l])||!fit(st[j],st[l])) continue;
                dp[i][j]=(dp[i][j]+dp[i-1][l])%MOD;
            }
        }
}
int main()
{
    freopen("in.txt","r",stdin);
    while(scanf("%d%d",&M,&N)!=EOF)
    {
        int i,j,t;
        num=0;
        ans=0;
        memset(a,0,sizeof(a));
        memset(dp,0,sizeof(dp));
        init();
        for(i=1; i<=M; i++)
            for(j=1; j<=N; j++)
            {
                scanf("%d",&t);
                if(t==0) a[i]+=1<<(j-1);
            }
        DP();
        for(i=0; i<num; i++)
            ans=(ans+dp[M][i])%MOD;
        printf("%d\n",ans);
    }
    return 0;
}

炮兵阵地
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15946 Accepted: 6056

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

  这个比上一个难一些,因为附近两格之内都不能再有,因此dp数组变成了三维的,dp[i][j][k]代表第i-1行状态是j,第i行状态是k,这样循环也要多一层,判断是否矛盾的时候要把两行都判断,先判断这两行满不满足,如果满足,再枚举这两行上一行的情况。题目问的是最多能放多少个炮,那么用cnt[i]数组记录状态i时的一行有多少个炮(多少个1),在所有都不矛盾的情况下,dp[i][j][k]=max(dp[i][j][k],dp[i-1][l][j]+cnt[k])。在初始化函数里算cnt,如果想知道状态i有多少个1,就让i=i&(i-1),直到i=0,能进行多少次就有多少个1(每进行一次最后那个1就变成0)。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#define INF 0x3f3f3f3f
using namespace std;

int N,M,num,ans;
int a[110],cnt[1<<11],st[1<<11],dp[110][110][110];

int fit(int a,int b)
{
    return a&b?0:1;
}

void init()
{
    int i,S=1<<M;
    for(i=0; i<S; i++)
    {
        if(i&(i<<1)||i&(i<<2))
            continue;
        st[num]=i;
        int m=i,s=0;
        while(m)
        {
            m&=m-1;
            s++;
        }
        cnt[num++]=s;
    }
}

void DP()
{
    int i,j,k,l;
    for(i=0; i<num; i++)   //处理第1行
    {
        if(!fit(a[1],st[i]))
            continue;
        dp[1][0][i]=cnt[i];
        ans=max(ans,dp[1][0][i]);
    }
    for(i=2; i<=N; i++)
        for(j=0; j<num; j++)
            for(k=0; k<num; k++)
            {
                if(!fit(st[k],a[i])||!fit(st[j],a[i-1])||!fit(st[k],st[j])) //k和j这两行不满足
                    continue;
                for(l=0; l<num; l++)
                {
                    if(!fit(st[l],a[i-2])||!fit(st[l],st[k])||!fit(st[l],st[j])) //上一行满足的情况
                        continue;
                    dp[i][j][k]=max(dp[i][j][k],dp[i-1][l][j]+cnt[k]);
                    ans=max(ans,dp[i][j][k]);
                }
            }
}

int main()
{
    freopen("in.txt","r",stdin);
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        getchar();
        int i,j;
        char ch;
        num=0;
        ans=0;
        memset(a,0,sizeof(a));
        memset(dp,0,sizeof(dp));
        init();
        for(i=0; i<N; i++)
        {
            for(j=0; j<M; j++)
            {
                ch=getchar();
                if(ch=='H')
                {
                    a[i+1]+=1<<j;
                }
            }
            getchar();
        }
        DP();
        printf("%d\n",ans);
    }
    return 0;
}


  这两题思路是一样的,都是以行来DP,先要对第一行初始化,然后判断各种是否矛盾,如果这行不跟题目给的条件矛盾,就枚举上行的情况,如果仍然不矛盾就状态转移。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值