Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.util.Arrays;
  3. import java.util.HashSet;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.Set;
  8.  
  9. public class Solution {
  10.  
  11. private static final Scanner scanner = new Scanner(System.in);
  12.  
  13. public static void main(String[] args) {
  14. int obstaclesCount = scanner.nextInt();
  15. int commandsCount = scanner.nextInt();
  16.  
  17. scanner.nextLine();
  18.  
  19. //collect obstacles
  20. List<int[]> obstacles = new LinkedList<>();
  21. for(int i = 0; i < obstaclesCount; i++) {
  22. String[] ob = scanner.nextLine().split(" ");
  23. int x = Integer.parseInt(ob[0]);
  24. int y = Integer.parseInt(ob[1]);
  25. obstacles.add(new int[] {x, y});
  26. }
  27.  
  28. //collect commands
  29. List<Command> commands = new LinkedList<>();
  30. String commandLine;
  31. for(int i = 0; i < commandsCount; i++) {
  32. commandLine = scanner.nextLine();
  33. //Moves
  34. if(commandLine.length() > 1) {
  35. String moveVal = commandLine.substring(2);
  36. commands.add(new Move(Integer.parseInt(moveVal)));
  37. }
  38. //Rotations
  39. else{
  40. if(commandLine.charAt(0) == 'L') {
  41. commands.add(new Rotate(0));
  42. }else {
  43. commands.add(new Rotate(1));
  44. }
  45. }
  46. }
  47.  
  48. Robot robot = new Robot();
  49. double result = processCommands(robot, commands, obstacles);
  50. System.out.println(result);
  51.  
  52. }
  53.  
  54.  
  55. public static double processCommands(Robot robot, List<Command> commands, List<int[]> obstacles) {
  56. double maxEuclideanDistance = 0;
  57. //robot initially at (0,0), pointing North
  58. for(Command c: commands) {
  59.  
  60. //update direction
  61. if(c instanceof Rotate) {
  62. if(((Rotate) c).direction == 0) { //turn left
  63. int newDirection = (robot.getDirection() - 1 + 4) % 4;
  64. robot.setDirection(newDirection);
  65. }
  66. if(((Rotate) c).direction == 1) { //turn right
  67. int newDirection = robot.getDirection() + 1 % 4;
  68. robot.setDirection(newDirection);
  69. }
  70. }
  71.  
  72. //update location after move
  73. else if(c instanceof Move) {
  74. int dir = robot.getDirection();
  75. int projectedX = 0;
  76. int projectedY = 0;
  77. boolean metObstacle = false;
  78. int finalX = robot.getCurrentX();
  79. int finalY = robot.getCurrentY();
  80.  
  81. //calculate PROJECTED location and FINAL location after move
  82. if(dir == 0) { //North
  83. projectedX = robot.getCurrentX();
  84. projectedY = robot.getCurrentY() + ((Move) c).getDistance();
  85.  
  86. for(int i = robot.getCurrentY(); i < projectedY; i++) {
  87. int[] coord = new int[] {robot.getCurrentX(), i};
  88. for(int[] obstacle: obstacles) {
  89. if(Arrays.equals(obstacle, coord)) {
  90. metObstacle = true;
  91. }
  92. }
  93. if(metObstacle) {
  94. finalX = robot.getCurrentX();
  95. finalY = i - 1;
  96. break;
  97. }
  98. }
  99. }else if (dir == 1) { //East
  100. projectedX = robot.getCurrentX() + ((Move) c).getDistance();
  101. projectedY = robot.getCurrentY();
  102.  
  103. for(int i = robot.getCurrentX(); i < projectedX; i++) {
  104. int[] coord = new int[] {i, robot.getCurrentY()};
  105. for(int[] obstacle: obstacles) {
  106. if(Arrays.equals(obstacle, coord)) {
  107. metObstacle = true;
  108. }
  109. }
  110. if(metObstacle) {
  111. finalX = i - 1;
  112. finalY = robot.getCurrentY();
  113. break;
  114. }
  115. }
  116.  
  117. }else if (dir == 2) { //South
  118. projectedX = robot.getCurrentX();
  119. projectedY = robot.getCurrentY() - ((Move) c).getDistance();
  120.  
  121. for(int i = robot.getCurrentY(); i > projectedY; i--) {
  122. int[] coord = new int[] {robot.getCurrentX(), i};
  123. for(int[] obstacle: obstacles) {
  124. if(Arrays.equals(obstacle, coord)) {
  125. metObstacle = true;
  126. }
  127. }
  128. if(metObstacle) {
  129. finalX = robot.getCurrentX();
  130. finalY = i + 1;
  131. break;
  132. }
  133. }
  134.  
  135. }else if (dir == 3) { //West
  136. projectedX = robot.getCurrentX() - ((Move) c).getDistance();
  137. projectedY = robot.getCurrentY();
  138.  
  139. //check if obstacle encountered
  140. for(int i = robot.getCurrentX(); i > projectedX; i--) {
  141. int[] coord = new int[] {i, robot.getCurrentY()};
  142. for(int[] obstacle: obstacles) {
  143. if(Arrays.equals(obstacle, coord)) {
  144. metObstacle = true;
  145. }
  146. }
  147. if(metObstacle) {
  148. finalX = i + 1;
  149. finalY = robot.getCurrentY();
  150. break;
  151. }
  152. }
  153. }
  154.  
  155. //System.out.println("finalX : " + finalX + " finalY: " + finalY);
  156.  
  157. robot.setLocation(finalX, finalY);
  158.  
  159. if(!metObstacle) {
  160. robot.setLocation(projectedX, projectedY);
  161. }
  162.  
  163. }
  164.  
  165. //compute distance from origin after move
  166. double expr = Math.pow(robot.getCurrentX(), 2) + Math.pow(robot.getCurrentY(), 2);
  167. double currentDistance = Math.sqrt(expr);
  168.  
  169. //update maxEuclideanDistance
  170. maxEuclideanDistance = Math.max(currentDistance, maxEuclideanDistance);
  171. }
  172. return maxEuclideanDistance;
  173. }
  174.  
  175. }
  176.  
  177. class Robot {
  178. int currentX;
  179. int currentY;
  180.  
  181. int direction = 0; //0 = North, 1 = East, 2 = South, 3 = West
  182. public Robot() {
  183.  
  184. }
  185.  
  186. public int getCurrentX() {
  187. return this.currentX;
  188. }
  189.  
  190. public int getCurrentY() {
  191. return this.currentY;
  192. }
  193.  
  194. public void setLocation(int x, int y) {
  195. this.currentX = x;
  196. this.currentY = y;
  197. }
  198.  
  199. public int getDirection() {
  200. return this.direction;
  201. }
  202.  
  203. public void setDirection(int direction) {
  204. this.direction = direction;
  205. }
  206. }
  207.  
  208. class Command {
  209.  
  210. }
  211.  
  212. class Move extends Command {
  213. int distance;
  214. public Move(int distance) {
  215. this.distance = distance;
  216. }
  217.  
  218. public int getDistance() {
  219. return this.distance;
  220. }
  221. }
  222.  
  223. class Rotate extends Command {
  224. int direction; //0 = Left, 1 = Right
  225. public Rotate(int direction) {
  226. this.direction = direction;
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement