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.

转载于:https://www.cnblogs.com/zhouyoulie/p/3960536.html

基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip 【备注】 1、该资源内项目代码百分百可运行,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值