Problem Description
One day, Resty comes to an incredible world to seek Eve -- The origin of life. Lilith, the sister of Eve, comes with him. Although Resty wants to find Eve as soon as possible, Lilith likes to play games so much that you can't make her make any move if you don't play with her.

Now they
comes to the magical world and Lilish ask Resty to play with her.

The
game is following :
Now the world is divided into a m * n grids by Lilith,
and Lilith gives each grid a score.
So we can use a matrix to describe
it.
You should come from cell(0, 0) to cell(m-1, n-1) (Up-Left to Down-Right)
and try to colloct as more score as possible.
According to Lilish's rule, you
can't arrive at each cell more than once.

Resty knows that Lilish will be
easy to find the max score, and he doesn't want to lose the game.
So he want
to find the game plan to reach the max score.

Your task is to calculate
the max score that Lilish will find, the map is so small so it shouldn't be
difficult for you, right?

 
Input
The input consists of more than one
testdata.
Process to the END OF DATA.
For each test data :
the first
live give m and n. (1<=m<=8, 1<=n<=9)
following m lines, each
contain n number to give you the m*n matrix.
each number in the matrix is
between -2000 and 2000
 
Output
Output Format is "Case ID: ANS" one line for each
data
Don't print any empty line to the output
 
Sample Input
2 2
1 2
3 1
3 3
0 -20 100
1 -20 -20
1 1 1
 
Sample Output
Case 1: 5
Case 2: 61
 
