题目链接: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[][<<];
int cnt[][<<];
int a[][],n;
char s[];
int vis[];
int maxx = ;
void dfs(int i,int now,int state,int sumstate)
{
maxx = max(maxx,dp[i][sumstate]);
if(vis[now]==)
{
if(now<n-)
{
dfs(i,now+,,sumstate);
}
else
{
return;
}
}
else if(vis[now]==)
{
if(now>=n-&&state==) dfs(i,now+,,sumstate+(<<now));
else if(now>=n-&&state==) return ;
else if(now<n-&&state==)
{
dfs(i,now+,,sumstate+(<<now));
dfs(i,now+,,sumstate);
}
else if(now<n-&&state==)
{
dfs(i,now+,,sumstate);
}
}
return;
}
int cal(int c1,int c2)
{
return ((c1-'')*+c2-'');
}
bool judge(int state) //判断此状态是否符合
{
for(int i=;i<n-;i++)
{
if(((<<i)&state)>&&((<<(i+))&state)>) return ;
}
return ;
}
int main()
{
while(gets(s))
{
int len = strlen(s);
n = (len+)/;
int ans = ;
for(int i=;i<len;i+=)
{
a[][ans++] = cal(s[i],s[i+]);
}
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(int i=;i<n;i++) //预处理记录cnt[i][j]
{
for(int j=;j<(<<(n));j++)
{
int sum = ;
for(int k=;k<n;k++)
{
if(((<<k)&j)>)
{
sum += a[i][k];
}
}
cnt[i][j] = sum;
}
}
for(int j=;j<(<<n);j++) dp[][j] = cnt[][j];
for(int i=;i<n;i++) //枚举每一行,枚举每个状态,判断可行性。
{
for(int j=;j<(<<n);j++)
{
if(!judge(j)) continue;
memset(vis,,sizeof(vis));
for(int k=;k<n;k++)
{
if(k==)
{
if(((<<k)&j)==&&((<<(k+))&j)==)
{
vis[k] = ;
}
}
else if(k==n-)
{
if(((<<k)&j)==&&((<<(k-))&j)==)
{
vis[k] = ;
}
}
else
{
if(((<<k)&j)==&&((<<(k-))&j)==&&((<<(k+))&j)==)
{
vis[k] = ;
}
}
}
maxx = ; //dfs寻找i-1行的最大值
if(vis[]==)
dfs(i-,,,);
else
{
dfs(i-,,,);
dfs(i-,,,);
}
dp[i][j] = maxx+cnt[i][j];
}
}
getchar();
gets(s);
int maxn = ;
for(int i=;i<(<<n);i++) maxn = max(maxn,dp[n-][i]);
printf("%d\n",maxn);
memset(dp,,sizeof(dp));
}
return ;
}
/*
10 20 30
10 20 30
10 20 30
*/
 
 

HDU 2167 Pebbles(状压DP)的更多相关文章

  1. HDU 4284Travel(状压DP)

    HDU 4284    Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1 ...

  2. HDU 4336 容斥原理 || 状压DP

    状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示 ...

  3. HDU 3001 Travelling ——状压DP

    [题目分析] 赤裸裸的状压DP. 每个点可以经过两次,问经过所有点的最短路径. 然后写了一发四进制(真是好写) 然后就MLE了. 懒得写hash了. 改成三进制,顺利A掉,时间垫底. [代码] #in ...

  4. HDU - 5117 Fluorescent(状压dp+思维)

    原题链接 题意 有N个灯和M个开关,每个开关控制着一些灯,如果按下某个开关,就会让对应的灯切换状态:问在每个开关按下与否的一共2^m情况下,每种状态下亮灯的个数的立方的和. 思路1.首先注意到N< ...

  5. hdu 4114(状压dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4114 思路:首先是floyd预处理出任意两点之间的最短距离.dp[state1][state2][u] ...

  6. HDU 3091 - Necklace - [状压DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3091 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  7. HDU 3811 Permutation 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...

  8. HDU 5838 (状压DP+容斥)

    Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...

  9. hdu 4628 Pieces 状压dp

    题目链接 枚举所有状态, 1表示这个字符还在原来的串中, 0表示已经取出来了. 代码中j = (j+1)|i的用处是枚举所有包含i状态的状态. #include <iostream> #i ...

随机推荐

  1. JqGrid动态改变列名

    setLabel colname, data, class, properties jqGrid对象 给指定列设置一个新的显示名称.colname:列名称,也可以是列的位置索引,从0开始:data:列 ...

  2. PHP之输出控制 ob_start(),ob_get_contents(),ob_end_clean()

    1.常用函数 ob_start();#打开输出缓冲区 ob_get_contents();#获取缓冲区内容 ob_get_length();#获取缓冲区内容长度 ob_clean();#清除之前的所有 ...

  3. Windows 系统变量大全

    来源:http://blog.csdn.net/kingwolf_javascript/article/details/2477234 %ALLUSERSPROFILE% : 列出所有用户Profil ...

  4. 理解Java String和String Pool

    本文转载自: http://blog.sina.com.cn/s/blog_5203f6ce0100tiux.html 要理解 java中String的运作方式,必须明确一点:String是一个非可变 ...

  5. su -无反应

    一.问题产生环境 CentOS 6.3 X64SecureCRT 7.0.0 英文版 二.问题具体描述 今天操作自己的一台Linux服务器时,突然su命令不好使了,敲入命令回车后,没有任何反应,不管是 ...

  6. 倒计时demo

    #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (strong,nonatom ...

  7. sqlserver-根据2张表的id更新其他列值

    update a set a.ORGID = (select b.ORG_ID from PT_SERVICE b where a.SERVICEOID = b.SERVICEOID and a.OR ...

  8. List<T>转换为ObservableCollection<T>

    ObservableCollection能通知他变化了也正是因为它实现了INotifyPropertyChanged接口, 在wpf项目中经常会遇到把List<T>转换为Observabl ...

  9. 【二分图】 poj 1466

    #include <iostream> #include <memory.h> #include <cstdio> using namespace std; int ...

  10. HDU2586 How far away ? 邻接表+DFS

    题目大意:n个房子,m次询问.接下来给出n-1行数据,每行数据有u,v,w三个数,代表u到v的距离为w(双向),值得注意的是所修建的道路不会经过一座房子超过一次.m次询问,每次询问给出u,v求u,v之 ...