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 ...
随机推荐
- Junit4.12、Hamcrest1.3、Eclemma的安装和使用
1. Junit4.12和Hamcrest1.3的安装过程 步骤: 网上下载Junit和Hamcrest包文件,保存在本地. 新建Java项目命名为Triangle,在Eclipse菜单栏选择项目(P ...
- 图片生成操作属性导致WP内存溢出解决办法
在开发的项目中,发现经常会出现异常 “内存溢出,不能再继续执行程序”,通过搜索一些国外的文章,发现原来是由于项目中的图片比较多,而生存操作设为了“内容”.通过设置图片的生成操作为“无”,复制操作为“如 ...
- C# 操作mongodb 分组
c#操作mongodb的分组的简单例子: 1.首先要下载c#对应的mongodb驱动,官方下载地址:https://github.com/mongodb/mongo-csharp-driver/rel ...
- tomcat 7 下添加 shared/lib 文件夹
你打开tomcat7\conf\catalina.properties文件再打开tomcat5的,看完后, 你就知道了 tomcat 5.5.35 # # List of comma-separate ...
- ie6 css sprites重复加载
如果你使用css sprites,那么在ie6下并不能发挥sprites的作用,它还是会每次再重新 加载这个图片,解决方法为为ie6添加下面这条js: <!--[if IE 6]> ...
- Python中zip()函数用法
定义:zip([iterable, …])zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的l ...
- 剖析 Linux hypervisor--KVM 和 Lguest 简介
慢慢弄清楚.. M. Tim Jones, 顾问工程师, Emulex Corp. M. Tim Jones 是一名嵌入式软件工程师,他是 Artificial Intelligence: A S ...
- 增加Android可用内存
In the development of TV applications, especially when dealing with images were more likely to feel ...
- android 提示用户是否退出应用程序 提升用户体验
首先明确一点,用户的一直点击的返回键,之后就会退出到桌面.那么,如何提示呢?很简单,在用户一顿返回键回到程序入口处(即程序的第一个Activity)给用户一个提示,您再按可就退出程序啦?那么如何在程序 ...
- 面向XX编程
[一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/5033186.html ] 基于面向XX编程的个人理解 面向过程编程 Procedure Or ...