-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path05_shortest_path_string.cpp
68 lines (60 loc) · 1.41 KB
/
05_shortest_path_string.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
/*
Problem - Find the displacement N ^
|
Input String: NNNSSEEWE |
Output : NEE |
W<-------o------->E
(0,0)|
|
|
S v
*/
#include <iostream>
using namespace std;
int main(){
char path;
int x=0, y=0;
cout<<"Enter your path: ";
path = cin.get();
while (path != '\n'){
if(path == 'N' or path == 'n'){
y++;
}
else if(path == 'S' or path == 's'){
y--;
}
else if(path == 'E' or path == 'e'){
x++;
}
else if(path == 'W' or path == 'w'){
x--;
}
path = cin.get();
}
cout<<"Final Displacement: x="<< x<<" y="<<y<<endl;
cout<<"Shortest Path: ";
if(x>0){
while(x>0){
cout<<"E";
x--;
}
}else{
while(x<0){
cout<<"W";
x++;
}
}
if(y>0){
while(y>0){
cout<<"N";
y--;
}
}else{
while(y<0){
cout<<"S";
y++;
}
}
cout<<endl;
return 0;
}