插头dp
左上角走到右下角,不得重复经过格子,也可以不经过,求最大分数。
两种方法,第一是上下加两行,加两列,加障碍,使题目变成简单的回路,而不是单路。
第二在起点终点都加一个单插头处理,其他地方也应相应微调,不过我好像调得不好,一直WA……233,不过本机测了数百组数据都是对哒,所以不管了……
/*第一种方法*/
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mn=;
int i;
struct na{
int x,z;
na(int xx,int zz):x(xx),z(zz){}
};
int n,m,x,y,z,a[],k,en,u=,p1,p2;
bool map[][];
int f[][mn+],ans;
int v[][mn+];
int re[][];
queue <na> q;
inline int gx(int x,int q1,int q2){k=;for (register int i=m+;i;i--) k=k*+(i==x?q1:(i==x+?q2:a[i]));return k;}
inline void up(int x,int z,int lj,bool la){
if (la) lj+=re[x/m+][x%m+];
x++;
k=x%;
if (v[k][z]!=x) v[k][z]=x,f[k][z]=-1e9,q.push(na(x,z));
f[k][z]=max(f[k][z],lj);
}
int main(){
//freopen("a.in","r",stdin);
register int i,j;
while(scanf("%d%d",&n,&m)!=EOF){
u++;
printf("Case %d: ",u);
ans=-1e9;
memset(map,,sizeof(map));memset(v,,sizeof(v));memset(re,,sizeof(re));memset(f,,sizeof(f));
for (i=;i<=m;i++)
for (j=;j<=n;j++)
map[i][j+]=;
for (i=;i<=n;i++)
for (j=;j<=m;j++)
scanf("%d",&re[i+][j]);
n+=;
m+=;
en=n*m-;
for (i=;i<=m;i++) map[i][]=map[i][n]=;
for (i=;i<=n;i++) map[m][i]=;
map[][]=;map[m-][n-]=;
if (n==&&m==){
printf("%d\n",re[][]);
continue;
}
f[][]=;v[][]=;
q.push(na(,));
while(!q.empty()){
na no=q.front();q.pop();
int an=f[no.x%][no.z];
if(no.x%m==) no.z*=;
x=no.x%m+;y=no.x/m+;
for (i=;i<=m+;i++) a[i]=;
for (i=,j=no.z;j;i++,j/=) a[i]=j%;
if (!map[x][y])up(no.x,gx(x,,),an,);else
if (a[x]==&&a[x+]==){
if (no.x==en) ans=max(ans,an);
}else if (a[x]==&&a[x+]==) up(no.x,gx(x,,),an,);else
if (a[x]==&&a[x+]==){
if (no.x!=en&&no.x!=) up(no.x,gx(x,,),an,);
if (map[x][y+]&&map[x+][y]) up(no.x,gx(x,,),an,);
}else if (a[x]==){
if (map[x+][y]) up(no.x,gx(x,,a[x+]),an,);
if (map[x][y+]) up(no.x,gx(x,a[x+],),an,);
}else if (a[x+]==){
if (map[x+][y]) up(no.x,gx(x,,a[x]),an,);
if (map[x][y+]) up(no.x,gx(x,a[x],),an,);
}else if (a[x]==a[x+]){
p1=p2=;
if (a[x]==)
for (j=,i=x+;i<=m;i++){
if (a[i]==) j--;
if (a[i]==) j++;
if (j>&&!p1) p1=i,j--;
if (j>&&p1){p2=i;break;}
}else
for (j=,i=x-;i;i--){
if (a[i]==) j++;
if (a[i]==) j--;
if (j>&&!p2) p2=i,j--;
if (j>&&p2){p1=i;break;}
}
a[p1]=;a[p2]=;up(no.x,gx(x,,),an,);
}
}
printf("%d\n",ans);
}
}
/*第二种方法*/
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int i;
struct na{
int x,z;
na(int xx,int zz):x(xx),z(zz){}
};
int n,m,x,y,z,a[],k,en,u=,p1,p2;
bool map[][];
int f[][],ans;
int v[][];
int re[][];
queue <na> q;
inline int gx(int x,int q1,int q2){k=;for (register int i=m+;i;i--) k=k*+(i==x?q1:(i==x+?q2:a[i]));return k;}
inline void up(int x,int z,int lj,bool la){
if (la) lj+=re[x/m+][x%m+];
x++;
k=x%;
if (v[k][z]!=x) v[k][z]=x,f[k][z]=lj,q.push(na(x,z));
if (lj>f[k][z]) f[k][z]=lj;
}
int main(){
register int i,j;
while(scanf("%d%d",&n,&m)!=EOF){
u++;
ans=;
memset(map,,sizeof(map));memset(v,,sizeof(v));memset(f,,sizeof(f));
en=n*m-;
for (i=;i<=m;i++)
for (j=;j<=n;j++)
map[i][j]=;
for (i=;i<=n;i++)
for (j=;j<=m;j++)
scanf("%d",&re[i][j]);
if (n==&&m==){
printf("Case %d: %d\n",u,re[][]);
continue;
}
f[][]=;
v[][]=;
q.push(na(,));
while(!q.empty()){
na no=q.front();q.pop();
int an=f[no.x%][no.z];
if (no.x%m==) no.z*=;
x=no.x%m+;y=no.x/m+;
for (i=;i<=m+;i++) a[i]=;
for (i=,j=no.z;j;i++,j/=) a[i]=j%;
if (no.x==en){
k=;
for (i=;i<=m+;i++) k+=a[i]!=;
if (k==&&(a[m]==||a[m+]==)&&an+re[n][m]>ans) ans=an+re[n][m];
continue;
}
if (a[x]==&&a[x+]==){
up(no.x,gx(x,,),an,);
}else if (a[x]==&&a[x+]==){
up(no.x,gx(x,,),an,);
if (map[x][y+]&&map[x+][y])
up(no.x,gx(x,,),an,);
}else if (a[x]==){
if (map[x+][y]) up(no.x,gx(x,,a[x+]),an,);
if (map[x][y+]) up(no.x,gx(x,a[x+],),an,);
}else if (a[x+]==){
if (map[x+][y]) up(no.x,gx(x,,a[x]),an,);
if (map[x][y+]) up(no.x,gx(x,a[x],),an,);
}else if (a[x]==&&a[x+]==){
p1=p2=;
for (j=,i=x+;i<=m+;i++){
if (a[i]==) j--;
if (a[i]==) j++;
if (j>&&!p1) p1=i,j--;
if (j>&&p1){p2=i;break;}
}
a[p1]=;a[p2]=;
up(no.x,gx(x,,),an,);
}else if (a[x]==&&a[x+]==){
p1=p2=;
for (j=,i=x-;i;i--){
if (a[i]==) j++;
if (a[i]==) j--;
if (j>&&!p2) p2=i,j--;
if (j>&&p2){p1=i;break;}
}
a[p1]=;a[p2]=;up(no.x,gx(x,,),an,);
}
}
printf("Case %d: %d\n",u,ans);
}
}

