码迷,mamicode.com
首页 > 其他好文 > 详细

Select the best path in a matrix

时间:2014-09-07 17:14:35      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   ar   for   2014   div   

  I feel a little down recently.But just like Lady Antebellum‘s song says,there are a lot of ups and downs in life,so I choose to learn from the defeat.

  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.

  Using 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

标签:style   blog   color   os   io   ar   for   2014   div   

原文地址:http://www.cnblogs.com/zhouyoulie/p/3960536.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!