Pebbles

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

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

题意,放贝壳,要求四周不能有相临的贝壳,放的位置上有价值,求和理的方法中,价值最大的。

思路:状压dp,先筛选出(0,1<<15)内符合要求的状态,也就是考虑列上两个相邻的不能放的去掉,最终打表记录,可以发现符合要求的不过6000个不到。

然后dp[i][j],表示第i行第j种状态的最大价值。每一行的状态只和上一行的状态有关。转移方程dp[i][j]=max(dp[i][j],dp[i-1][x]+fu);

复杂度为n*n*b;(n为符合的状态,b为层数);

  1 #include<stdio.h>
2 #include<algorithm>
3 #include<string.h>
4 #include<iostream>
5 #include<stdlib.h>
6 #include<queue>
7 #include<stack>
8 int check(int n);
9 int check2(int n,int m);
10 typedef long long ll;
11 char a[2000][2000];
12 int ma[20][20];
13 int dp[20][10000];
14 char b[2000];
15 int kp[20];
16 int mm[10000];
17 using namespace std;
18 int main(void)
19 {
20 int n,i,j,k,p,q,m;
21 int bb=0;
22 for(i=0; i<(1<<15); i++)
23 {
24 if(check(i))
25 {
26 mm[bb++]=i;
27 }
28 }
29 int kkk=0;
30
31 while(gets(b)!=NULL)
32 {
33 kkk++;
34 memset(dp,0,sizeof(dp));
35 int l=strlen(b);
36 int sum=0;
37 int ans=10;
38 int cnt=0;
39 for(i=0; i<=l; i++)
40 {
41 if(b[i]==' '||i==l)
42 {
43 kp[cnt++]=sum;
44 sum=0;
45 }
46 else
47 {
48 sum*=ans;
49 sum+=b[i]-'0';
50 }
51 }
52
53 for(i=0; i<cnt; i++)
54 {
55 ma[0][i]=kp[i];
56 }
57 sum=0;
58 for(i=1; i<cnt; i++)
59 {
60 gets(b);
61 int y=strlen(b);
62 int vk=0;
63 for(j=0; j<=y; j++)
64 {
65 if(b[j]==' '||j==y)
66 {
67 ma[i][vk++]=sum;
68 sum=0;
69 }
70 else
71 {
72 sum*=10;
73 sum+=b[j]-'0';
74 }
75 }
76
77 }
78 for(i=0; i<bb; i++)
79 {
80 if(mm[i]>(1<<cnt))
81 {
82 break;
83 }
84 }
85 int uu=i;
86 for(i=0; i<uu; i++)
87 {
88 int zz=0;
89 for(j=0; j<cnt; j++)
90 {
91 if((1<<j)&mm[i])
92 {
93 zz+=ma[0][j];
94 }
95 }
96 dp[0][i]=zz;
97 }
98 int x;
99 for(i=1; i<cnt; i++)
100 {
101 for(j=0; j<uu; j++)
102 {
103 int fu=0;
104 for(int y=0; y<cnt; y++)
105 {
106 if((1<<y)&mm[j])
107 {
108 fu+=ma[i][y];
109
110 }
111 }
112 for(x=0; x<uu; x++)
113 {
114 if(check2(mm[x],mm[j]))
115 {
116 dp[i][j]=max(dp[i][j],dp[i-1][x]+fu);
117
118 }
119 }
120
121 }
122
123 }
124 int maxx=0;
125 for(i=0; i<uu; i++)
126 {
127 maxx=max(maxx,dp[cnt-1][i]);
128 }
129 printf("%d\n",maxx);
130 if(kkk)
131 {
132 getchar();
133 }
134 }
135 return 0;
136 }
137
138 int check(int n)
139 {
140 if((n>>1)&(n))
141 {
142 return 0;
143 }
144 else
145 {
146 return 1;
147 }
148 }
149 int check2(int n,int m)
150 {
151 int ss=(n>>1);
152 int vv=n<<1;
153 if(m&ss||vv&m||n&m)
154 {
155 return 0;
156 }
157 return 1;
158 }

状压DP

Pebbles(hdu2167)的更多相关文章

  1. HDU2167 Pebbles(状压DP)

    题目给一张n×n的格子,每个格子都有数字,要从格子中取若干个数字,八个方向相邻的数字不能一起取,问取的数字最大和是多少. 从第一行一行一行看下去,可以发现第1行取哪几列只会影响到第2行,第3行后面的一 ...

  2. [hdu2167]Pebbles

    来自FallDream的博客,未经允许,请勿转载,谢谢. 给定一个方阵,你要取出一些数字,满足没有两个格子八联通相邻的前提下和最大,求这个和 n<=15 插头dp,保存轮廓线以及目前转移点左上方 ...

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

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

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

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

  5. Pebbles

    Pebbles Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

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

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

  7. B. Painting Pebbles

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

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

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

  9. cf509B Painting Pebbles

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

随机推荐

  1. C++你不知道的事

    class A { public: A() { cout<<"A's constructor"<<endl; } virtual ~A() { cout&l ...

  2. windows系统 svn自动更新

    如果对svn不熟悉,当svn上面有更新时,想看到实时效果,就得去web目录手动更新,比较麻烦 其它svn有一个自动更新的功能 利用 hook   在svn 仓库目录下面有一个hook目录 在post- ...

  3. 分布式服务治理框架Dubbo的前世今生及应用实战

    Dubbo的出现背景 Dubbo从开源到现在,已经出现了接近10年时间,在国内各大企业被广泛应用. 它到底有什么魔力值得大家去追捧呢?本篇文章给大家做一个详细的说明. 大规模服务化对于服务治理的要求 ...

  4. 从for循环到机器码

    def p(*x): print(x) p(type(range), dir(range)) r = range(2); i = iter(r) try: p(next(i)); p(next(i)) ...

  5. 【leetcode】563. Binary Tree Tilt

    Given the root of a binary tree, return the sum of every tree node's tilt. The tilt of a tree node i ...

  6. Shell学习(五)—— awk命令详解

    一.awk简介   awk是一个非常好用的数据处理工具,相对于sed常常作用于一整个行的处理,awk则比较倾向于一行当中分成数个[字段]处理,因此,awk相当适合处理小型的数据数据处理.awk是一种报 ...

  7. Gradle安装与配置

    一.Gradle安装 1.Gradle安装 (1)先安装JDK/JRE (2)Gradle下载官网 Gradle官网 (3)解压安装包到想安装到的目录.如D:\java\gradle-5.2.1 (4 ...

  8. 【编程思想】【设计模式】【其他模式】hsm

    Python版 https://github.com/faif/python-patterns/blob/master/other/hsm/hsm.py """ Impl ...

  9. SpringIOC原理

    IOC(DI):其实这个Spring架构核心的概念没有这么复杂,更不像有些书上描述的那样晦涩.java程序员都知道:java程序中的每个业务逻辑至少需要两个或以上的对象来协作完成,通常,每个对象在使用 ...

  10. 【Linux】【Basis】进程及作业管理

    进程及作业管理       内核的功用:进程管理.文件系统.网络功能.内存管理.驱动程序.安全功能       Process: 运行中的程序的一个副本:         存在生命周期       L ...