-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvacuumba.cpp
117 lines (86 loc) · 2.84 KB
/
vacuumba.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
113
114
115
116
117
/**
* @file vacuumba.cpp
* @author William Weston
* @brief Vacuumba Problem From Kattis
* @version 0.1
* @date 2023-07-07
*
* @copyright Copyright (c) 2023
*
* Source: https://open.kattis.com/problems/vacuumba
*/
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <utility>
// ------------------------------------------------------------------------------------------------
struct Point final
{
double x;
double y;
};
auto operator<<( std::ostream& out, Point const& point ) -> std::ostream&
{
out << std::setprecision( 6 ) << std::fixed << point.x << ' ' << point.y;
return out;
}
// ------------------------------------------------------------------------------------------------
class Robot final
{
public:
auto move( double angle, double distance ) -> void;
auto location() const noexcept -> Point;
private:
Point location_ = { 0, 0 };
double direction_ = 90.0; // degrees
auto normalize_direction() -> void;
};
// ------------------------------------------------------------------------------------------------
auto main() -> int
{
auto const test_cases = []() { int n; std::cin >> n; return n; }();
for ( auto current_case = 0; current_case < test_cases; ++current_case )
{
auto const inputs = []() { int m; std::cin >> m; return m; }();
auto vacuumba = Robot();
for ( auto input = 0; input < inputs; ++input )
{
auto const [angle, distance] = []() { double degrees, dist; std::cin >> degrees >> dist; return std::make_pair( degrees, dist ); }();
vacuumba.move( angle, distance );
}
std::cout << vacuumba.location() << '\n';
}
return EXIT_SUCCESS;
}
// ------------------------------------------------------------------------------------------------
auto degrees_to_radians( double degrees ) -> double;
// ------------------------------------------------------------------------------------------------
auto Robot::move( double const angle, double const distance ) -> void
{
direction_ += angle;
normalize_direction();
auto const radians = degrees_to_radians( direction_ );
auto const delta_x = distance * std::cos( radians );
auto const delta_y = distance * std::sin( radians );
location_.x += delta_x;
location_.y += delta_y;
}
auto Robot::location() const noexcept -> Point
{
return location_;
}
auto Robot::normalize_direction() -> void
{
while ( direction_ >= 360.0 )
{
direction_ -= 360.0;
}
}
// ------------------------------------------------------------------------------------------------
auto degrees_to_radians( double const degrees ) -> double
{
static constexpr auto pi = 3.14159265358979323846264338327;
return degrees * pi / 180.0;
}
// ------------------------------------------------------------------------------------------------