fork(1) download
  1. // your code goes here
  2. importPackage(java.io);
  3. importPackage(java.lang);
  4.  
  5. var whereAboutUs = [[0,0]];
  6. var currentposition_x = 0;
  7. var currentposition_y = 0;
  8. var currentDirection = "S";
  9. var currentposition = "e8";
  10. var arr = ["a","b","c","d","e","f","g","h"];
  11.  
  12. function changeDirection(Direction) {
  13. switch(Direction){
  14. case "N":
  15. currentDirection = "N";
  16. break;
  17. case "S":
  18. currentDirection = "S";
  19. break;
  20. case "E":
  21. currentDirection = "E";
  22. break;
  23. case "W":
  24. currentDirection = "W";
  25. break;
  26. case "SE":
  27. currentDirection = "SE";
  28. break;
  29. case "SW":
  30. currentDirection = "SW";
  31. break;
  32. case "NE":
  33. currentDirection = "NE";
  34. break;
  35. case "NW":
  36. currentDirection = "NW";
  37. break;
  38. }
  39. }
  40. function moveForward() {
  41. switch(currentDirection){
  42. case "N":
  43. if(currentposition_x - 1 < 0){
  44. return "error";
  45. }
  46. currentposition_x = currentposition_x - 1;
  47. break;
  48. case "S":
  49. if(currentposition_x + 1 > 7){
  50. return "error";
  51. }
  52. currentposition_x = currentposition_x + 1;
  53. break;
  54. case "E":
  55. if(currentposition_y + 1 > 7){
  56. return "error";
  57. }
  58. currentposition_y = currentposition_y + 1;
  59. break;
  60. case "W":
  61. if(currentposition_y - 1 < 0){
  62. return "error";
  63. }
  64. currentposition_y = currentposition_y - 1;
  65. break;
  66. case "SE":
  67. if(currentposition_x + 1 > 7 || currentposition_y + 1 > 7){
  68. return "error";
  69. }
  70. currentposition_x = currentposition_x + 1;
  71. currentposition_y = currentposition_y + 1;
  72. break;
  73. case "SW":
  74. if(currentposition_x + 1 > 7 || currentposition_y - 1 < 0){
  75. return "error";
  76. }
  77. currentposition_x = currentposition_x + 1;
  78. currentposition_y = currentposition_y - 1;
  79. break;
  80. case "NE":
  81. if(currentposition_x - 1 < 0 || currentposition_y + 1 > 7){
  82. return "error";
  83. }
  84. currentposition_x = currentposition_x - 1;
  85. currentposition_y = currentposition_y + 1;
  86. break;
  87. case "NW":
  88. if(currentposition_x - 1 < 0 || currentposition_y - 1 < 0){
  89. return "error";
  90. }
  91. currentposition_x = currentposition_x - 1;
  92. currentposition_y = currentposition_y - 1;
  93. break;
  94. default:
  95. return "directionerror";
  96.  
  97. }
  98. var position = [currentposition_x,currentposition_y];
  99. whereAboutUs.push(position);
  100. return "updated";
  101. }
  102. function jumpMoveDirection(noOfSteps) {
  103. switch(currentDirection){
  104. case "N":
  105. if(currentposition_x - noOfSteps < 0){
  106. return "error";
  107. }
  108. currentposition_x = currentposition_x - noOfSteps;
  109. break;
  110. case "S":
  111. if(currentposition_x + noOfSteps > 7){
  112. return "error";
  113. }
  114. currentposition_x = currentposition_x + noOfSteps;
  115. break;
  116. case "E":
  117. if(currentposition_y + noOfSteps > 7){
  118. return "error";
  119. }
  120. currentposition_y = currentposition_y + noOfSteps;
  121. break;
  122. case "W":
  123. if(currentposition_y - noOfSteps < 0){
  124. return "error";
  125. }
  126. currentposition_y = currentposition_y - noOfSteps;
  127. break;
  128. case "SE":
  129. if(currentposition_x + noOfSteps > 7 || currentposition_y + noOfSteps > 7){
  130. return "error";
  131. }
  132. currentposition_x = currentposition_x + noOfSteps;
  133. currentposition_y = currentposition_y + noOfSteps;
  134. break;
  135. case "SW":
  136. if(currentposition_x + noOfSteps > 7 || currentposition_y - noOfSteps < 0){
  137. return "error";
  138. }
  139. currentposition_x = currentposition_x + noOfSteps;
  140. currentposition_y = currentposition_y - noOfSteps;
  141. break;
  142. case "NE":
  143. if(currentposition_x - 1 < 0 || currentposition_y + 1 > 7){
  144. return "error";
  145. }
  146. currentposition_x = currentposition_x - 1;
  147. currentposition_y = currentposition_y + 1;
  148. break;
  149. case "NW":
  150. if(currentposition_x - noOfSteps < 0 || currentposition_y - noOfSteps < 0){
  151. return "error";
  152. }
  153. currentposition_x = currentposition_x - noOfSteps;
  154. currentposition_y = currentposition_y - noOfSteps;
  155. break;
  156. default:
  157. return "directionerror";
  158.  
  159. }
  160. var position = [currentposition_x,currentposition_y];
  161. whereAboutUs.push(position);
  162. return "updated";
  163. }
  164. function updatePosition() {
  165. currentposition = arr[currentposition_y] + (8-currentposition_x);
  166. }
  167.  
  168.  
  169. var stdin= new BufferedReader(new InputStreamReader(System['in']));
  170. var line = stdin.readLine();
  171. print("enter number of testcases")
  172. var t = parseInt(line);
  173. for(var i=0; i<t; i++) {
  174. var input = stdin.readLine();
  175. print("enter direction and number of jumps")
  176. var elements = input.split(" ");
  177. var d = String(elements[0]);
  178. var n = parseInt(elements[1]);
  179. print(d);
  180. print(n);
  181. if(n == 1){
  182. changeDirection(d);
  183. print(moveForward());
  184. updatePosition();
  185. //print(currentposition);
  186. }else{
  187. changeDirection(d);
  188. print(jumpMoveDirection(n));
  189. updatePosition();
  190. //print(currentposition);
  191. }
  192.  
  193. }
Success #stdin #stdout 0.48s 45516KB
stdin
1
W 1
stdout
enter number of testcases
enter direction and number of jumps
W
1
error