当前位置:网站首页>数字三角形模型 AcWing 1027. 方格取数
数字三角形模型 AcWing 1027. 方格取数
2022-07-07 06:11:00 【T_Y_F666】
数字三角形模型 AcWing 1027. 方格取数
原题链接
算法标签
DP 线性DP
思路
错误思路:分两次走
由于第一次走时可能存在多条路径都是第一次的解集,分开走只能选择其中的一条。但第一次走过的地方会被重置成0,无法确定是第一次同样是最优解而未被你选择的情况下第二次的值会不会比你已经得出的答案要大





代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>=b;--i)
using namespace std;
const int N = 105;
int f[2*N][N][N], a[N][N];
int aa, b, c, d;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read();
while(aa=read(), b=read(), c=read(), aa||b||c){
a[aa][b]=c;
}
// 行列值之和 由行确定列
rep(k, 2, n+n+1){
// 第一次第i行
rep(i1, 1, n+1){
// 第二次第i行
rep(i2, 1, n+1){
// j1表示第一次第j列 j2表示第二次第j列
int j1=k-i1, j2=k-i2;
if(j1>=1&&j1<=n&&j2>=1&&j2<=n){
int t=a[i1][j1];
// 非同一条路径
if(i1-i2){
t+=a[i2][j2];
}
// 下下 下右 右下 右右
f[k][i1][i2]=max({f[k][i1][i2], f[k-1][i1-1][i2-1]+t, f[k-1][i1-1][i2]+t, f[k-1][i1][i2-1]+t, f[k-1][i1][i2]+t});
}
}
}
}
printf("%lld", f[2*n][n][n]);
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量
- Laravel8 uses passport login and JWT (generate token)
- Tips for using jeditabletable
- IP guard helps energy enterprises improve terminal anti disclosure measures to protect the security of confidential information
- ES6_ Arrow function
- POJ - 3616 Milking Time(DP+LIS)
- Count sort (diagram)
- 2-3 lookup tree
- Greenplum 6.x build_ Environment configuration
- leetcode135. Distribute candy
猜你喜欢

Data type - integer (C language)

Count sort (diagram)

Image segmentation in opencv

Merge sort and non comparison sort

ncs成都新电面试经验

【踩坑】nacos注册一直连接localhost:8848,no available server

关于基于kangle和EP面板使用CDN

Implementation of navigation bar at the bottom of applet

如何在快应用中实现滑动操作组件

Data type - floating point (C language)
随机推荐
详解华为应用市场2022年逐步减少32位包体上架应用和策略
POJ - 3784 running medium
Golang compilation constraint / conditional compilation (/ / +build < tags>)
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
Compilation and linking of programs
[南京大学]-[软件分析]课程学习笔记(一)-introduction
【踩坑】nacos注册一直连接localhost:8848,no available server
idea里使用module项目的一个bug
更改当前文件夹及文件夹下文件日期shell脚本
如何在图片的目标中添加目标的mask
[Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
Frequently Asked Coding Problems
FPGA knowledge accumulation [6]
[Yu Yue education] C language programming reference of Zhongbei College of Nanjing Normal University
PLSQL的安装和配置
Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
Data analysis methodology and previous experience summary 2 [notes dry goods]
如何理解分布式架构和微服务架构呢
National SMS center number inquiry
SSM integration