Created
April 19, 2020 01:20
-
-
Save duskvirkus/8d4d4a090f6a8f97dbe2889cc05fc9ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Shadows of the Knight - Episode 1 Solution | |
| // Violet Graham | |
| // https://www.codingame.com/training/medium/shadows-of-the-knight-episode-1 | |
| #include <iostream> | |
| #include <string> | |
| using std::cin; | |
| using std::cout; | |
| using std::string; | |
| enum Position { | |
| EXACT = 0, | |
| LOW, | |
| HIGH | |
| }; | |
| class Range { | |
| private: | |
| int m_min; | |
| int m_max; | |
| int m_current; | |
| public: | |
| Range(const int min, const int max, const int current); | |
| ~Range() = default; | |
| Range(const Range&) = delete; | |
| Range(const Range&&) = delete; | |
| Range& operator=(const Range&) = delete; | |
| Range& operator=(const Range&&) = delete; | |
| void update(const Position&); | |
| int predict() const; | |
| }; | |
| Range::Range(const int min, const int max, const int current): | |
| m_min(min), | |
| m_max(max), | |
| m_current(current) | |
| {} | |
| void Range::update(const Position& pos) { | |
| switch(pos) { | |
| case LOW: | |
| m_min = m_current; | |
| break; | |
| case HIGH: | |
| m_max = m_current; | |
| break; | |
| case EXACT: | |
| m_min = m_current; | |
| m_max = m_current; | |
| break; | |
| } | |
| m_current = m_min + (m_max - m_min) / 2; | |
| } | |
| int Range::predict() const { | |
| return m_current; | |
| } | |
| Position calc_pos_y(const string& dir) { | |
| if (dir[0] == 'D') return LOW; | |
| if (dir[0] == 'U') return HIGH; | |
| return EXACT; | |
| } | |
| Position calc_pos_x(const string& dir) { | |
| if (dir[0] == 'R' || dir[1] == 'R') return LOW; | |
| if (dir[0] == 'L' || dir[1] == 'L') return HIGH; | |
| return EXACT; | |
| } | |
| int main() | |
| { | |
| int width; | |
| int height; | |
| cin >> width >> height; cin.ignore(); | |
| short turns; | |
| cin >> turns; cin.ignore(); | |
| int x; | |
| int y; | |
| cin >> x >> y; cin.ignore(); | |
| Range x_range(0, width, x); | |
| Range y_range(0, height, y); | |
| while(1) { | |
| string dir; | |
| cin >> dir; cin.ignore(); | |
| x_range.update(calc_pos_x(dir)); | |
| y_range.update(calc_pos_y(dir)); | |
| cout << x_range.predict() << ' ' << y_range.predict() << '\n'; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment