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的更多相关文章

  1. Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

    Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...

  2. 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. Longest Increasing Path in a Matrix

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  7. LeetCode Longest Increasing Path in a Matrix

    原题链接在这里:https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. 1.0 基础、标示符、常量、数据类型(enum 枚举,struct 结构体)、操作符、循环、数组

    一.程序 现实生活中,程序是指完成某些事务的一种既定方法和过程,可以把程序看成是一系列动作执行过程的描述. 在计算机世界,程序是指令,即为了让计算机执行某些操作或解决某个问题而编写的一系列有序指令的集 ...

  2. 【mapping】 springmvc的注解mapping无法生效的问题

    springmvc 始终无法加载 注解 map, 解决办法 八月 11, 2015 8:24:42 下午 org.springframework.web.servlet.DispatcherServl ...

  3. MVC-处理时间格式

    第一种方法:先设置一个时间显示的模板,然后在需要显示时间的地方调用这个模板就行了. 1.在Share文件夹下,创建一个文件夹DisplayTemplates 2.在DisplayTemplates文件 ...

  4. C# dynamic

    [TestMethod] public void DynamicTest() { dynamic Customer = new ExpandoObject(); Customer.Name = &qu ...

  5. C# - 接口,继承

    接口 接口是把公共实例(非静态)方法和属性组合起来,以封装特定功能的一个集合.不能像实例化一个类那样实例化接口.接口不能包含实现其成员的任何代码,而只能定义成员本身.实现过程必须在实现接口的类中完成. ...

  6. Python标准库与第三方库详解(转载)

    转载地址: http://www.codeweblog.com/python%e6%a0%87%e5%87%86%e5%ba%93%e4%b8%8e%e7%ac%ac%e4%b8%89%e6%96%b ...

  7. python 内置函数 getattr

    class Getattr_Test(): var_a = 'abc' def methodA(self): var_b = 'xyz' return var_b t = Getattr_Test() ...

  8. Mysql 批量建表存储过程

    最近项目中用到了使用存储过程批量建表的功能,记录下来: USE db_test_3; drop procedure if EXISTS `createTablesWithIndex`; create ...

  9. GridView 根据要求显示指定值

    最近在写一个小项目用来练手恢复一下功力的,在Users表中有一个用户字段是状态,我使用"0"表示启用,“1”表示禁用, 存到数据库中, 由于之前有一段时间没写代码了,所以有点生疏了 ...

  10. QML嵌入到QWidget中方法

    简介 嵌入方法有两种一种是直接拖控件,另一种是cpp代码动态生成, 控件方式 动态代码生成 QQuickWidget *m_quickWidget=new QQuickWidget(); QUrl s ...