题目链接: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. hdu_1429_胜利大逃亡(续)(BFS状压)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 题意:迷宫的加强版,迷宫里有钥匙和门,问在指定的时间下能否逃出 题解:用二进制位来记录是否有该门 ...

  2. petstore-jdbc

    拖了好多天,终于决定开始写作业了,搞了大半天的把软件安好. jdk(安装与环境配置) Tomcat(安装与环境配置) mysql(安装,同时配置图形化操作界面) eclipse for Javaee ...

  3. H5之重力感应篇

    手机的重力感应支持里,有两个主要的事件: 1. OrientationChange (在屏幕发生翻转的时候触发) 2. DeviceOrientation+DeviceMotion(重力感应与陀螺仪) ...

  4. Apache的最小配置

    以下配置是Apache的最小配置,路径为/usr/local/apache2/conf/include/lww.xhprof.conf <VirtualHost *:> ServerNam ...

  5. spring 基于XML和注解的两种事务配置方式

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  6. to_date()与to_char()

    1.以时间(Date类型)为查询条件时,可以用to_date函数实现: select t.* from D101 t where t.d101_40 = to_date('2013/9/12', 'y ...

  7. ProgressDialog

    几个方法介绍: 1.setMax() 设置对话框中进度条的最大值. 2.setTile() 设置标题. 3.setProgressStyl() 设置对话框中进度条的样式.例如:环形和水平. 参数: P ...

  8. 转载,find.sh

    #!/bin/bash #find files contains a keyword #write by xiaojing.zhao #2012.12.14 echo -e "\nThis ...

  9. hover带有动画效果的导航

    html,body{overflow-x:hidden;} ul,li{list-style: none;} .nav{width:100%; height: 26px; overflow: hidd ...

  10. HDU 2018 undefined

    题目思路:完全背包,dp[i][j]代表,砍j只怪,用i点疲劳最多能获得的经验值. 和平常的完全背包不一样的是多了一个限制条件:最多只砍S只怪,所以我们应该多一重循环来q:for 1->S,代表 ...