当前位置:网站首页>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];
}
边栏推荐
- Basic Parameters of RF Devices 2
- leetcode-1161:最大层内元素和
- Know what DTU is 4GDTU equipment
- JS逆向之浏览器补环境(一)
- pc端判断当前使用浏览器类型
- GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13
- TiCDC 架构和数据同步链路解析
- Meta元宇宙部门第二季度亏损28亿 仍要继续押注?元宇宙发展尚未看到出路
- 加密生活,Web3 项目合伙人的一天
- System design. Short chain system design
猜你喜欢
数字图像隐写术之卡方分布
MySQL (6)
第一学年课程期末考试
Overview of prometheus monitoring
Parameter introduction and selection points of wireless module
The sword refers to offer17---print the n digits from 1 to the largest
case语句的综合结果,你究竟会了吗?【Verilog高级教程】
Mysql: Invalid default value for TIMESTAMP
Meta元宇宙部门第二季度亏损28亿 仍要继续押注?元宇宙发展尚未看到出路
想要写出好的测试用例,先要学会测试设计
随机推荐
Gateway路由的配置方式
Programmer's debriefing report/summary
"Cloud native's master, master and vulgar skills" - 2022 National New College Entrance Examination Volume I Composition
tkinter模块高级操作(二)—— 界面切换效果、立体阴影字效果及gif动图的实现
勾股数元组 od js
GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13
蛮力法/邻接表 广度优先 有向带权图 无向带权图
Dispatch Center xxl-Job
GCC Rust获批将被纳入主线代码库,或将于GCC 13中与大家见面
使用PageHelper实现分页查询(详细)
leetcode-952: Calculate max component size by common factor
221. 最大正方形
简易表白小页面
Word/Excel 固定表格大小,填写内容时,表格不随单元格内容变化
Charging effect simulation
【genius_platform软件平台开发】第七十四讲:window环境下的静态库和动态库的一些使用方法(VC环境)
rpm install postgresql12
uniapp使用第三方字体
Centos 7.9 install PostgreSQL14.4 steps
16、注册中心-consul