HDU 3377 Plan的更多相关文章

  1. HDU 3377 Plan (插头DP,变形)

    题意:有一个n*m的矩阵,每个格子中有一个值(可能负值),要从左上角走到右下角,求路径的最大花费. 思路: 除了起点和终点外,其他的点可以走,也可以不走. (2)我用的是括号表示法,所以起始状态为') ...

  2. HDU 3377 插头dp

    题目大意: 从左上角走到右下角,每个点之多经过一次,取到所有路径上经过点的权值,求最大的权值之和,这里走到右下角就算停止了 这里有个思路是转化成熟悉的回路问题 在上方和右方最外围定义一圈权值为0 , ...

  3. 插头DP专题

    建议入门的人先看cd琦的<基于连通性状态压缩的动态规划问题>.事半功倍. 插头DP其实是比较久以前听说的一个东西,当初是水了几道水题,最近打算温习一下,顺便看下能否入门之类. 插头DP建议 ...

  4. 插头dp练习

    最近学了插头dp,准备陆续更新插头dp类练习. 学习论文还是cdq那篇<基于连通性状态压缩的动态规划问题>. 基本的想法都讲得很通透了,接下来就靠自己yy了. 还有感谢kuangbin大大 ...

  5. HDU 3080 The plan of city rebuild(prim和kruskal)

    The plan of city rebuild Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  6. HDU 3757 Evacuation Plan DP

    跟 UVa 1474 - Evacuation Plan 一个题,但是在杭电上能交过,在UVa上交不过……不知道哪里有问题…… 将施工队位置和避难所位置排序. dp[i][j] 代表前 i 个避难所收 ...

  7. hdu 5540 Secrete Master Plan(水)

    Problem Description Master Mind KongMing gave Fei Zhang a secrete master plan stashed × matrix, but ...

  8. hdu 5290 Bombing plan

    http://acm.hdu.edu.cn/showproblem.php?pid=5290 题意: 一棵树,每个点有一个权值wi,选择点i即可破坏所有距离点i<=wi的点,问破坏所有点 最少需 ...

  9. 【HDOJ】【3377】Plan

    插头DP sigh……其实思路很简单的= =就多加一种转移:从(0,0)->(0,0),也就是不走这个格子…… 初始状态就是第一格有一个左插头= =结束状态可以让(n,m)这个位置可以走到(n+ ...

随机推荐

  1. sqlserver 存储过程 删除

    --删除(delete from) CREATE PROCEDURE [dbo].[DeleteMessage] @strtable varchar(),--要删除信息的表名 @strwhere va ...

  2. Python 集体智慧编程PDF

    集体智慧编程PDF 1.图书思维导图http://www.pythoner.com/183.html p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12. ...

  3. cocoapods安装说明,最快安装,以及使用

    安装卸载更新新推荐 文章最后 其他问题总结: 1 添加taobao提供的镜像地址:http://ruby.taobao.org/ 移除命令:gem sources --remove https://r ...

  4. Qt之移动硬盘热插拔监控

    最近在做一个通用对话框,类似于windows的资源管理器,当然了没有windwos资源管理器那么强大.用户报了一个bug,说通用对话框打开之后不能实时监控U盘插入,随手在百度上搜索了一圈,这个问题还是 ...

  5. 【ASP.NET系列】详解Views

    描述 本片文章内容属于ASP.NET MVC系列视图篇,主要讲解View,大致内容如下: 1.Views文件夹讲解 2.View种类 3.Razor语法 4.对视图的基本操作 一   Views文件夹 ...

  6. Linux发行版 CentOS6.5下的分区操作

    本文地址http://comexchan.cnblogs.com/ ,尊重知识产权,转载请注明出处,谢谢! 查询磁盘信息并作分区规划 执行下述命令查询磁盘信息: fdisk -l 可知.数据盘大小50 ...

  7. PHP array_map()

    PHP array_map() 函数 将函数作用到数组中的每个值上,每个值都乘以本身,并返回带有新值的数组: <?php function myfunction($v) { return($v* ...

  8. 访问vm中centos的web站点

    vm网络连接设置成NAT 需要把centos设置成静态IP 再不行,记得把centos的防火墙先关闭

  9. 程序猿的日常——HashMap的相关知识

    背景知识 哈希冲突 哈希是指通过某种方法把数据转变成特定的数值,数值根据mod对应到不同的单元上.比如在Java中,字符串就是通过每个字符的编码来计算.数字是本身对应的值等等,不过就算是再好的哈希方法 ...

  10. 中文代码示例之Vuejs入门教程(一)

    原址: https://zhuanlan.zhihu.com/p/30917346 为了检验中文命名在主流框架中的支持程度, 在vuejs官方入门教程第一部分的示例代码中尽量使用了中文命名. 所有演示 ...