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. [Flex] 组件Tree系列 —— 阻止用户点击选中Tree中分支节点

    mxml: <?xml version="1.0" encoding="utf-8"?> <!--功能描述:阻止用户点击选中Tree中分支节点 ...

  2. docker导入导出镜像

    docker容器导入导出有两种方法: 一种是使用save和load命令 使用例子如下: docker save ubuntu:load>/root/ubuntu.tar docker load& ...

  3. PL/SQL那点事-->修改Oracle数据库里面的字段长度

    在开发过程中,遇到有个问题:在Oracle数据库中,利用PL/SQL数据库开发工具来开发,某一字段的长度不能满足需求时候,采用下面的语法就行修改 alter table 表名 modify 字段名 长 ...

  4. python3入门之集合set

    之前介绍python的数据结构时,没有介绍set(集合)现在在这里稍微介绍下: set原理 Python 还 包 含 了 一 个 数 据 类 型-- set ( 集 合 ) . 集 合 是 一 个 无 ...

  5. thinkphp3.2----实现伪静态和路由配置

    URL模式: 0.普通   http://localhost/qixin/ThinkCMF(test)_backup/index.php?g=user&m=login&a=index ...

  6. Saiku2.6 Saiku315 链接SQL的JDBC字符串

    Saiku26 type=OLAP name=CloudConn driver=mondrian.olap4j.MondrianOlap4jDriver location=jdbc:mondrian: ...

  7. 洛谷 P5249 [LnOI2019]加特林轮盘赌 题解【概率期望】【DP】

    很有意思的题目. 题目背景 加特林轮盘赌是一个养生游戏. 题目描述 与俄罗斯轮盘赌等手枪的赌博不同的是,加特林轮盘赌的赌具是加特林. 加特林轮盘赌的规则很简单:在加特林的部分弹夹中填充子弹.游戏的参加 ...

  8. P4027 [NOI2007]货币兑换

    传送门 首先有一个显然的贪心,每次操作都要做到底,为了最优不会出现只卖一部分或者只买一部分的操作 所以设 $f[i]$ 表示前 $i$ 天得到的最大价值,那么对于每一个 $i$,枚举所有 $j< ...

  9. python 中mysql数据库的读写

    1.读取数据库 import pymysql id=[] name=[] explain=[] db=pymysql.Connection(host=,user="root", p ...

  10. 在vue中import()语法不能传入变量

    解决办法: 一定要用变量的时候,可以通过字符串模板来提供部分信息给webpack:例如import(`./path/${myFile}`), 这样编译时会编译所有./path下的模块,但运行时确定my ...