来自FallDream的博客,未经允许,请勿转载,谢谢。


给定一个方阵,你要取出一些数字,满足没有两个格子八联通相邻的前提下和最大,求这个和

n<=15

插头dp,保存轮廓线以及目前转移点左上方那个格子的状态,枚举格子转移即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#define MN 15
using namespace std;
inline int read()
{
int x = , f = ; char ch = getchar();
while(ch < '' || ch > ''){ if(ch == '-') f = -; ch = getchar();}
while(ch >= '' && ch <= ''){x = x * + ch - '';ch = getchar();}
return x * f;
} char st[];
int a[MN+][MN+],m,f[][<<]; int Strread()
{
int top=,j=;
for(int i=;st[i]==' '||(st[i]>=''&&st[i]<='');++i)
if(st[i]==' ') a[][++top]=j,j=;
else j=j*+st[i]-'';
return a[][++top]=j,top;
}
inline void R(int&x,int y){y>x?x=y:;}
int main()
{
while(cin.getline(st,))
{
m=Strread();if(m==&&a[][]==) continue;
for(int i=;i<=m;++i)
for(int j=;j<=m;++j)
a[i][j]=read();
memset(f,,sizeof(f));
int now=,pre=;
for(int i=;i<=m;++i)
for(int j=;j<=m;++j,swap(now,pre),memset(f[now],,sizeof(f[now])))
for(int k=;k<<<(m+);++k)
{
R(f[now][(k&((<<m)-(<<(j-))-))|((k&(<<(j-)))?(<<m):)],f[pre][k]);
if((j==||(!((k&(<<m)))&&!(k&(<<(j-)))))&&!(k&(<<(j-)))&&(j==m||!(k&(<<j))))
R(f[now][(k&((<<m)-))|(<<(j-))|((k&(<<(j-)))?(<<m):)],f[pre][k]+a[i][j]);
}
int ans=;
for(int i=;i<<<(m+);++i) ans=max(ans,f[pre][i]);
cout<<ans<<endl;
}
return ;
}

[hdu2167]Pebbles的更多相关文章

  1. HDU2167 Pebbles(状压DP)

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

  2. Pebbles(hdu2167)

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

  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. verilog学习笔记(1)_两个小module

    第一个小module-ex_module module ex_module( input wire sclk,//声明模块的时候input变量一定是wire变量 input wire rst_n,// ...

  2. Hibernate与mysql的对应类型

    Hibernate映射类型 Java类型 标准SQL类型  integer  java.lang.Integer  integer  long  java.lang.Long  bigint  sho ...

  3. ruby:TypeError: 对象不支持此属性或方法

    解决办法. 1.下载对应版本 下载node.js,根据ruby版本决定下载32还是x64,我的ruby版本x64 https://npm.taobao.org/mirrors/node/v8.9.3/ ...

  4. nyoj 矩形个数

    矩形的个数 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 在一个3*2的矩形中,可以找到6个1*1的矩形,4个2*1的矩形3个1*2的矩形,2个2*2的矩形,2个3 ...

  5. python 学习笔记

    1. 关于两者详细解释,参考链接:www.crifan.com/python_re_search_vs_re_findall/ 代码图

  6. Java服务器端生成报告文档:使用SQL Server Report Service(SSRS)

    SQL Server Report Service(SSRS)提供了Asp.Net和WinForm两类客户端组件封装,因此使用C#实现SSRS报表的导出功能,仅需要使用相应的组件即可. Java操作S ...

  7. 以太坊挖矿源码:clique算法

    上文我们总结了以太坊最主要的共识算法:ethash算法,本文将重点分析以太坊的另一个共识算法:clique. 关键字:clique,共识算法,puppeth,以太坊地址原理,区块校验,认证结点,POA ...

  8. IntelliJ IDEA开发Scala代码,与java集成,maven打包编译

    今天尝试了一下在IntelliJ IDEA里面写Scala代码,并且做到和Java代码相互调用,折腾了一下把过程记录下来. 首先需要给IntelliJ IDEA安装一下Scala的插件,在IDEA的启 ...

  9. CSS中容易混淆的伪元素类型和用法

    :first-of-type 匹配属于其父元素的第一个特定类型的子元素. 1.例子 <head> <meta charset="UTF-8"> <ti ...

  10. ZOJ-1655 Transport Goods---dijkstra变形&&最长路

    题目链接: https://vjudge.net/problem/ZOJ-1655 题目大意: 有N-1个城市给首都(第N个城市)支援物资,有M条路,走每条路要耗费一定百分比的物资.问给定N-1个城市 ...