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 ...
随机推荐
- Random.Next获取随即数
Random random = new Random(); random.Next()--------------返回非负的一个随机数. random.Next(int maxValue)----- ...
- 一步步学习ASP.NET MVC3 (5)——View从Action中获得数据
请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们把Razor的模板技术给大家介绍了一下,当然模板中还有其他的知识点,这个以后我们还会继续讲解.本章我们主要讨论 ...
- Xcode常见错误以及解决方案
一.Undefined symbols for architecture x86_64: Xcode升级到5.1 新特性之一就是默认让所有App都通过64位编译器编译.原来在Xcode5.0.x的时候 ...
- C# 启动进程和杀死进程
/// <summary> /// 杀死进程 /// </summary> private void KillProcesses() { var cfn = GetAppset ...
- 计算S(n)=a+aa+aaa+...... 其中a是一个数字
描述 计算S(n)=a+aa+aaa+...... 其中a是一个数字 输入数据 两个分别表示a和n的整数 输出数据 一个表示S(n)的整数 输入示例 3 5 输出示例 37035 # include ...
- ISNULL
ISNULL 使用指定的替换值替换 NULL. 语法ISNULL ( check_expression , replacement_value ) 参数check_expression 将被检查是否为 ...
- Android App性能优化笔记之一:性能优化是什么及为什么?
By Long Luo 周星驰的电影<功夫>里面借火云邪神之口说出了一句至理名言:“天下武功,唯快不破”. 在移动互联网时代,同样如此,留给一个公司的窗口往往只有很短的时间,如何把握住 ...
- Windows server 2008下开启telnet功能
今天在windows server 2008 R2下使用telnet 来测试端口是否可以连接,结果发现如下错误:
- [dp]Codeforces30C Shooting Gallery
题目链接 题意: 给n个点 每个点的坐标 x y 出现的时间t 射中的概率 从i点到j点的时间为它们的距离. 求射中个数的最大期望 很水的dp 坑点就是要用LL #include <cstdi ...
- ANDROID_MARS学习笔记_S01原始版_020_Mp3player001_歌曲列表
一.项目设计 二.歌曲列表简介 1.利用java.net.HttpURLConnection以流的形式下载xml文件为String 2.自定义ContentHandler-->Mp3ListCo ...