Pebbles

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 795    Accepted Submission(s): 439

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):

 
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
 
Source
 
题目:要求选择了一个点后,其周围8个点都不能取。
        总是弄错,然后写了3遍,发现一开始写的是对的。......
 
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std; int a[][];
int state[],len;//
int Num[][];
int befor[];
int now[];
void Init()
{
int i,k=<<;
len=;
for(i=;i<k;i++)
{
if( (i&(i>>)) || (i&(i<<)) );
else
state[len++]=i;
}
}
void make_init(int n)
{
int i,j,k=<<n,ans,x;
memset(Num,,sizeof(Num));
for(i=;i<=n;i++)
{
for(j=;j<len&&state[j]<k;j++)
{
x=state[j];
ans=;
while(x)
{
if(x&)
{
Num[i][j]+=a[i][ans];
}
x=x>>;
ans++;
}
}
}
}
void solve(int n)
{
int i,j,k=<<n,s,hxl;
memset(befor,,sizeof(befor));
memset(now,,sizeof(now));
for(i=;i<=n;i++)
{
for(j=;j<len&&state[j]<k;j++)
{
hxl=;
for(s=;s<len&&state[s]<k;s++)
{
if( (state[j]&state[s]) )continue;
if( ((state[j]<<)&state[s]) || ((state[j]>>)&state[s]) ) continue;
if(hxl<befor[s])
hxl=befor[s];
}
now[j]=hxl+Num[i][j];
}
for(j=;j<len&&state[j]<k;j++)
{
befor[j]=now[j];
now[j]=;
}
}
for(i=,hxl=;i<len&&state[i]<k;i++)
if(hxl<befor[i])
hxl=befor[i];
printf("%d\n",hxl);
}
int main()
{
int n;
int i,j,tom,k;
char c[];
Init();
while(gets(c+)>)
{
k=strlen(c+);
if(k==)continue;
for(tom=,i=,n=;i<=k;i++)
{
if(c[i]>=''&&c[i]<='')
{
tom=tom*+c[i]-'';
}
else
{
a[][++n]=tom;
tom=;
}
}
a[][++n]=tom; for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&a[i][j]); make_init(n);
solve(n);
}
return ;
}

HDU 2167 Pebbles 状态压缩dp的更多相关文章

  1. HDOJ 2167 Pebbles (状态压缩dp)

    题意:给你一个n*n的矩阵,让你从矩阵中选择一些数是的他们的和最大,规则是:相邻的两个数不能同时取,位置为(i,j)的数与(i+1,j),(i-1,j),(i,j+1),(i,j-1),(i+1,j+ ...

  2. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  3. HDU 3001(状态压缩dp)

    状态压缩dp的第一题! 题意:Mr ACMer想要进行一次旅行,他决定访问n座城市.Mr ACMer 可以从任意城市出发,必须访问所有的城市至少一次,并且任何一个城市访问的次数不能超过2次.n座城市间 ...

  4. hdu 4856 Tunnels 状态压缩dp

    Tunnels Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  5. HDU 3001【状态压缩DP】

    题意: 给n个点m条无向边. 要求每个点最多走两次,要访问所有的点给出要求路线中边的权值总和最小. 思路: 三进制状态压缩DP,0代表走了0次,1,2类推. 第一次弄三进制状态压缩DP,感觉重点是对数 ...

  6. HDU 2167 Pebbles(状压DP)

    题目链接:Pebbles Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  7. hdu 5045 Contest(状态压缩DP)

    题解:我们使用一个二位数组dp[i][j]记录进行到第i个任务时,人组合为j时的最大和(这里的j我们用二进制的每位相应一个人). 详细见代码: #include <iostream> #i ...

  8. hdu 3091 Necklace 状态压缩dp *******

    Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)Total ...

  9. hdu 4628 Pieces 状态压缩dp

    Pieces Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

随机推荐

  1. ACM-ICPC 2018青岛网络赛-H题 Traveling on the Axis

    题目:略(不知道怎么从ZOJ搬题) 地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4054 把这题的每个点分成两种情况 ...

  2. 二,php的错误处理

    php处理错误的三种方式: 简单的die()语句: 自定义错误和错误触发器:错误日志: 1,简单的die()语句 if(!file_exists("aaa.txt")){ die( ...

  3. Oracle中对多行查询结果进行拼接

    to_char(wmsys.wm_concat(to_char( st.col_name))) as new_name to_char: 将当前值转换成字符串类型; wmsys.wm_concat:拼 ...

  4. <b>与<strong> <em>与<i>标签的区别

    <b>与 <strong>用在网页上都能使字体加粗,二者的不同是:<b>是物理元素 ;<strong>是逻辑元素. 物理元素强调的是一种物理行为.比如说 ...

  5. python爬虫urllib库使用

    urllib包括以下四个模块: 1.request:基本的HTTP请求模块,可以用来模拟发送请求.就像在浏览器里输入网址然后回车一样,只需要给库方法传入URL以及额外的参数,就可以模拟实现这个过程. ...

  6. ES6 (一)变量声明方法 & 解构赋值

    就是最新的JavaScript 原来的是var,要求不严格,不能限制修改,函数级 es6要求严格 1.防止重复声明 let      变量=var const 常量 2.控制修改 const常量不能修 ...

  7. [原创] Linux 中的 nohup 与 &

    目录 背景 结论放前面 & nohup nohup + & 测试 直接运行 单独使用 & 单独使用 nohup nohup + & 背景 一直没搞清楚 nohup 与 ...

  8. 编写高质量代码:Web前端开发修炼之道(三)

    第五章:高质量的Javascript 这章的内容我看的最久,这是跟我js基础没打好有着莫大的关系,但是还是耐着性子看完了, 不懂的东西都是百度上搜索,理解后再继续.下面是记录下来的笔记. 1)如何避免 ...

  9. c++ 用 0x3f3f3f3f 设定最大int值的优点

    在许多算法中都要用到一个常量来表示最大值,例如:寻找一个最小数,就要先设定一个值a,如果比a小,a就等于这个数:再如,最短路径中基本的松弛操作: 0 在c++中可以用memset() 来初始化数组成最 ...

  10. Struts2和SpringMVC的action是单例还是原型的?

    struts2的acion单独使用的时候应是多例的,也就是原型(prototype). 因为它是基于类开发的,它的三种获取页面传参的方式都是通过成员变量的方式来接受的. 如果用struts2框架基于方 ...