-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortest Path.cpp
More file actions
114 lines (85 loc) · 2.47 KB
/
Copy pathShortest Path.cpp
File metadata and controls
114 lines (85 loc) · 2.47 KB
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
#include<iostream>
using namespace std;
const int rows = 5;
const int cols = 6;
int trace[rows][cols];
int cost[rows][cols] = {
{3,0,0,0,0,0},
{6,0,0,0,0,0},
{5,0,0,0,0,0},
{8,0,0,0,0,0},
{3,0,0,0,0,0}};
int weight[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 main(){
int left, up, down; //tiles from feeder tiles
//a double for loop, starts at first row of the second column, and moves down along tiles
for(int j = 1; j <= cols-1; j++){
for(int i = 0; i <= rows-1; i++){
//defining the directions of filling the cost table
left = cost[i][j-1];
up = cost[(i-1 + rows) % rows][j-1]; //mod for wraparound
down = cost[(i+1) % rows][j-1];
//now determine what tile was the minimum cost
int min = left;
if(up < min)
min = up;
if(down < min)
min = down;
//now fill the trace table
if(left == min)
trace[i][j] = i;
else if(up == min)
trace[i][j] = (i-1+rows) % rows;
else if(down == min)
trace[i][j] = (i+1) % rows;
//now that we have determined the cheapest tile, update the cost table
cost[i][j] = weight[i][j] + min;
}
}//end double for loop
//printing the completed cost table
cout<<"The cost table is: "<<endl;
for( int i = 0; i < 5; i++){
for(int j = 0; j < 6; j++){
cout<<cost[i][j]<<", ";
}
cout<<endl;
}
int ex[rows];
int min_row = 0;
//get the shortest path out of each cell on the right
for( int i = 0; i<rows; i++){
ex[i] = cost[i][cols-1];
}
//now find the smallest of them
int min = ex[0];
for(int i = 0; i<rows; i++){
if(ex[i] < min){
min = ex[i];
min_row = i;
}
}
cout<<endl<<"The shortest path is of length: "<<min<<endl;
int path[cols]; //array to store the path taken
for(int i = cols-1; i>=0; i--){ //working backwards
path[i] = min_row;
min_row = trace[min_row][i]; //get the value of tile from previous column of trace table
}
//printing the path from left to right
cout<<endl<<"The path taken is: ";
for(int i = 0; i<=cols-1; i++){
cout<<path[i]<<" ";
}
cout<<endl<<endl<<"Printing the actual trace table:"<<endl;
for(int i = 0; i <5; i++){
for(int j = 0; j < 6; j++){
cout<<trace[i][j];
}
cout<<endl;
}
return 0;
}//main