首页 技术 正文
技术 2022年11月14日
0 收藏 352 点赞 5,068 浏览 1070 个字

第一次走用dfs枚举每种情况,第二次走用dp求剩下的最大值

设一个点集q用来保存有价值的点,排序,在最后加一个终点:x=m+1,y=m+1,v=0  //m是矩阵长宽

因为v=0的点是没有意义的,所以忽略它们,用q进行dfs

设当前点在q中的下标为p,已经积累的分数为score:

for(int i=p+;i<=n+;i++){
if(q[i].y>=q[p].y){//可以到达
a[q[i].x][q[i].y]=;
dfs(i,score+q[i].v);
a[q[i].x][q[i].y]=q[i].v;
}
}

当p=n+1时,到了终点(n是有价值点的个数,n+1为最后添加的终点)

在残缺的a中dp找到第二次的最大价值,更新ans即可

代码如下:

#include<iostream>
#include<algorithm>
using namespace std;struct Point{
int x,y;
int v;
}q[];
bool cnt(Point c,Point d){
return c.x<d.x || (c.x==d.x && c.y<d.y);
}
int n,m;//m是边长,n是有值的点的个数
int a[][];
int d[][];
int ans=; int dp(){
for(int i=;i<=m;i++){
for(int j=;j<=m;j++){
d[i][j]=max(d[i-][j],d[i][j-])+a[i][j];
}
}
return d[m][m];
}void dfs(int p,int score){
if(p==n+){
int c=dp();
if(ans<c+score)ans=c+score;
return;
}
for(int i=p+;i<=n+;i++){
if(q[i].y>=q[p].y){//可以到达
a[q[i].x][q[i].y]=;
dfs(i,score+q[i].v);
a[q[i].x][q[i].y]=q[i].v;
}
}
}int main(){
cin>>m;
for(n=;true;n++){
cin>>q[n].x>>q[n].y>>q[n].v;
if(q[n].x==){n--;break;}
a[q[n].x][q[n].y]=q[n].v;
}
sort(q+,q+n+,cnt);
q[n+].x=m+;q[n+].y=m+;q[n+].v=;//终点
dfs(,);
cout<<ans<<endl; return ;
}

如果你看着那个a和q有点重复不爽,可以试着直接在q上dp,这样就可以省去a了

ps:code上用operator为什么编译不过去!!求解!!

上一篇: if-return 语句
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898