Pebbles

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

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
2007 ACM-ICPC Pacific Northwest Region

Recommend
lcy

把每行取或不取的情况看成二进制数DP[i][u]表示到第i行为止且第i行以第u种状态能取到的最大值.
状态转移方程:DP[i][u]=Max(DP[i-1][v])+sum(i,u).

#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
int N,S=0;
int s[20],state[1600],d[20][20];
vector<int> G[1600];
int DP[20][1600];
bool legal(int x)
{
int i;
for (i=1;i<15;i++)
if ((x & s[i])&&(x & s[i-1])) return false;
return true;
}
void getstate()
{
int i;
for (i=0;i<=15;i++) s[i]=1<<i;
for (i=0;i<(1<<15);i++)
if (legal(i))
{
S++;
state[S]=i;
}
}
bool link(int u,int v)
{
int i;
if ((u & s[0])&&((v & s[0])||(v & s[1]))) return false;
if ((u & s[14])&&((v & s[14])||(v & s[13]))) return false;
for (i=1;i<14;i++)
if ((u & s[i])&&((v & s[i-1])||(v & s[i])||(v & s[i+1]))) return false;
return true;
}
void neibour()
{
int i,j;
for (i=1;i<=S;i++) G[i].clear();
for (i=1;i<=S;i++)
for (j=1;j<=S;j++)
if (link(state[i],state[j]))
G[i].push_back(j);
}
int sum(int l,int st)
{
int ans=0,i;
if (st>=s[N]) return 0;
for (i=0;i<N;i++)
if (st & s[i]) ans+=d[l][i];
return ans;
}
int main()
{
char str[200],ch;
getstate();
neibour();
while(gets(str))
{
int i,j,k;
N=(strlen(str)+1)/3;
for (i=0;i<N;i++)
{
char ch1=str[3*i]-'0',ch2=str[3*i+1]-'0';
d[0][i]=ch1*10+ch2;
}
for (i=1;i<N;i++)
for (j=0;j<N;j++)
scanf("%d",&d[i][j]);
ch=getchar();
ch=getchar();
memset(DP,0,sizeof(DP));
for (i=1;i<=S;i++)
{
if (state[i]>=s[N]) break;
DP[0][i]=sum(0,state[i]);
}
for (i=1;i<N;i++)
for (j=1;j<=S;j++)
{
if (state[j]>s[N]) break;
int tmp=sum(i,state[j]);
for (k=0;k<G[j].size();k++)
if (DP[i-1][G[j][k]]>DP[i][j]) DP[i][j]=DP[i-1][G[j][k]];
DP[i][j]+=tmp;
}
int Max=0;
for (i=1;i<=S;i++)
if (DP[N-1][i]>Max) Max=DP[N-1][i];
printf("%d\n",Max);
}
return 0;
}

Pebbles的更多相关文章

  1. codeforces 507B. Painting Pebbles 解题报告

    题目链接:http://codeforces.com/problemset/problem/509/B 题目意思:有 n 个piles,第 i 个 piles有 ai 个pebbles,用 k 种颜色 ...

  2. 贪心 Codeforces Round #289 (Div. 2, ACM ICPC Rules) B. Painting Pebbles

    题目传送门 /* 题意:有 n 个piles,第 i 个 piles有 ai 个pebbles,用 k 种颜色去填充所有存在的pebbles, 使得任意两个piles,用颜色c填充的pebbles数量 ...

  3. UVALive 7461 Separating Pebbles (计算几何)

    Separating Pebbles 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/H Description http://7 ...

  4. B. Painting Pebbles

    B. Painting Pebbles time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Bzoj 1982: [Spoj 2021]Moving Pebbles 博弈论

    1982: [Spoj 2021]Moving Pebbles Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 130  Solved: 88[Submi ...

  6. cf509B Painting Pebbles

    B. Painting Pebbles time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. HDU 2167 Pebbles(状压DP)

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

  8. Codeforces 789A Anastasia and pebbles(数学,思维题)

    A. Anastasia and pebbles time limit per test:1 second memory limit per test:256 megabytes input:stan ...

  9. 789A Anastasia and pebbles

    A. Anastasia and pebbles time limit per test 1 second memory limit per test 256 megabytes input stan ...

随机推荐

  1. Flash Player 19.0.0.124 Beta + IHTMLDocument3 IHTMLDocument2 ->get_innerHTML

    安装 Flash Player 19 之后 有 flash 动画的网页中 IHTMLDocument3 IHTMLDocument2 ->get_innerHTML 获取的 html 内容都是空 ...

  2. VS 高亮显示不带后缀的C++头文件

    工具-选项-文本编辑器-文件扩展名-勾选“将无扩展名文件映射到(M)” Microsoft Visual C++

  3. DOS下常用网络命令技巧

    DOS,即使对于许多自称了解计算机的人而言,也是一个比较陌生的词汇.然而,在网络管理过程中,DOS命令却是一个不可逾越的障碍,几乎所有的网络命令都运行在DOS界面.对初级用户而言,掌握一些常用网络命令 ...

  4. ctrl + z fg bg

    [root@bass ~]# jobs [1]+ Stopped vncviewer 192.168.1.17:5904 [root@bass ~]# #ctrl + z [root@bass ~]# ...

  5. 《Java程序性能优化》学习笔记 设计优化

    豆瓣读书:http://book.douban.com/subject/19969386/ 第一章 Java性能调优概述 1.性能的参考指标 执行时间: CPU时间: 内存分配: 磁盘吞吐量: 网络吞 ...

  6. CI中自定义SQL查询,LIKE模糊查询的处理

    参考: /** * 据用户输入的关键字查询相册信息;照片墙搜索框功能 * @param $keyWord 关键字 * @param $pageNum 页码,第几页 * @param $pageSize ...

  7. sql server 本地复制订阅 实现数据库服务器 读写分离

    再前段echosong 写了一遍关于mysql 数据同步实现业务读写分离的文章,今天咱们来看下SQL Server的复制订阅实现数据的读写分离 比起mysql的复制,SQL server 复制相对强大 ...

  8. Pyqt在QListWidget中添加右键菜单

    Pyqt 的资料奇少, 攻破难点之后, 就在这里记一下笔记. QListWidget 是继承 QWidget 的, 所以 QListWidget 是有右键菜单的, 从文档上可以找到 QWidget 上 ...

  9. C# 编程音量控制

    public FrmVoice() { InitializeComponent(); } [DllImport("user32.dll", EntryPoint = "S ...

  10. Java Hour 42 fastjson

    fastjson 神一样的存在,然后由于缺乏文档,很多功能完全不知道该怎么用. 42.1 字段的大小写问题 刚开始没想到会因为字段的大小写问题而导致反序列化json 失败. @Override pub ...