当前位置:网站首页>最大路径和
最大路径和
2022-07-31 01:38:00 【小卢要刷力扣题】
前言
链接:https://www.nowcoder.com/questionTerminal/8ecfe02124674e908b2aae65aad4efdf
来源:牛客网
给定一个矩阵matrix,先从左上角开始,每一步只能往右或者往下走,走到右下角。然后从右下角出发,每一步只能往上或者往左走,再回到左上角。任何一个位置的数字,只能获得一遍。返回最大路径和。
输入描述:
第一行输入两个整数M和N,M,N<=200
接下来M行,每行N个整数,表示矩阵中元素
输出描述:
输出一个整数,表示最大路径和
解题思路
两个人A, B都从左下角走到右下角,都只能向下或者向右走,但是A跟B能做出不同的选择
如果,某一时刻,AB进入相同的一 个格子,A和B只获得一份
A走到之后,就认为B就是回来的路径
A来到了a行b列, B来到了c行d列,如果它们跳进不同的格子里。
只获得一个的情况下,问你a跟b获得整体的最大。

如果某一个位置A也来过,B也来过,AB-定是同时来的,而不会分先后
因为AB同步走
可以省掉一个变量d,可以根据abc来推出d
代码
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
int[][] matrix = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
matrix[i][j] = sc.nextInt();
}
}
int ans = cherryPickup(matrix);
System.out.println(ans);
sc.close();
}
public static int cherryPickup(int[][] grid) {
int N = grid.length;
int M = grid[0].length;
int[][][] dp = new int[N][M][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
for (int k = 0; k < N; k++) {
dp[i][j][k] = Integer.MIN_VALUE;
}
}
}
int ans = process(grid, 0, 0, 0, dp);
return ans < 0 ? 0 : ans;
}
public static int process(int[][] grid, int x1, int y1, int x2, int[][][] dp) {
if (x1 == grid.length || y1 == grid[0].length || x2 == grid.length || x1 + y1 - x2 == grid[0].length) {
return Integer.MIN_VALUE;
}
if (dp[x1][y1][x2] != Integer.MIN_VALUE) {
return dp[x1][y1][x2];
}
if (x1 == grid.length - 1 && y1 == grid[0].length - 1) {
dp[x1][y1][x2] = grid[x1][y1];
return dp[x1][y1][x2];
}
int next = Integer.MIN_VALUE;
next = Math.max(next, process(grid, x1 + 1, y1, x2 + 1, dp));
next = Math.max(next, process(grid, x1 + 1, y1, x2, dp));
next = Math.max(next, process(grid, x1, y1 + 1, x2, dp));
next = Math.max(next, process(grid, x1, y1 + 1, x2 + 1, dp));
if (grid[x1][y1] == -1 || grid[x2][x1 + y1 - x2] == -1 || next == -1) {
dp[x1][y1][x2] = -1;
return dp[x1][y1][x2];
}
if (x1 == x2) {
dp[x1][y1][x2] = grid[x1][y1] + next;
return dp[x1][y1][x2];
}
dp[x1][y1][x2] = grid[x1][y1] + grid[x2][x1 + y1 - x2] + next;
return dp[x1][y1][x2];
}
边栏推荐
- Mysql: Invalid default value for TIMESTAMP
- 有没有可以做副业可以日入300元方法?
- Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
- uniapp使用第三方字体
- PDF split/merge
- Dispatch Center xxl-Job
- MySql installation and configuration super detailed tutorial and simple method of building database and table
- Jetpack Compose学习(8)——State及remeber
- Installation problem corresponding to tensorflow and GPU version
- Jiuzhou Cloud was selected into the "Trusted Cloud's Latest Evaluation System and the List of Enterprises Passing the Evaluation in 2022"
猜你喜欢

The level of ShardingSphere depots in actual combat (4)

uniapp使用第三方字体

想要写出好的测试用例,先要学会测试设计

C语言_结构体指针数组函数选票系统

Analyze the capabilities and scenarios of the cloud native message flow system Apache Pulsar

16、注册中心-consul

手把手教你配置Jenkins自动化邮件通知

九州云获评云计算标准化优秀成员单位

《云原生的本手、妙手和俗手》——2022全国新高考I卷作文

《实战》基于电商领域的词性提取及其决策树模型建模
随机推荐
prometheus 监控概述
【微信小程序】一文带你了解数据绑定、事件绑定以及事件传参、数据同步
《实战》基于电商领域的词性提取及其决策树模型建模
ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!
聚簇索引和非聚簇索引到底有什么区别
tkinter模块高级操作(二)—— 界面切换效果、立体阴影字效果及gif动图的实现
What is the ideal college life?
Jiuzhou Cloud was selected into the "Trusted Cloud's Latest Evaluation System and the List of Enterprises Passing the Evaluation in 2022"
pycharm重命名后无法运行(报错: can‘t open file......No such file or directory)
基于Keras_bert模型的Bert使用与字词预测
射频器件的基本参数2
观察者(observer)模式(一)
Know what DTU is 4GDTU equipment
Basic Parameters of RF Devices 1
SQLserver查询最近三个月的数据,语句该怎么写sqlserver
PDF split/merge
无线模块的参数介绍和选型要点
【Mysql】——索引的深度理解
勾股数元组 od js
Programmer's debriefing report/summary