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 ...
随机推荐
- hdu_4714_Tree2cycle(树形DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意:给你N个点N-1条边,形成一个树,让你拆树,并连接成一个环,每拆一次,连接一次,消耗1,问 ...
- 第13章 Swing程序设计
1.Swing概述 GUI(图形用户界面)为程序提供图形界面,最初的设计目的是为程序员构建一个通用的GUI,使其能够在所有平台上运行.但Java 1.0中基础类AWT(抽象窗口工具箱)并没有达到这个要 ...
- TextBox只读时不能通过后台赋值取值解决办法
给页面的TextBox设置ReadOnly="True"时,在后台代码中不能赋值取值,下边几种方法可以避免: 1.不设置ReadOnly,设置onfocus=this.blur( ...
- MySQL-测试卷一
MySQL-测试卷一 一.单项选择题 1 下面不属于Msql数据库特点的是( ) A. 免费使用 B.不能跨平台 C.开源软件 D.功能强大 2 定义表的一个字段, 要求能表示4位整数,2位小 ...
- 线段树+dp+贪心 Codeforces Round #353 (Div. 2) E
http://codeforces.com/contest/675/problem/E 题目大意:有n个车站,每个车站只能买一张票,这张票能从i+1到a[i].定义p[i][j]为从i到j所需要买的最 ...
- linux压缩和解压缩命令
压缩:tar -zcvf 名称.tar.gz 文件夹 解压:tar -zxvf 包名.tar.gz 解压路径
- PopUpManager弹出窗口
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- asp之vbscript函数
'函数Abs(number)'返回绝对值.Array(arglist)'创建一个数组.Asc(string)'返回字符串第一个字符的ANSI码.Atn(number)'返回反正弦值.CBool(exp ...
- wpf之Popup弹出自定义输入"键盘"
在很多工厂的信息化MES系统中,车间的采集数据的机器是触摸屏电脑(工厂环境所限,用外接鼠标键盘反而不方便). 由于没有外接键盘,所以用户无法像坐在办公室一样,用鼠标键盘进行录入信息. 这时我们可以用w ...
- 【dp 背包变形】 poj 1837
#include <cstdio> #include <memory.h> #include<iostream> using namespace std; ][]; ...