-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12b.cpp
More file actions
68 lines (63 loc) · 1.47 KB
/
12b.cpp
File metadata and controls
68 lines (63 loc) · 1.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
#include <iostream>
#include <cstring>
#include <sstream>
#include <math.h>
#include <math.h>
#include <cstdlib>
#define PI 3.141592653589793
using namespace std;
float radians(float ang){
return ang*PI/180;
}
pair<float,float> rotate(pair<float,float>origen, pair<float,float>coord, float radian){
float orx=origen.first, ory=origen.second;
float cox=coord.first, coy=coord.second;
float resx=orx+cos(radian)*(cox-orx)-sin(radian)*(coy-ory);
float resy=ory+sin(radian)*(cox-orx)+cos(radian)*(coy-ory);
return {round(resx),round(resy)};
}
int main(){
string dato;
string accion;
float valor;
pair<float,float>coord={0.0,0.0};
pair<float,float>waypoint={10.0,1.0};
while(cin>>dato){
/*leer dato*/
accion=dato[0];
string s = dato.substr(1);
stringstream ss(s);
ss >> valor;
if(accion=="N"){
//norte
waypoint.second+=valor;
}
else if(accion=="S"){
//sur
waypoint.second-=valor;
}
else if(accion=="E"){
//este
waypoint.first+=valor;
}
else if(accion=="W"){
//oeste
waypoint.first-=valor;
}
else if(accion=="R" or accion=="L"){
//gira der o izq
if(accion=="R")
valor*=-1;
waypoint=rotate({0.0,0.0},waypoint,radians(valor));
}
else if(accion=="F"){
//avanza
coord.second+=waypoint.second*valor;
coord.first+=waypoint.first*valor;
}
cout<<accion<<" "<<valor<<endl;
cout<<coord.first<<" "<<coord.second<<endl;
cout<<waypoint.first<<" "<<waypoint.second<<endl;
}
cout<<abs(coord.first)+abs(coord.second);
}