状圧DP入門問題

10963 ワード

一:四角数
質問説明:Descriptionはn*nの格子の碁盤をあげて、各格子の中に非負の数があります.任意の2つの数が存在する格子に共通のエッジがないように、いくつかの数を取り出します.つまり、取得した数が存在する2つの格子が隣接することができず、取得した数の和が最大になります.Inputは複数の試験例を含み、各試験例は1つの整数nとn*n個の負でない数(n<=20)Outputを含み、各試験例について、取得可能な最大値とSample Input 3 75 15 21 75 15 28,475 Sample Output 188を出力する.
問題を解く構想:1.まず、すべての状態(0から1<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 Mby 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.
芝生があります.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<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;
}