-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaveprogram.cpp
More file actions
79 lines (71 loc) · 2.04 KB
/
Copy pathcaveprogram.cpp
File metadata and controls
79 lines (71 loc) · 2.04 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
#include <iostream>
using namespace std;
void loadalldata();
bool findpath(int here, int last);
class cave_class {
private:
int numdoors; // number of doors out of the cave
int door[25]; // hold index number of cave behind the door
public:
void loaddata(int n, int a, int b, int c, int d, int e);
int getnumdoors() { return numdoors; }
int getdoor(int i) { return door[i]; }
};
int cave_total; //total number of caves
cave_class cave[500]; // assume a max of 500 cave_class
int main() {
int first, last;
bool pathexists;
loadalldata();
cout << "Enter the number of the first cave: ";
cin >> first;
cout << "Enter the number of the last cave: ";
cin >> last;
cout << endl;
pathexists = findpath(first, last);
cout << last << endl; //DEBUG cout
if(pathexists == true) {
cout << "A path was found from " << first;
cout << " to " << last << endl << endl;
} else {
cout << "There is no path from " << first;
cout << " to " << last << endl << endl;
}
}
bool findpath(int here, int last) {
int cavenum, num, i;
bool success;
if(here == last) {
return true; // the base case
}
num = cave[here].getnumdoors();
cout << here << " -> "; //debug cout
for (i = 0; i < num; i++) {
cavenum = cave[here].getdoor(i);
success = findpath(cavenum, last);
if (success == true) {
return true;
}
}
return false;
}
void loadalldata() {
// load the data for all caves - change as needed
// this data is for the example in the notes
// -1 us used to indicate no door
cave[0].loaddata(3, 4, 3, 2, -1, -1);
cave[1].loaddata(0, -1, -1, -1, -1, -1);
cave[2].loaddata(1, 1, -1, -1, -1, -1);
cave[3].loaddata(2, 0, 2, -1, -1, -1);
cave[4].loaddata(1, 3, -1, -1, -1, -1);
cave_total = 5;
}
//----------methods for the cave_class-----------
void cave_class::loaddata(int n, int a, int b, int c, int d, int e){
numdoors=n;
door[0] = a;
door[1] = b;
door[2] = c;
door[3] = d;
door[4] = e;
}