HDU 2167 Pebbles(状压DP)

题目链接:Pebbles

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1504    Accepted Submission(s): 865


Problem Description
You're given an unlimited number of pebbles to distribute across an N x N game board (N drawn from [3, 15]), where each square on the board contains some positive point value between 10 and 99, inclusive. A 6 x 6 board might look like this:

The player distributes pebbles across the board so that:

?At most one pebble resides in any given square.
?No two pebbles are placed on adjacent squares. Two squares are considered adjacent if they are horizontal, vertical, or even diagonal neighbors. There's no board wrap, so 44 and 61 of row three aren't neighbors. Neither are 33 and 75 nor 55 and 92.

The goal is to maximize the number of points claimed by your placement of pebbles.

Write a program that reads in a sequence of boards from an input file and prints to stdout the maximum number of points attainable by an optimal pebble placement for each. 

 
Input
Each board is expressed as a series of lines, where each line is a space-delimited series of numbers. A blank line marks the end of each board (including the last one)

 
Output
then your program would print the maximum number of points one can get by optimally distributing pebbles while respecting the two rules, which would be this (each output should be printed on a single line and followed with a newline):

 题解:代码虽然长,却是我调了四个小时调出来的。QAQ
二进制表示每一行的状态,初始化记录一整行每一个状态的值,用cnt[i][j]表示,j表示这一行的状态。然后dp开始转移,对于第 i 行,枚举每一个状态j,对于状态j,首先分析j是否满足相邻的最多有一个石子,然后考虑 i-1 位,分析i-1可能的状态,dfs一下寻找i-1行的最大值maxx,所以dp[i][j]  = maxx + cnt[i][j].
 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int dp[17][1<<17];
int cnt[17][1<<17];
int a[17][17],n;
char s[50];
int vis[20];
int maxx = 0;
void dfs(int i,int now,int state,int sumstate)
{
    maxx = max(maxx,dp[i][sumstate]);
    if(vis[now]==0)
    {
        if(now<n-1)
        {
            dfs(i,now+1,1,sumstate);
        }
        else
        {
            return;
        }
    }
    else if(vis[now]==1)
    {
        if(now>=n-1&&state==1) dfs(i,now+1,0,sumstate+(1<<now));
        else if(now>=n-1&&state==0) return ;
        else if(now<n-1&&state==1)
        {
            dfs(i,now+1,0,sumstate+(1<<now));
            dfs(i,now+1,1,sumstate);
        }
        else if(now<n-1&&state==0)
        {
            dfs(i,now+1,1,sumstate);
        }
    }
    return;
}
int cal(int c1,int c2)
{
    return ((c1-'0')*10+c2-'0');
}
bool judge(int state)   //判断此状态是否符合
{
    for(int i=0;i<n-1;i++)
    {
        if(((1<<i)&state)>0&&((1<<(i+1))&state)>0) return 0;
    }
    return 1;
}
int main()
{
    while(gets(s))
    {
        int len = strlen(s);
        n = (len+1)/3;
        int ans = 0;
        for(int i=0;i<len;i+=3)
        {
            a[0][ans++] = cal(s[i],s[i+1]);
        }
        for(int i=1;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        for(int i=0;i<n;i++)      //预处理记录cnt[i][j]
        {
            for(int j=0;j<(1<<(n));j++)
            {
                int sum = 0;
                for(int k=0;k<n;k++)
                {
                    if(((1<<k)&j)>0)
                    {
                        sum += a[i][k];
                    }
                }
                cnt[i][j] = sum;
            }
        }
        for(int j=0;j<(1<<n);j++) dp[0][j] = cnt[0][j];
        for(int i=1;i<n;i++)             //枚举每一行,枚举每个状态,判断可行性。
        {
            for(int j=0;j<(1<<n);j++)
            {
                if(!judge(j)) continue;
                memset(vis,0,sizeof(vis));
                for(int k=0;k<n;k++)
                {
                    if(k==0)
                    {
                        if(((1<<k)&j)==0&&((1<<(k+1))&j)==0)
                        {
                            vis[k] = 1;
                        }
                    }
                    else if(k==n-1)
                    {
                        if(((1<<k)&j)==0&&((1<<(k-1))&j)==0)
                        {
                            vis[k] = 1;
                        }
                    }
                    else
                    {
                        if(((1<<k)&j)==0&&((1<<(k-1))&j)==0&&((1<<(k+1))&j)==0)
                        {
                            vis[k] = 1;
                        }
                    }
                }
                maxx = 0;   //dfs寻找i-1行的最大值
                if(vis[0]==0)
                dfs(i-1,0,0,0);
                else
                {
                    dfs(i-1,0,1,0);
                    dfs(i-1,0,0,0);
                }
                dp[i][j] = maxx+cnt[i][j];
            }
        }
        getchar();
        gets(s);
        int maxn = 0;
        for(int i=0;i<(1<<n);i++) maxn = max(maxn,dp[n-1][i]);
        printf("%d
",maxn);
        memset(dp,0,sizeof(dp));
    }
    return 0;
}
/*
10 20 30
10 20 30
10 20 30
*/
 
 
原文地址:https://www.cnblogs.com/littlepear/p/5747723.html