状压DP 入门题

一:方格取数

问题描述:
Description
给你一个n*n的格子的棋盘,每个格子里面有一个非负数。
从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数的和最大。
Input
包括多个测试实例,每个测试实例包括一个整数n 和n*n个非负数(n<=20)
Output
对于每个测试实例,输出可能取得的最大的和
Sample Input
3
75 15 21
75 15 28
34 70 5
Sample Output
188

解题思路:
1.先枚举所有状态(从0到1<< n)找出不含相邻1的状态,那么这就是满足每一行不相邻的所有合法状态。
2.特殊处理第1行的状态。上一步找到的合法状态都可以作为是第1行的状态。
3.枚举后面的行。后面的行要满足2个条件。一就是没有相邻的1。二是和上一行的1不冲突。找到合法的状态,利用dp求出最大值就好了。

ac代码:

#include
#include
#include
#include
using namespace std;
int dp[25][18000];
int num[25][18000];
int state[18000];
int mapp[25][25];
int N,top,total;
void init()
{
    top=0;
    int i;
    total=1<for(i=0;iif((i&i<<1))
            continue;
        state[++top]=i;
    }
}
int fit(int x,int k) //计算x状态,在k行的和。
{
    int sum=0;
    for(int i=1;i<=N;i++)
    {
        if((x>>(i-1)&1))
            sum+=mapp[k][i];
    }
    return sum;
}
int main()
{
    while(cin>>N)
    {
        int i,j,k,z;
        memset(dp,0,sizeof(dp));
        for(i=1;i<=N;i++)
        {
            for(j=1;j<=N;j++)
            {
                cin>>mapp[i][j];
            }
        }
        init();
        for(i=1;i<=top;i++)
        {
            num[1][i]=dp[1][i]=fit(state[i],1);
            for(j=2;j<=N;j++)
            {
                num[j][i]=fit(state[i],j);
            }
        }
        for(i=2;i<=N;i++)
        {
            for(j=1;j<=top;j++)   //遍历第i行的状态
            {
                for(k=1;k<=top;k++)//遍历第i-1行的状态
                {
                    if(state[j]&state[k])
                        continue;
                    dp[i][j]=max(dp[i][j],dp[i-1][k]+num[i][j]);
                }
            }
        }
        int maxx=-1;
        for(i=1;i<=top;i++)
        {
            maxx=max(maxx,dp[N][i]);
        }
        cout<return 0;
}

二: Corn Fields
问题描述:
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.

大致题意:
有一个草地,1是肥沃的。0是贫瘠的。要把牛放在肥沃的地上,并且不能相邻。问有几种放法。和上一个题很类似。

ac代码:

#include
#include
#include
#include
using namespace std;
int N,M;
int top,total;
int state[1000];  //满足不相邻的状态。
int dp[20][1000];
int cur[20];   //地图。
void init()
{
    total=1<0;
    for(int i=0;iif(i&(i<<1))
            continue;
        state[++top]=i;
    }
}
bool fit(int x,int k) //判断状态x与第k行的地图是否符合。
{
    if(x&cur[k])
        return 0;
    return 1;
}
int main()
{
    while(~scanf("%d%d",&N,&M))
    {
        int i,j,k,mapp;
        memset(dp,0,sizeof(dp));
        memset(cur,0,sizeof(cur));
        memset(state,0,sizeof(state));
        init();
        for(i=1;i<=N;i++) //把地图打表。
        {
            for(j=1;j<=M;j++)
            {
                cin>>mapp;
                if(mapp==0)
                    cur[i]+=1<<(M-j);
            }
        }
        for(i=1;i<=top;i++)
        {
            if(fit(state[i],1))
                dp[1][i]=1;
        }
        for(i=2;i<=N;i++)
        {
            for(j=1;j<=top;j++)
            {
                if(!fit(state[j],i))
                    continue;
                //再看是不是和上一行冲突。
                for(k=1;k<=top;k++)
                {
                    if(!fit(state[k],i-1))
                        continue;
                    if(state[k]&state[j])
                        continue;
                    dp[i][j]+=dp[i-1][k];
                }
            }
        }
        int sum=0;
        for(k=1;k<=top;k++)
        {
            sum=(sum+dp[N][k])%100000000;
        }
        cout<return 0;
}

你可能感兴趣的:(状态压缩dp)