Select the best path in a matrix
Amazon interview question:
Given a 2-dimensional array with arbitrary sizes and contains random positive values, you are required to move from the first element [0][0] to the last element [n][n] using the path which will yield the maximum sum of all the elements traversed. You can only move right and down; NOT left and up.
With brute force,this question can be solved by our thought but not computer,because time complexity is exponential.Actually it's a typical DP question,and we should try our best to keep track of something useful to save CPU while we are running in the matrix.I mean for each step we take,we should make sure that it's the optimized choice,which can be used to make choices later.So what does it mean by "later"?This is the point of every DP problem.Most of the time,when we figure out this core problem,we are just near the final solution.Check the code below.
- 1 /*******************************************
- 2 Author:Zhou You
- 3 Time:2014.09.07
- 4 Feature:finding the optimized path in an matrix
- 5 *******************************************/
- 6 #include <iostream>
- 7 #include <cstdio>
- 8 #include <algorithm>
- 9
- 10 using namespace std;
- 11
- 12 void BuildMatrix(int *** pmaze,unsigned row_num,unsigned column_num)
- 13 {
- 14 *pmaze = new int*[row_num];
- 15 for(unsigned i=0;i<row_num;++i){
- 16 (*pmaze)[i] = new int[column_num];
- 17 }
- 18 }
- 19
- 20 void ReleaseMatrix(int ***pmaze,unsigned row_num)
- 21 {
- 22 if(!pmaze) return;
- 23
- 24 for(unsigned i=0;i<row_num;++i){
- 25 delete [](*pmaze)[i];
- 26 }
- 27
- 28 delete [](*pmaze);
- 29 }
- 30
- 31 void CoreSolve(int ***ppDistanceMatrix,unsigned matrix_size)
- 32 {
- 33 for(int i=0;i<matrix_size;++i){
- 34 for(int j=i;j<matrix_size;++j){
- 35 if(i-1>=0&&j-1>=0){
- 36 (*ppDistanceMatrix)[i][j] += max((*ppDistanceMatrix)[i-1][j],(*ppDistanceMatrix)[i][j-1]);
- 37 }else if(i-1>=0){
- 38 (*ppDistanceMatrix)[i][j] += (*ppDistanceMatrix)[i-1][j];
- 39 }else if(j-1>=0){
- 40 (*ppDistanceMatrix)[i][j] += (*ppDistanceMatrix)[i][j-1];
- 41 }
- 42 }
- 43
- 44 for(int k=i+1;k<matrix_size;++k){
- 45 if(k-1>=0&&i-1>=0){
- 46 (*ppDistanceMatrix)[k][i] += max((*ppDistanceMatrix)[k-1][i],(*ppDistanceMatrix)[k][i-1]);
- 47 }else if(k-1>=0){
- 48 (*ppDistanceMatrix)[k][i] += (*ppDistanceMatrix)[k-1][i];
- 49 }else if(i-1>=0){
- 50 (*ppDistanceMatrix)[k][i] += (*ppDistanceMatrix)[k][i-1];
- 51 }
- 52 }
- 53 }
- 54 }
- 55
- 56 void Solve()
- 57 {
- 58 unsigned matrix_size = 0;
- 59 int **ppmatrix = NULL;
- 60 cin>>matrix_size;
- 61 BuildMatrix(&ppmatrix,matrix_size,matrix_size);
- 62 for(unsigned i=0;i<matrix_size;++i){
- 63 for(unsigned j=0;j<matrix_size;++j){
- 64 cin>>ppmatrix[i][j];
- 65 }
- 66 }
- 67
- 68 int **ppDistanceMatrix = NULL;
- 69 BuildMatrix(&ppDistanceMatrix,matrix_size,matrix_size);
- 70 for(unsigned i=0;i<matrix_size;++i){
- 71 for(unsigned j=0;j<matrix_size;++j){
- 72 ppDistanceMatrix[i][j]=ppmatrix[i][j];
- 73 }
- 74 }
- 75
- 76 CoreSolve(&ppDistanceMatrix,matrix_size);
- 77 cout<<ppDistanceMatrix[matrix_size-1][matrix_size-1];
- 78
- 79 ReleaseMatrix(&ppmatrix,matrix_size);
- 80 ReleaseMatrix(&ppDistanceMatrix,matrix_size);
- 81 }
- 82
- 83 int main()
- 84 {
- 85 freopen("data.in","r",stdin);
- 86 freopen("data.out","w",stdout);
- 87
- 88 unsigned case_num = 0;
- 89 cin>>case_num;
- 90
- 91 for(unsigned i=1;i<=case_num;++i){
- 92 cout<<"Case #"<<i<<": ";
- 93 Solve();
- 94 cout<<endl;
- 95 }
- 96
- 97 return 0;
- 98 }
Cases in data.in file
- 3
- 3
- 1 2 8
- 7 20 8
- 5 3 8
- 3
- 1 2 8
- 7 0 8
- 5 3 8
- 2
- 1 2
- 3 4
output in data.out file
- Case #1: 44
- Case #2: 27
- Case #3: 8
Pls let me know if you find any mistakes above.Thx.
Select the best path in a matrix的更多相关文章
- Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
- 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- Longest Increasing Path in a Matrix -- LeetCode 329
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- LeetCode #329. Longest Increasing Path in a Matrix
题目 Given an integer matrix, find the length of the longest increasing path. From each cell, you can ...
- Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- LeetCode Longest Increasing Path in a Matrix
原题链接在这里:https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, ...
- leetcode@ [329] Longest Increasing Path in a Matrix (DFS + 记忆化搜索)
https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, find the ...
- [Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
随机推荐
- Timer组件
1.常用属性 Interval 用于获取或设置Timer组件Tick事件发生的时间间隔,属性值不能小于1 制作左右飘摇窗体 private void timer1_Tick(object sender ...
- SD卡FAT32文件系统格式
一.声明 1.本文来源和主旨 2.本文测试环境 二.SD卡FAT文件系统 1.SD卡FAT32文件系统的整体布局 2.FAT文件系统简介 ① 文件分配表 ② 目录项 三.DBR(DOS BOOT RE ...
- 【Http】Http权威指南
God Is Coder 2012-10-17 22:25 阅读:77 评论:0 <http权威指南>阅读笔记(十二) God Is Coder 2012-10-17 22:04 阅读 ...
- VisualStudio自定义代码段_方法二
1.在项目中新增一个xml文件为vcoo.snippet,然后右键“插入代码段”,选择Snippet即可: 2.修改代码片段内容后保存: 3.VS菜单中选择“工具”-“代码段管理器”导入这个snipp ...
- 将CMD内的显示内容输出到txt文件
将CMD内的显示内容输出到txt文件 xxxx -t >c:\test.txt //xxxx为命令 如ping www.baidu.com //-t >c:\test.tx ...
- mysql基本内容学习过程
mysql数据库的基本操作: , 数据库的登录:mysql -u 用户名(root) -p密码 -P (端口) -h服务器名(本地表示:127.0.0.1) . 更改数据库显示:mysql -u ro ...
- TSS 内核栈 用户栈的关系
http://blog.sina.com.cn/s/blog_673ef8130100qaje.html 该博客不错,有不少有用的信息 中断程序的一开始我们执行一个PUSHALL,把这些积存器保存在核 ...
- RedHat Linux 下安装MPlayer 编译源代码方式
http://blog.csdn.net/hotday_kevin/article/details/6874703
- VC 无标题栏对话框移动(在OnLButtonDown里再次发送消息)
操作系统:Windows 7软件环境:Visual C++ 2008 SP1本次目的:实现无框移动 所谓的无标题栏对话框,是基于对话框的工程,对话框属性Border设置为None,对话框如下所示: 为 ...
- 【HDOJ】1720 A+B coming
水题. #include <stdio.h> #include <string.h> #define MAXNUM 1005 int stoi(char); int main( ...