Recursion to find shortest path

Long question.
I'm finishing a class project and I'm in desperate need of help. The goal is practicing recursion. Were given a text file like this:

5 5
6 2 3 44 15
1 7 2 9 10
11 1 5 14 12
5 17 2 1 20
21 7 33 4 25
where the first row is the row and column size. We only can move right or down to make it easy for us. The output would look like:

50 > > v v v > v >

My header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    #ifndef MATRIX_H
    #define MATRIX_H
    
    #include <string>
    #include <iostream>
    using namespace std;
    
    class Matrix
    {
    	public:
    		Matrix(int pRows = 10, int pCols = 10);
    		Matrix(string file);
    		~Matrix();
    
    		void resize(int pRows, int pCols);
    		void print(ostream &sout = cout) const;
    
    		bool loadFile(string file);
    		bool saveFile(string file) const;
    
    		int getRows() const;
    		int getCols() const;
    
    		void set(int row, int col, int value);
    		int get(int row, int col) const;
    
    		const int *operator[](int i) const;
    		int *operator[](int i);
    	private:
    		int **array;
    		int rows, cols;
        };
    
        #endif 


Includers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    #include <iostream>
    #include <fstream>
    #include "matrix.h"
    using namespace std;
    
    Matrix::Matrix(int pRows, int pCols)
    {
    	array = NULL;
    	resize(pRows, pCols);
    }
    
    Matrix::Matrix(string file)
    {
    	array = NULL;
    	loadFile(file);
    }
    
    Matrix::~Matrix()
    {
    	// Deallocate each row
    	for(int i = 0; i < rows; i++)
    	{
    		delete[] array[i];
    	}
    	// Deallocate the array of rows
    	delete[] array;
    }
    
    void Matrix::resize(int pRows, int pCols)
    {
    	// Deallocate old rows
    	if(array != NULL)
    	{
    		for(int i = 0; i < rows; i++)
    		{
    			delete[] array[i];
    		}
    		delete[] array;
    		array = NULL;
    	}
    
    	// Save new rows
    	rows = pRows;
    	cols = pCols;
    	
    	// Allocate an array of rows
    	array = new int *[rows];
    	for(int i = 0; i < rows; i++)
    	{
    		// Allocate each row
    		array[i] = new int[cols];
    		
    		// Initialize each row
    		for(int j = 0; j < cols; j++)
    		{
    			array[i][j] = 0;
    		}
    	}	
    }
    
    bool Matrix::loadFile(string file)
    {
    	ifstream fin;
    	fin.open(file.c_str());
    
    	int r, c;
    
    	if(!fin.good())
    	{
    		fin.close();
    		return false;
    	}
    	
    	fin >> r >> c;
    	if(r > 0 && c > 0)
    	{
    		resize(r, c);
    		for(int i = 0; i < r; i++)
    		{
    			for(int j = 0; j < c; j++)
    			{
    				if(!fin.good())
    				{
    					fin.close();
    					return false;
    				}
    				fin >> array[i][j];
    			}
    		}
    	}
    
    	fin.close();
    	return true;
    }
    
    bool Matrix::saveFile(string file) const
    {
    	ofstream fout;
    	fout.open(file.c_str());
    	
    	if(!fout.good())
    	{
    		fout.close();
    		return false;
    	}
    
    	fout << rows << " " << cols << "\n";	
    	print(fout);
    	
    	fout.close();
    	return true;
    }
    
    void Matrix::print(ostream &sout) const
    {
    	for(int i = 0; i < rows; i++)
    	{
    		for(int j = 0; j < cols; j++)
    		{
    			sout << array[i][j] << " ";
    		}
    		sout << "\n";
    	}
    }
    
    int Matrix::getRows() const
    {
    	return rows;
    }
    
    int Matrix::getCols() const
    {
    	return cols;
    }
    
    // Set a value in the matrix by row and column
    void Matrix::set(int row, int col, int value)
    {
    	array[row][col] = value;
    }
    
    // Get a value from the matrix by row and column
    int Matrix::get(int row, int col) const
    {
    	return array[row][col];
    }
    
    // Operator for getting
    const int *Matrix::operator[](int i) const
    {
    	return array[i];
    }
    
    // Operator for setting
    int *Matrix::operator[](int i)
    {
    	return array[i];
    }


I need help with the actual recursion part in the main program. I have no idea what to do. This concept is confusing to me and I'm short on time.
Last edited on
I'll give you the function prototype and base case and let you figure out the rest.
1
2
3
4
5
int shortest_path(int depth, bool sequence[]){
    if (last_cell())
        return my_value();
    //...
}
Topic archived. No new replies allowed.