【hdu2167】Pebbles
2016-08-24 16:39
274 查看
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. 题意:有一个N*N的格子(3≤N≤15),每个格子的元素的值在10~99之间,在从中取出若干个格子中的元素,使得所得到的值的和最大,但取出一个格子中的元素后,其周围的8个元素都不能取。
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) 每组数据为N行N列,但N没有给出(此题其中的一个难点),每一个元素之间有空格相隔。最后输入一个换行表示这组数据的结束(包括最后一组数据)。
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): 输出取得的元素的最大值,注意:每次输出后要加换行符。
Sample Input
71 24 95 56 54 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
572 683 2096 2755
解题思路
状态压缩 另外,输入要特殊处理。。
代码如下:
#include<cstdio> #include<sstream> #include<cstring> using namespace std; int f[16][(1<<15)+10]; int a[16][16],v[1<<15],vn,bit[20]; char s[110000]; int n; int mymax(int x,int y) { return x>y?x:y; } int main() { bit[1]=1; for(int i=2;i<=15;i++) bit[i]=(bit[i-1]<<1); while(scanf("%d",&a[1][1])!=EOF) { char c; c=getchar(); n=1; while(c!='\n') { n++; scanf("%d",&a[1] ); c=getchar(); } for(int i=2;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&a[i][j]); memset(f,0,sizeof(f)); int Maxx=(1<<n)-1; int vn=0; for(int x=0;x<=Maxx;x++) if(((x<<1)&x)==0) { for(int j=1;j<=n;j++) if((bit[j]&x)==bit[j]) f[1][x]+=a[1][j]; v[++vn]=x; } for(int i=2;i<=n;i++) for(int p=1;p<=vn;p++) { int tt=0; for(int j=1;j<=n;j++) if((bit[j]&v[p])) tt+=a[i][j]; for(int q=1;q<=vn;q++) if(((v[p]&v[q]))==0 && ((v[q]<<1)&v[p])==0 && ((v[q]>>1)&v[p])==0) f[i][v[p]]=mymax(f[i][v[p]],tt+f[i-1][v[q]]); } int ans=0; for(int p=1;p<=vn;p++) if(ans<f [v[p]]) ans=f [v[p]]; printf("%d\n",ans); } return 0; }
相关文章推荐
- Linux进程管理概述
- 内存管理
- 八大经典排序算法总结
- Visual studio 2013 community搭建 opencv2.4.10
- BZOJ3262: 陌上花开
- 【测试开发面试】阿里二面
- C++之string
- alias拦截器的使用
- [EmguCV|WinForm] 使用EmguCV內建直方圖工具繪製直方圖(Histogram)-直方圖(Histogram)系列 (1)
- Java学习片段
- 青春的我们
-  与 兼容性问题
- 有一个mainwindow.ui的界面类,有一个added.ui的界面类,如何将added.ui界面加入到另外一个界面呢?
- [置顶] 新浪云 上传图片
- 自定义view系列(3)--给自定义View添加点击事件
- JAVA序列化标准格式(XML、JSON)
- opengl es三角形例子
- 非常好用的List交集、并集
- Qt之模型/视图(自定义按钮)(重绘QStyleOptionButton)
- UVA11754 - Code Feat