The first step make sure the meaning of the contents in dp[i][j];
In this question, the phrase could be considered as the different rows and columns, the evaluaton function is the summerization whole values from (0,0) to here.
because in the end , we need to output the trajectory from the start to end point. Therefore, we need another pre matrix to record previous points for every stages.
The outputs:
The corresponding codes:
//obtain the least summary of a trajetory in the given matrix
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
void initialize();
int min_path();
#define MAX 20
void display_matrix(int pre[][MAX]);
int a[][4] = {
{1, 3, 5, 9},{8, 1, 3, 4},{5, 0, 6, 1}, {8, 8, 4, 0}};
int row = sizeof(a)/sizeof(a[0]);
int col = sizeof(a[0])/sizeof(a[0][0]);
int dp[MAX][MAX];
int pre[MAX][MAX];
int main()
{
initialize();
cout << endl << min_path() << endl;
return 0;
}
void initialize()
{
memset(dp, 1, sizeof(dp));
memset(pre, -1, sizeof(pre));
}
int min_path()
{
dp[0][0] = a[0][0];
for (int i = 1; i < row; i++){
dp[i][0] = dp[i-1][0] + a[i][0];
pre[i][0] = 0;
}
for (int j = 1; j < col; j++){
dp[0][j] = dp[0][j-1] + a[0][j];
pre[0][j] = 1;
}
for (int i = 1; i < row; i++){
for (int j = 1; j < col; j++){
if(dp[i-1][j] > dp[i][j-1]){
dp[i][j] = dp[i][j-1] + a[i][j];
pre[i][j] = 1;
}else{
dp[i][j] = dp[i-1][j] + a[i][j];
pre[i][j] = 0;
}
}
}
cout << "The matrix dp[][]" << endl;
display_matrix(dp);
cout << "The matix pre:[][]" << endl;
display_matrix(pre);
return dp[row - 1][col -1];
}
void display_matrix(int pre[][MAX])
{
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
cout << setw(3) << pre[i][j];
}
cout << endl;
}
}
The final results :
The corresping codes with a recursive methods :
//obtain the least summary of a trajetory in the given matrix
#include <iostream>
#include <iomanip>
#include <cstring>
#include <vector>
using namespace std;
void initialize();
int min_path();
#define MAX 20
void display_matrix(int pre[][MAX]);
void display_trajectory(int row, int col);
int a[][4] = {
{1, 3, 5, 9},{8, 1, 3, 4},{5, 0, 6, 1}, {8, 8, 4, 0}};
int row = sizeof(a)/sizeof(a[0]);
int col = sizeof(a[0])/sizeof(a[0][0]);
int dp[MAX][MAX];
int pre[MAX][MAX];
vector<int> path;
int main()
{
initialize();
cout << endl << min_path() << endl;
display_trajectory(row-1, col-1);
vector<int>::reverse_iterator it;
for (it = path.rbegin(); it != path.rend(); ++it){
cout << *it << "->";
}
return 0;
}
void initialize()
{
memset(dp, 1, sizeof(dp));
memset(pre, -1, sizeof(pre));
}
int min_path()
{
dp[0][0] = a[0][0];
for (int i = 1; i < row; i++){
dp[i][0] = dp[i-1][0] + a[i][0];
pre[i][0] = 0;
}
for (int j = 1; j < col; j++){
dp[0][j] = dp[0][j-1] + a[0][j];
pre[0][j] = 1;
}
for (int i = 1; i < row; i++){
for (int j = 1; j < col; j++){
if(dp[i-1][j] > dp[i][j-1]){
dp[i][j] = dp[i][j-1] + a[i][j];
pre[i][j] = 1;
}else{
dp[i][j] = dp[i-1][j] + a[i][j];
pre[i][j] = 0;
}
}
}
cout << "The matrix dp[][]" << endl;
display_matrix(dp);
cout << "The matix pre:[][]" << endl;
display_matrix(pre);
return dp[row - 1][col -1];
}
void display_matrix(int pre[][MAX])
{
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
cout << setw(3) << pre[i][j];
}
cout << endl;
}
}
void display_trajectory(int row, int col)
{
if (row == 0 && col == 0){
path.push_back(a[row][col]);
}else{
if (pre[row][col] == 0){
path.push_back(a[row][col]);
display_trajectory(row-1, col);
}else{
path.push_back(a[row][col]);
display_trajectory(row, col-1);
}
}
}