1. 程式人生 > >狀壓DP 入門題

狀壓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<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
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<<N;
    for(i=0;i<total;i++)
    {
        if
((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<<maxx<<endl; } 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<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
using namespace std;
int N,M;
int top,total;
int state[1000];  //滿足不相鄰的狀態。
int dp[20][1000];
int cur[20];   //地圖。
void init()
{
    total=1<<M;
    top=0;
    for(int i=0;i<total;i++)
    {
        if(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<<sum<<endl;
    }
    return 0;
}