Advertisement
Guest User

robot programmabile 70/100

a guest
Apr 4th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. struct commandOutput {
  6. pair<int,int> cords;
  7. int dir;
  8. int lastBomb;
  9. };
  10.  
  11. void changeIncr(int dir, int& incrx, int& incry){
  12. switch (dir) {
  13. case 0:
  14. incrx = 1;
  15. incry = 0;
  16. break;
  17. case 1:
  18. incrx = 0;
  19. incry = -1;
  20. break;
  21. case 2:
  22. incrx = -1;
  23. incry = 0;
  24. break;
  25. case 3:
  26. incrx = 0;
  27. incry = 1;
  28. }
  29. }
  30.  
  31. int main() {
  32. ifstream in("input.txt");
  33. ofstream out("output.txt");
  34.  
  35. int N, M, i;
  36. in>>N>>M;
  37.  
  38. commandOutput comms[N];
  39. int last = 0, x = 0, y = 0, dir = 0, incrx = 1, incry = 0;
  40. char command;
  41. for (i = 0; i < N; i++) {
  42. in>>command;
  43. if (command == 'R') {
  44. dir = dir == 3 ? 0 : dir+1;
  45. changeIncr(dir, incrx, incry);
  46. } else if (command == 'L') {
  47. dir = dir == 0 ? 3 : dir-1;
  48. changeIncr(dir, incrx, incry);
  49. } else if (command == 'F') {
  50. x += incrx;
  51. y += incry;
  52. } else if (command == 'B') {
  53. x -= incrx;
  54. y -= incry;
  55. }
  56. comms[i].cords = {x,y};
  57. comms[i].dir = dir;
  58. comms[i].lastBomb = last;
  59. if (command == 'X') {
  60. last = i+1;
  61. }
  62. }
  63.  
  64. int s, e;
  65. bool end = false;
  66. x = 0; y = 0; dir = 0;
  67. for (i = 0; i < M && !end; i++) {
  68. in>>s>>e;
  69. while(s < comms[e].lastBomb) {
  70. e = comms[e].lastBomb-1;
  71. end = true;
  72. }
  73. if(s == 0){
  74. incrx = comms[e].cords.first;
  75. incry = comms[e].cords.second;
  76. last = comms[e].dir;
  77. }else{
  78. incrx = comms[e].cords.first - comms[s-1].cords.first;
  79. incry = comms[e].cords.second - comms[s-1].cords.second;
  80. last = comms[e].dir - comms[s-1].dir;
  81. if(comms[s-1].dir == 1){
  82. int temp = incrx;
  83. incrx = -incry;
  84. incry = temp;
  85. }else if(comms[s-1].dir == 2){
  86. incrx = -incrx;
  87. incry = -incry;
  88. }else if(comms[s-1].dir == 3){
  89. int temp = incrx;
  90. incrx = incry;
  91. incry = -temp;
  92. }
  93. }
  94.  
  95. if(dir == 0){
  96. x += incrx;
  97. y += incry;
  98. }else if(dir == 1){
  99. x += incry;
  100. y -= incrx;
  101. }else if(dir == 2){
  102. x -= incrx;
  103. y -= incry;
  104. }else{
  105. x -= incry;
  106. y += incrx;
  107. }
  108.  
  109. dir += last;
  110. if(dir > 3){
  111. dir-=4;
  112. }
  113. if(dir < 0){
  114. dir+=4;
  115. }
  116. }
  117.  
  118. out<<x<<' '<<y<<'\n';
  119.  
  120. in.close();
  121. out.close();
  122. return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement