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

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.
#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)的更多相关文章
- HDU 4284Travel(状压DP)
HDU 4284 Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1 ...
- 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表示 ...
- HDU 3001 Travelling ——状压DP
[题目分析] 赤裸裸的状压DP. 每个点可以经过两次,问经过所有点的最短路径. 然后写了一发四进制(真是好写) 然后就MLE了. 懒得写hash了. 改成三进制,顺利A掉,时间垫底. [代码] #in ...
- HDU - 5117 Fluorescent(状压dp+思维)
原题链接 题意 有N个灯和M个开关,每个开关控制着一些灯,如果按下某个开关,就会让对应的灯切换状态:问在每个开关按下与否的一共2^m情况下,每种状态下亮灯的个数的立方的和. 思路1.首先注意到N< ...
- hdu 4114(状压dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4114 思路:首先是floyd预处理出任意两点之间的最短距离.dp[state1][state2][u] ...
- HDU 3091 - Necklace - [状压DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3091 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- HDU 3811 Permutation 状压dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...
- HDU 5838 (状压DP+容斥)
Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...
- hdu 4628 Pieces 状压dp
题目链接 枚举所有状态, 1表示这个字符还在原来的串中, 0表示已经取出来了. 代码中j = (j+1)|i的用处是枚举所有包含i状态的状态. #include <iostream> #i ...
随机推荐
- LeetCode OJ 86. Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 不同版本的Api控件显示可能不同
同样的一个布局, compileSdkVersion 24(android7.0)和 compileSdkVersion 18(android4.3)得到的结果可能不一样: <FrameLayo ...
- Fibonacci 数列第 N项 O(logN)算法
时间复杂度为O( log n )的方法: 该算法使用矩阵乘法操作,使得算法时间复杂度为 O(logN) long long Fibonacci( unsigned n ) { ] = {, }; ) ...
- Linux文件系统中硬链接和软链接的区别 (转)
建立硬链接命令:ln src-link dest-link建立软链接:ln -s src-link dest-link 1. 硬链接是别名,软链接是快捷方式 2. 硬链接和源链接指向同一个i节 ...
- perl的logwrapper
来源: http://www.cnblogs.com/itech/archive/2012/09/22/2698385.html 对任何的函数将标准输出和错误输出重定向到对应的log文件. 对任何的函 ...
- 根据View获取该控制器
//根据View获取控制器 - (UIViewController*)viewController { for (UIView* next = [self superview]; next; next ...
- 使用anyremote进行远程鼠标控制
源代码安装 http://anyremote.sourceforge.net/pre.html 安装 -xtest apt-get install libxtst-dev 安装 glib sudo ...
- 1.1 python 安装(Windows)
1.浏览器输入URL http://www.python.org/download/ https://www.python.org/downloads/release/python-2712/ Win ...
- UVA - 10635 最长公共子序列
input n,p,q 2<=n<=250 1<=p,q<=n*n 1 a1 a2 a3 ... ap 1<ai<n*n,ai!=aj 1 b1 b2 b3 ... ...
- Windows下MySQL分步安装图解及问题总结
MySQL是一个出色的开源数据库,在易用性和性能方面都有相当不错的表现.就作者发帖为止, MySQL官方发布的所有版本中(4.1/5.0/5.1/6.0),推荐使用稳定的MySQL5.0版本(商用). ...