-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.cpp
112 lines (102 loc) · 3.46 KB
/
robot.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
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
#include <iostream>
#include "robot.h"
#include <math.h>
using namespace std;
// implementation of constructor with no path given
Robot::Robot(int curX, int curY, int targetX, int targetY)
{
this->curX = curX;
this->curY = curY;
this->targetX = targetX;
this->targetY = targetY;
this->sd = abs(this->curX - this->targetX) + abs(this->curY - this->targetY);
this->path = "";
}
// implementation of constructor with path given
Robot::Robot(int curX, int curY, int targetX, int targetY, string path)
{
this->curX = curX;
this->curY = curY;
this->targetX = targetX;
this->targetY = targetY;
this->sd = abs(this->curX - this->targetX) + abs(this->curY - this->targetY);
this->path = path;
}
// facade method which calls actual recursive method
void Robot::printShortestPath()
{
int numOfPaths = 0;
printShortestPath(*this, numOfPaths);
cout << "Number of Paths: " << numOfPaths << endl;
}
// recursive method which prints every shortest path of robot once robot has reached target
void Robot::printShortestPath(Robot &r, int &numOfPaths)
{
// base case:
if (r.sd == 0) // if length of shortest path is 0, robot is at target value
{
if (r.path.length() > 0) // if path is not empty
{
numOfPaths++;
cout << r.path << endl;
}
return;
}
if (r.shouldGoNorth())
{
// new robot is closer to target gaurenteed
Robot closer(r.curX, r.curY + 1, r.targetX, r.targetY, r.path + 'N'); // new robot that is north of current
printShortestPath(closer, numOfPaths);
}
if (r.shouldGoEast())
{
// new robot is closer to target gaurenteed
Robot closer(r.curX + 1, r.curY, r.targetX, r.targetY, r.path + 'E'); // new robot that is east of current
printShortestPath(closer, numOfPaths);
}
if (r.shouldGoSouth())
{
// new robot is closer to target gaurenteed
Robot closer(r.curX, r.curY - 1, r.targetX, r.targetY, r.path + 'S'); // new robot that is east of current
printShortestPath(closer, numOfPaths);
}
if (r.shouldGoWest())
{
// new robot is closer to target gaurenteed
Robot closer(r.curX - 1, r.curY, r.targetX, r.targetY, r.path + 'W'); // new robot that is east of current
printShortestPath(closer, numOfPaths);
}
}
// All shouldGo methods determine if robot is allowed to go the given direction
bool Robot::shouldGoNorth()
{
if (abs(this->curX - this->targetX) + abs((this->curY + 1) - this->targetY) >= this->sd || (this->path[this->path.length() - 2] == 'N' && this->path[this->path.length() - 1] == 'N'))
{
return false;
}
return true;
}
bool Robot::shouldGoSouth()
{
if (abs(this->curX - this->targetX) + abs((this->curY - 1) - this->targetY) >= this->sd || (this->path[this->path.length() - 2] == 'S' && this->path[this->path.length() - 1] == 'S'))
{
return false;
}
return true;
}
bool Robot::shouldGoEast()
{
if (abs((this->curX + 1) - this->targetX) + abs(this->curY - this->targetY) >= this->sd || (this->path[this->path.length() - 2] == 'E' && this->path[this->path.length() - 1] == 'E'))
{
return false;
}
return true;
}
bool Robot::shouldGoWest()
{
if (abs((this->curX - 1) - this->targetX) + abs(this->curY - this->targetY) >= this->sd || (this->path[this->path.length() - 2] == 'W' && this->path[this->path.length() - 1] == 'W'))
{
return false;
}
return true;
}