来自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. django搭建web (五) views.py

    http请求: HttpRequest http响应: HttpResponse 所在位置:django.http isinstance(request,HttpResponse) True-> ...

  2. 200行Python代码实现2048

    200行Python代码实现2048 一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环境,实验中会用到桌面 ...

  3. 201421123042 《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 继承中的关键词:Soper,object,override,project, 1.2 尝试使用思维导图将这些关键词组织起来.注: ...

  4. volt问题

    1./表示当前目录:/college/detail/{{ item.sid }}表示这个路径超链接,url实在不好写就不写,作为开发人员想怎么弄就怎么弄最后发布是项目主管的事 2.不需要服务器给值,直 ...

  5. 用‘+=’拼接字符串,打印时总会出现一个undefined

    var str; for(var i = 0; i < 5; i++){ str += String(i); } console.log(str); 他喵的,打印的结果竟然是"unde ...

  6. spring-oauth-server实践:授权方式四:client_credentials 模式下有效期内重复申请 access_token ?

    spring-oauth-server入门(1-12)授权方式四:client_credentials 模式下有效期内重复申请 access_token ? 一.失效重建邏輯 二.如果沒有失效,不会重 ...

  7. Spring Security 入门(1-8)缓存EhCache

  8. Asp.NET Core2.0 项目实战入门视频课程_完整版

    END OR START? 看到这个标题,你开不开心,激不激动呢? 没错,.net core的入门课程已经完毕了.52ABP.School项目从11月19日,第一章视频的试录制,到今天完整版出炉,离不 ...

  9. java将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

    首先我们的算法是:例如 输入的是 90 1.找到90的最小公约数(1除外)是 2 2.然后把公约数 2 输出 3.接着用 90 / 2 = 45 (如果这里是素数,就结束,否则继续找最小公约数) 4. ...

  10. JavaScript简单重写构造器的原型

    //简单重写原型对象: //一个构造函数Person function Person(){ } //重写Person的原型 //把Person的原型赋值给一个新的对象 是我们重写的过程 Person. ...