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. WPF中的一些常用类型转换

    1.string和Color的转换: //string转Color (Color)ColorConverter.ConvertFromString((string)str); //Color转stri ...

  2. ubuntu10.04搭建嵌入式开发环境

    改源 配置vim set number set autoindent set smartindent set tabstop=4 set incsearch 安装g++ 配置samba 1.先安装程序 ...

  3. linux内核驱动模型

    linux内核驱动模型,以2.6.32内核为例.(一边写一边看的,有点乱.) 1.以内核对象为基础.用kobject表示,相当于其它对象的基类,是构建linux驱动模型的关键.具有相同类型的内核对象构 ...

  4. 2.MVC框架开发(视图开发----基础语法)

    1.区别普通的html,在普通的html中不能将控制器里面的数据展示在html中. 在MVC框架中,它提供了一种视图模板(就是结合普通的html标签并能将控制器里传出来的数据进行显示) 视图模板特性: ...

  5. cocos2d-x mac or windows eclipse android ------ Eclipse工程里面还会有许多警告

    由于公司的游戏项目比较特殊,  coco2d-x 的  ios  和  android  的游戏 代码 没有采用 共享目录的开发方式.所以android 内的 c++ 游戏代码全部放在 jni 下, ...

  6. Java中堆和栈创建对象的区别

    http://blog.csdn.net/hbhhww/article/details/8152838

  7. Android Button悬浮在SurfaceView上

    实现Button悬浮于与SurfaceView之上实现 注意:你实现的SurfaceView和android中的Button,EditView是同级的,不能把一个包含在另一个里面 1.创建自己的Sur ...

  8. laravel 模板 blade

    控制器布局 在Laravel框架中使用模板的一种方法就是通过控制器布局.通过在控制器中指定 layout 属性,对应的视图会被创建并且作为请求的默认返回数据. 在控制器中定义一个布局 class Us ...

  9. Android 导出db并查看内容

    1.导出sqlite的db文件: 使用工具DDMS,切换到DDMS,显示File Explorer窗口,找到/data/data/应用名/databases/数据库名,点击导出按钮,导出文件. 2.使 ...

  10. Windows7 64下MinGW64/MSYS环境搭建

    原文出处: CompileGraphics Magick, Boost, Botan and QT with MinGW64 under Windows 7 64 http://www.kinetic ...