Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <string>
  5. #include <utility>
  6. #include <set>
  7. #include <tuple>
  8. using namespace std;
  9.  
  10. enum Oritation
  11. {
  12. North,
  13. East,
  14. South,
  15. West,
  16. };
  17.  
  18. class Robot
  19. {
  20. public:
  21. Robot(const size_t x, const size_t y);
  22. void SetInitPosition(const size_t x, const size_t y, const char orientation);
  23. void SetInstruction(const std::string& instruction);
  24. void Go();
  25. bool MoveOneStep();
  26. bool AdjustNextCommand(char input);
  27.  
  28. std::set<std::pair<std::pair<size_t, size_t>, Oritation>> m_ForbiddenInstruction;
  29.  
  30. private:
  31.  
  32. char PrinOritation(Oritation& input);
  33. std::pair<size_t, size_t> m_map_boundary;
  34. std::pair<size_t, size_t> m_position_now;
  35. Oritation m_orientation_now;
  36. std::string m_Instruction;
  37. };
  38.  
  39. Oritation& operator++(Oritation& now)
  40. {
  41. unsigned int next = now + 1;
  42. if (next == 4)
  43. {
  44. next = 0;
  45. }
  46. now = static_cast<Oritation>(next);
  47. return now;
  48. }
  49.  
  50. Oritation& operator--(Oritation& now)
  51. {
  52. int next = now - 1;
  53. if (next == -1)
  54. {
  55. next = 3;
  56. }
  57. now = static_cast<Oritation>(next);
  58. return now;
  59. }
  60.  
  61.  
  62. Robot::Robot(const size_t x, const size_t y)
  63. : m_map_boundary(std::make_pair(x, y))
  64. {
  65.  
  66. }
  67.  
  68. void Robot::SetInitPosition(const size_t x, const size_t y, const char orientation)
  69. {
  70. m_position_now = std::make_pair(x, y);
  71.  
  72. switch (orientation)
  73. {
  74. case 'N':
  75. m_orientation_now = North;
  76. break;
  77. case 'E':
  78. m_orientation_now = East;
  79. break;
  80. case 'S':
  81. m_orientation_now = South;
  82. break;
  83. case 'W':
  84. m_orientation_now = West;
  85. break;
  86. default:
  87. break;
  88. }
  89. };
  90.  
  91. void Robot::SetInstruction(const std::string& instruction)
  92. {
  93. m_Instruction = instruction;
  94. }
  95.  
  96. void Robot::Go()
  97. {
  98. for (char& command : m_Instruction)
  99. {
  100. if (!AdjustNextCommand(command))
  101. {
  102. cout << m_position_now.first << " " << m_position_now.second << " " << PrinOritation(m_orientation_now) << " LOST" << endl;
  103. m_ForbiddenInstruction.insert(make_pair(m_position_now, m_orientation_now));
  104. return;
  105. }
  106. }
  107. cout << m_position_now.first << " " << m_position_now.second << " " << PrinOritation(m_orientation_now) << endl;
  108. return;
  109. }
  110.  
  111. bool Robot::MoveOneStep()
  112. {
  113. if (m_ForbiddenInstruction.find(make_pair(m_position_now, m_orientation_now)) != m_ForbiddenInstruction.end())
  114. {
  115. return true;
  116. }
  117.  
  118. std::pair<size_t, size_t> m_position_next(m_position_now);
  119. switch (m_orientation_now)
  120. {
  121. case North:
  122. ++m_position_next.second;
  123. break;
  124. case East:
  125. ++m_position_next.first;
  126. break;
  127. case South:
  128. --m_position_next.second;
  129. break;
  130. case West:
  131. --m_position_next.first;
  132. break;
  133. }
  134.  
  135. if ((m_position_next.first > m_map_boundary.first) || (m_position_next.second > m_map_boundary.second))
  136. {
  137. return false;
  138. }
  139. else
  140. {
  141. m_position_now = m_position_next;
  142. return true;
  143. }
  144. }
  145.  
  146. bool Robot::AdjustNextCommand(char input)
  147. {
  148. switch (input)
  149. {
  150. case 'F':
  151. return MoveOneStep();
  152. case 'R':
  153. ++m_orientation_now;
  154. return true;
  155. case 'L':
  156. --m_orientation_now;
  157. return true;
  158. default:
  159. return false;
  160. }
  161. }
  162.  
  163. char Robot::PrinOritation(Oritation & input)
  164. {
  165. switch (input)
  166. {
  167. case North:
  168. return 'N';
  169. case East:
  170. return 'E';
  171. case South:
  172. return 'S';
  173. case West:
  174. return 'W';
  175. default:
  176. return ' ';
  177. }
  178. }
  179.  
  180. int main()
  181. {
  182. using namespace std;
  183. size_t max_X, max_Y;
  184. stringstream ss;
  185.  
  186. ss << cin.rdbuf();
  187. ss >> max_X >> max_Y;
  188.  
  189. Robot CG(max_X, max_Y);
  190.  
  191. while (!ss.eof())
  192. {
  193. size_t initX, initY;
  194. char orientation;
  195. string instruction;
  196.  
  197. ss >> initX >> initY >> orientation;
  198. CG.SetInitPosition(initX, initY, orientation);
  199. ss >> instruction;
  200. CG.SetInstruction(instruction);
  201. CG.Go();
  202. }
  203.  
  204. return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement