HDU2167(SummerTrainingDay02-D 状态压缩dp)
Pebbles
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1820 Accepted Submission(s): 1034
Problem Description

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
Output
Sample Input
85 50 74 94 28
92 96 23 71 10
23 61 31 30 46
64 33 32 95 89
78 78 11 55 20 11
98 54 81 43 39 97
12 15 79 99 58 10
13 79 83 65 34 17
85 59 61 12 58 97
40 63 97 85 66 90
33 49 78 79 30 16 34 88 54 39 26
80 21 32 71 89 63 39 52 90 14 89
49 66 33 19 45 61 31 29 84 98 58
36 53 35 33 88 90 19 23 76 23 76
77 27 25 42 70 36 35 91 17 79 43
33 85 33 59 47 46 63 75 98 96 55
75 88 10 57 85 71 34 10 59 84 45
29 34 43 46 75 28 47 63 48 16 19
62 57 91 85 89 70 80 30 19 38 14
61 35 36 20 38 18 89 64 63 88 83
45 46 89 53 83 59 48 45 87 98 21
15 95 24 35 79 35 55 66 91 95 86 87
94 15 84 42 88 83 64 50 22 99 13 32
85 12 43 39 41 23 35 97 54 98 18 85
84 61 77 96 49 38 75 95 16 71 22 14
18 72 97 94 43 18 59 78 33 80 68 59
26 94 78 87 78 92 59 83 26 88 91 91
34 84 53 98 83 49 60 11 55 17 51 75
29 80 14 79 15 18 94 39 69 24 93 41
66 64 88 82 21 56 16 41 57 74 51 79
49 15 59 21 37 27 78 41 38 82 19 62
54 91 47 29 38 67 52 92 81 99 11 27
31 62 32 97 42 93 43 79 88 44 54 48
Sample Output
683
2096
2755
Source
//2017-08-02
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; int arr[][];
int dp[][], sum[][];//dp[i][j]表示第i行使用第j种方法所能得到的最大值,sum[i][j]表示第i行使用第j种方法所得的和
int state[];//表示可行的状态,即可行的取数方法
int len, n; bool ok(int sta)//可行状态,即1的位置两两不相邻
{
return (sta&(sta<<))== ? true : false;
} int get_sum(int pos, int x)//求第pos行,使用x方法能取得的和
{
int sum = , cnt = ;
while(x)
{
sum += (x%)*arr[pos][n-cnt];
x >>= ;
cnt++;
}
return sum;
} void init(int m)//初始化
{
len = ;
for(int i = ; i < (<<m); i++)
if(ok(i))state[len++] = i;
for(int i = ; i < n; i++)
for(int j = ; j < len; j++)
sum[i][j] = get_sum(i, state[j]);
memset(dp, , sizeof(dp));
for(int i = ; i < len; i++)
dp[][i] = sum[][i];
} void read(){
char str[];
int row = ;
while(cin.getline(str, )){
if(strlen(str) == )break;
for(int i = ; i < strlen(str); i+=){
arr[row][i/] = (str[i]-'')*+(str[i+]-'');
}
row++;
}
n = row;
} int main()
{
while()
{
read();
if(n == )break;
init(n);
for(int i = ; i < n; i++)//处理第i行
for(int j = ; j < len; j++)//采取第j种方法
for(int k = ; k < len; k++)//枚举上一行所采取的方法k
if((state[j]&state[k])== && ((state[j]<<)&state[k])== && ((state[j]>>)&state[k])==)//方法j、k可行。
dp[i][j] = max(dp[i][j], dp[i-][k]+sum[i][j]);//状态转移方程 int ans = ;
for(int i = ; i < len; i++)//找出最大值
if(dp[n-][i]>ans)
ans = dp[n-][i]; cout<<ans<<endl;
}
return ;
}
HDU2167(SummerTrainingDay02-D 状态压缩dp)的更多相关文章
- 状态压缩dp(hdu2167,poj2411)
hdu2167 http://acm.hdu.edu.cn/showproblem.php?pid=2167 给定一个N*N的板子,里面有N*N个数字,选中一些数字,使得和最大 要求任意两个选中的数字 ...
- HDU2167+状态压缩DP
状态压缩dp 详见代码 /* 状态压缩dp dp[ i ][ j ]:第i行j状态的最大和 dp[i][j] = max( dp[i-1][k]+sum[i][j] ); 题意:给定一个N*N的方格, ...
- HDU1565+状态压缩dp
简单的压缩状态 dp /* 状态压缩dp 同hdu2167 利用滚动数组!! */ #include<stdio.h> #include<string.h> #include& ...
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- [知识点]状态压缩DP
// 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...
- HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP
题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- 状态压缩dp问题
问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...
- BZOJ-1226 学校食堂Dining 状态压缩DP
1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...
随机推荐
- 【五校联考3day2】C
題意: 現有一平面直角坐標系,有n個點,每一個點必須向某一個方向發射射線,且任意一條射線必須與某一條坐標軸平行.定義一種發射射線的方案是合法的,則方案必須滿足: 1.沒有一條射線交叉 2.沒有一條射線 ...
- Python中Flask框架SQLALCHEMY_ECHO设置
在用配置类的方式给app设置配置时, SQLALCHEMY_ECHO 这个是记录打印SQL语句用于调试的, 一般设置为False, 不然会在控制台输出一大堆的东西 /home/python/.virt ...
- Selenium3 + Python3自动化测试系列四——鼠标事件和键盘事件
一.鼠标事件 在 WebDriver 中, 将这些关于鼠标操作的方法封装在 ActionChains 类提供. ActionChains 类提供了鼠标操作的常用方法. ActionChains 类的成 ...
- makemigrations migrate
教程 如何重置迁移 (图片:https://www.pexels.com/photo/sky-flying-animals-birds-1209/) Django迁移系统的开发和优化使其能够进行大量迁 ...
- LSP劫持症状及解决方案
[症状] 1.网络连接正常,win7诊断显示无问题,但打开网页很迅速的显示该页无法显示,好像浏览器并没有提交任何url就做出了反应一样,输入其他网址,有时候也出现等候很久最终也是无法上网,firefo ...
- WPF 中textBox实现只输入数字
刚学到 通过本方法可以使文本框只能输入或复制入数字 对于数量类输入文本框比较有用 金额类只需小改动也可实现 以TextBox txtCount为例 添加TextChanged事件 代码如下 priv ...
- 转:Intellij idea Version Control File Status Colors ( 版本控制文件状态颜色 )
https://blog.csdn.net/Bruce_Lee__/article/details/80261308 Added —— 添加 Added in not active changelis ...
- nginx反向代理如何获取真实IP?
由于客户端和web服务器之间增加了中间层,因此web服务器无法直接拿到客户端的ip,通过$remote_addr变量拿到的将是反向代理服务器的ip地址. 1.安装--with-http_realip_ ...
- maven install 打包 报错 Cannot run program "gpg.exe": CreateProcess error
打包报错, mvn install后加上参数-Dgpg.skip,例如:mvn install -Dgpg.skip 即可解决. 我们也可以去掉 这个 插件 <plugin> ...
- centos7 安装配置postgresql
考:https://www.linuxidc.com/Linux/2017-10/147536.htm http://blog.51cto.com/12482328/2090844 https://w ...