当前位置:网站首页>Path and the largest
Path and the largest
2022-07-31 01:45:00 【Xiao Lu wants to brush up on the question】
前言
链接:https://www.nowcoder.com/questionTerminal/8ecfe02124674e908b2aae65aad4efdf
来源:牛客网
给定一个矩阵matrix,先从左上角开始,每一步只能往右或者往下走,走到右下角.然后从右下角出发,每一步只能往上或者往左走,再回到左上角.任何一个位置的数字,只能获得一遍.返回最大路径和.
输入描述:
第一行输入两个整数M和N,M,N<=200
接下来M行,每行N个整数,represents the elements in the matrix
输出描述:
输出一个整数,表示最大路径和
解题思路
两个人A, B都从左下角走到右下角,都只能向下或者向右走,但是A跟B能做出不同的选择
如果,某一时刻,ABinto the same one 个格子,A和BOnly get one
Aafter walking,就认为BIt's the way back
A来到了a行b列, B来到了c行d列,If they jump into different grids.
In the case of only getting one,问你a跟bGet the overall maximum.

如果某一个位置A也来过,B也来过,AB-must come at the same time,而不会分先后
因为AB同步走
A variable can be omittedd,可以根据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];
}
边栏推荐
- JS逆向之浏览器补环境(一)
- 数字图像隐写术之JPEG 隐写分析
- leetcode-399:除法求值
- MySql的安装配置超详细教程与简单的建库建表方法
- Jiuzhou Cloud was selected into the "Trusted Cloud's Latest Evaluation System and the List of Enterprises Passing the Evaluation in 2022"
- Distributed. Idempotency
- How to expose Prometheus metrics in go programs
- 计算S=a+aa+…+aa…a
- Nacos
- 1782. Count the number of point pairs Double pointer
猜你喜欢

rpm安装postgresql12

MySQL (6)

GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13

数字图像隐写术之JPEG 隐写分析

GCC Rust获批将被纳入主线代码库,或将于GCC 13中与大家见面

leetcode-1161:最大层内元素和

数字图像隐写术之卡方分布

Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值

1782. 统计点对的数目 双指针

What have I experienced when I won the offer of BAT and TMD technical experts?
随机推荐
Multiplication, DFS order
MySQL的存储过程
黄东旭:TiDB的优势是什么?
Meta元宇宙部门第二季度亏损28亿 仍要继续押注?元宇宙发展尚未看到出路
TiKV主要内存结构和OOM排查总结
【Mysql】——索引的深度理解
35. Reverse linked list
12张图带你彻底搞懂服务限流、熔断、降级、雪崩
Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
leetcode-128:最长连续序列
聚簇索引和非聚簇索引到底有什么区别
成为比开发硬气的测试人,我都经历了什么?
Teach you how to configure Jenkins automated email notifications
Chi-square distribution of digital image steganography
简易表白小页面
1782. Count the number of point pairs Double pointer
16、注册中心-consul
Arbitrum Interview | L2 Summer, what does the standout Arbitrum bring to developers?
数字图像隐写术之卡方分布
计算S=a+aa+…+aa…a