PlasmaVNGaming

Untitled

Apr 21st, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. enum Dir {
  8. UP = 0,
  9. LEFT = 1,
  10. DOWN = 2,
  11. RIGHT = 3
  12. };
  13. const Dir DA[4] = {UP, LEFT, DOWN, RIGHT};
  14. struct Path {
  15. int value[4];
  16. };
  17. struct Robot {
  18. int x, y;
  19. Dir d;
  20. };
  21. typedef vector< vector<Path> > matrix;
  22. int n, s;
  23. matrix a;
  24. Robot r;
  25.  
  26. void sv(int x, int y, Path p) {
  27. a.at(x).at(y) = p;
  28. }
  29.  
  30. void sv(int x, int y, Dir d, int value) {
  31. a.at(x).at(y).value[d] = value;
  32. }
  33.  
  34. Path v(int x, int y) {
  35. return a.at(x).at(y);
  36. }
  37.  
  38. int v(int x, int y, Dir d) {
  39. return v(x, y).value[d];
  40. }
  41.  
  42. int v(const Robot& r) {
  43. return v(r.x, r.y, r.d);
  44. }
  45.  
  46. pair<int, int> go(const Robot& r, int go) {
  47. int x = r.x;
  48. int y = r.y;
  49. switch (r.d) {
  50. case UP:
  51. y-=go;
  52. break;
  53. case LEFT:
  54. x-=go;
  55. break;
  56. case DOWN:
  57. y+=go;
  58. break;
  59. case RIGHT:
  60. x+=go;
  61. break;
  62. }
  63. return make_pair(x, y);
  64. }
  65.  
  66. bool isBlock(int x, int y) {
  67. return v(x, y, UP) == -1;
  68. }
  69.  
  70. bool nextOut(const Robot& r) {
  71. switch (r.d) {
  72. case UP: return r.y == 0;
  73. case LEFT: return r.x == 0;
  74. case DOWN: return r.y == n;
  75. case RIGHT: return r.x == n;
  76. }
  77. }
  78.  
  79. void display(Dir d) {
  80. for (int i = 0; i < n; i++) {
  81. for (int j = 0; j < n; j++) {
  82. cout << v(i, j, d) << " ";
  83. }
  84. cout << endl;
  85. }
  86. }
  87.  
  88. //cột x dòng y
  89. int main()
  90. {
  91. ifstream in;
  92. in.open("robot.inp");
  93. in >> n >> r.x >> r.y >> s;
  94. r.d = UP;
  95.  
  96. a.resize(n, vector<Path> (n, {0, 0, 0, 0}));
  97. for (int j = 0; j < n; j++) {
  98. for (int i = 0; i < n; i++) {
  99. char c;
  100. in >> c;
  101. //cout << c << " ";
  102. if (c == '#') {
  103. sv(i, j, {-1, -1, -1, -1});
  104. } else {
  105. int up = (j == 0) ? 0 : v(i, j - 1, UP) + 1;
  106. int left = (i == 0) ? 0 : v(i - 1, j, LEFT) + 1;
  107. if (i > 0) {
  108. int right = v(i - 1, j, RIGHT);
  109. if (right > -1) sv(i - 1, j, RIGHT, right + 1);
  110. }
  111. if (j > 0) {
  112. int down = v(i, j - 1, DOWN);
  113. if (down > -1) sv(i, j - 1, DOWN, down + 1);
  114. }
  115.  
  116. //down = (j == 0) ? 0 : v(i, j - 1).value[1] + 1;
  117. //right = (i == n - 1) ? 0 : v(i - 1, j).value[2] + 1;
  118. //down = (j == n - 1) ? 0 : v(i, j - 1).down + 1;
  119. sv(i, j, {up, left, 0, 0});
  120. }
  121. }
  122. cout << endl;
  123. }
  124.  
  125. in.close();
  126.  
  127. /*cout << "UP" << endl;
  128. display(UP);
  129. cout << "LEFT" << endl;
  130. display(LEFT);
  131. cout << "DOWN" << endl;
  132. display(DOWN);
  133. cout << "RIGHT" << endl;
  134. display(RIGHT);*/
  135.  
  136. n--;
  137. r.x--;
  138. r.y--;
  139. if (r.x < 0 || r.x > n || r.y < 0) {
  140. r.y+=s;
  141. s=0;
  142. } else if (r.y > n) {
  143. bool stop = v(r.x, n, UP) == -1;
  144. int range = r.y - n - stop;
  145. int go = min(range, s);
  146. r.y-=go;
  147. s-=go;
  148. if (s > 0 && stop) {
  149. r.x-=s;
  150. s=0;
  151. r.d = DA[(r.d + 1)%4];
  152. }
  153. }
  154. while (s > 0) {
  155. if (nextOut(r)) {
  156. pair<int, int> go_res = go(r, s);
  157. r.x = go_res.first;
  158. r.y = go_res.second;
  159. break;
  160. }
  161. bool block = true;
  162. while (block) {
  163. pair<int, int> next_to = go(r, 1);
  164. block = isBlock(next_to.first, next_to.second);
  165. if (block) r.d = DA[(r.d + 1) % 4];
  166. }
  167. int go_range = min(v(r.x, r.y, r.d), s);
  168. pair<int, int> go_res = go(r, go_range);
  169. r.x = go_res.first;
  170. r.y = go_res.second;
  171. s-=go_range;
  172. }
  173.  
  174. ofstream out;
  175. out.open("robot.out");
  176. out << r.x + 1 << " " << r.y + 1;
  177. //cout << r.x + 1 << " " << r.y + 1;
  178. out.close();
  179.  
  180.  
  181. return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment