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 ...
随机推荐
- 产生一个长度为100的int数组,并向其中随机插入1-100,不能重复
]; ArrayList myList=new ArrayList(); Random rnd=new Random(); ) { ,); if(!myList.Contains(num)) myLi ...
- Python的库和资源(转)
Python的库和资源: http://blog.sina.com.cn/s/blog_3cb6a78c0100hpaq.html Python 常用模块: http://www.pythonclub ...
- visualvm 监控 远程 机器上的 Java 程序
JDK里面本身就带了很多的监控工具,如JConsole等. 我们今天要讲的这款工具visualvm,就是其中的一款.但是这款工具是在JDK1.6.07及以上才有的.它能够对JAVA程序的JVM堆.线程 ...
- django中外键关联表的查询随笔
django中,如果一个数据库中的表之间有外键的话可以方便的通过一个表查询到其相关表的数据.如有下面三个model:class Blog(models.Model): name = models ...
- hdu 4286
splay 练手用: 杭电的oj要手动开栈: #include<cstdio> #pragma comment(linker, "/STACK:102400000,1024000 ...
- MUH and Cube Walls
Codeforces Round #269 (Div. 2) D:http://codeforces.com/problemset/problem/471/D 题意:给定两个序列a ,b, 如果在a中 ...
- jenkin系列_调度jmeter实现分布式测试
假设现在有 192.168.1.100(jmeter 控制器 C ).192.168.1.101(jmeter负载机 B)两台机器进行分布式测试,各个步骤如下 1. C 和B 安装jmeter,并运行 ...
- HDU Traffic Real Time Query System
题目大意是:对于(n, m)的图,给定边a, b查询从a到b要经过的割点的最少数目. 先tarjan算法求双连通然后缩点,即对于每个割点将周围的每个双连通看成一个点与之相连.然后求解LCA即可,距离d ...
- Google的代码风格规范,各种语言都很全
https://code.google.com/p/google-styleguide/
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...