forked from arianamestel/CSCI211
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMestelAriana_Assignment_015.cpp
50 lines (42 loc) · 1.06 KB
/
MestelAriana_Assignment_015.cpp
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
#include <iostream>
using namespace std;
const int rows = 5;
const int cols = 6;
static int trace[rows][cols] = {0};
int step[rows][cols] = {0};
int w[rows][cols] = {{3,4,1,2,8,6},
{6,1,8,2,7,4},
{5,9,3,9,9,5},
{8,4,1,3,2,6},
{3,7,2,8,6,4}};
int min (int a, int b, int c) {
int min = a;
if (b < min) min = b;
if (c < min) min = c;
return min;
}
int cost(int i, int j) {
if (j == 0) return trace[i][j] = w[i][0];
if (trace[i][j] != 0) return trace[i][j];
int left = cost(i, j - 1);
int up = cost(((i - 1 + 5) % 5), (j - 1));
int down = cost(((i + 1 + 5) % 5),(j - 1));
int minimum = min(left, up, down);
return trace[i][j] = w[i][j] + minimum;
}
int main () {
int q[rows], path[cols];
for (int i = 0; i < rows; i++) {
q[i] = cost(i, cols - 1);
}
int min = q[0];
int min_i = 0;
for (int i = 0; i < rows; i++) {
if (q[i] < min) {
min = q[i];
min_i = i;
}
}
cout << "The shortest path is of length " << min << " " << endl;
return 0;
}