Created
March 17, 2019 19:25
-
-
Save anuragkalra/6e3d70ec285e11902ed938076cdfba8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Arrays; | |
| import java.util.HashSet; | |
| import java.util.LinkedList; | |
| import java.util.List; | |
| import java.util.Scanner; | |
| import java.util.Set; | |
| public class Solution { | |
| private static final Scanner scanner = new Scanner(System.in); | |
| public static void main(String[] args) { | |
| int obstaclesCount = scanner.nextInt(); | |
| int commandsCount = scanner.nextInt(); | |
| scanner.nextLine(); | |
| //collect obstacles | |
| List<int[]> obstacles = new LinkedList<>(); | |
| for(int i = 0; i < obstaclesCount; i++) { | |
| String[] ob = scanner.nextLine().split(" "); | |
| int x = Integer.parseInt(ob[0]); | |
| int y = Integer.parseInt(ob[1]); | |
| obstacles.add(new int[] {x, y}); | |
| } | |
| //collect commands | |
| List<Command> commands = new LinkedList<>(); | |
| String commandLine; | |
| for(int i = 0; i < commandsCount; i++) { | |
| commandLine = scanner.nextLine(); | |
| //Moves | |
| if(commandLine.length() > 1) { | |
| String moveVal = commandLine.substring(2); | |
| commands.add(new Move(Integer.parseInt(moveVal))); | |
| } | |
| //Rotations | |
| else{ | |
| if(commandLine.charAt(0) == 'L') { | |
| commands.add(new Rotate(0)); | |
| }else { | |
| commands.add(new Rotate(1)); | |
| } | |
| } | |
| } | |
| Robot robot = new Robot(); | |
| float result = processCommands(robot, commands, obstacles); | |
| } | |
| public static float processCommands(Robot robot, List<Command> commands, List<int[]> obstacles) { | |
| float maxEuclideanDistance = 0; | |
| //robot at (0,0), pointing North | |
| for(Command c: commands) { | |
| //update direction | |
| if(c instanceof Rotate) { | |
| if(((Rotate) c).direction == 0) { //turn left | |
| int newDirection = (robot.getDirection() - 1 + 4) % 4; | |
| robot.setDirection(newDirection); | |
| } | |
| if(((Rotate) c).direction == 1) { //turn right | |
| int newDirection = robot.getDirection() + 1 % 4; | |
| robot.setDirection(newDirection); | |
| } | |
| } | |
| //update location after move | |
| else if(c instanceof Move) { | |
| int dir = robot.getDirection(); | |
| int projectedX, projectedY; | |
| boolean metObstacle = false; | |
| int finalX = robot.getCurrentX(); | |
| int finalY = robot.getCurrentY(); | |
| //calculate PROJECTED location and FINAL location after move | |
| if(dir == 0) { //North | |
| projectedX = robot.getCurrentX(); | |
| projectedY = robot.getCurrentY() + ((Move) c).getDistance(); | |
| for(int i = robot.getCurrentY(); i < projectedY; i++) { | |
| int[] coord = new int[] {robot.getCurrentX(), i}; | |
| for(int[] obstacle: obstacles) { | |
| if(Arrays.equals(obstacle, coord)) { | |
| metObstacle = true; | |
| } | |
| } | |
| if(metObstacle) { | |
| finalX = robot.getCurrentX(); | |
| finalY = i - 1; | |
| break; | |
| } | |
| } | |
| }else if (dir == 1) { //East | |
| projectedX = robot.getCurrentX() + ((Move) c).getDistance(); | |
| projectedY = robot.getCurrentY(); | |
| for(int i = robot.getCurrentX(); i < projectedX; i++) { | |
| int[] coord = new int[] {i, robot.getCurrentY()}; | |
| for(int[] obstacle: obstacles) { | |
| if(Arrays.equals(obstacle, coord)) { | |
| metObstacle = true; | |
| } | |
| } | |
| if(metObstacle) { | |
| finalX = i - 1; | |
| finalY = robot.getCurrentY(); | |
| break; | |
| } | |
| } | |
| }else if (dir == 2) { //South | |
| projectedX = robot.getCurrentX(); | |
| projectedY = robot.getCurrentY() - ((Move) c).getDistance(); | |
| for(int i = robot.getCurrentY(); i > projectedY; i--) { | |
| int[] coord = new int[] {robot.getCurrentX(), i}; | |
| for(int[] obstacle: obstacles) { | |
| if(Arrays.equals(obstacle, coord)) { | |
| metObstacle = true; | |
| } | |
| } | |
| if(metObstacle) { | |
| finalX = robot.getCurrentX(); | |
| finalY = i + 1; | |
| break; | |
| } | |
| } | |
| }else if (dir == 3) { //West | |
| projectedX = robot.getCurrentX() - ((Move) c).getDistance(); | |
| projectedY = robot.getCurrentY(); | |
| //check if obstacle encountered | |
| for(int i = robot.getCurrentX(); i > projectedX; i--) { | |
| int[] coord = new int[] {i, robot.getCurrentY()}; | |
| for(int[] obstacle: obstacles) { | |
| if(Arrays.equals(obstacle, coord)) { | |
| metObstacle = true; | |
| } | |
| } | |
| if(metObstacle) { | |
| finalX = i + 1; | |
| finalY = robot.getCurrentY(); | |
| break; | |
| } | |
| } | |
| } | |
| //calculate real location | |
| System.out.println(finalX); | |
| System.out.println(finalY); | |
| System.out.println("finalX : " + finalX + " finalY: " + finalY); | |
| //set robot's real location | |
| robot.setLocation(finalX, finalY); | |
| } | |
| //compute distance from origin after move | |
| float currentDistance = 0; | |
| //update maxEuclideanDistance | |
| maxEuclideanDistance = Math.max(currentDistance, maxEuclideanDistance); | |
| } | |
| return maxEuclideanDistance; | |
| } | |
| } | |
| class Robot { | |
| int currentX; | |
| int currentY; | |
| int direction = 0; //0 = North, 1 = East, 2 = South, 3 = West | |
| public Robot() { | |
| } | |
| public int getCurrentX() { | |
| return this.currentX; | |
| } | |
| public int getCurrentY() { | |
| return this.currentY; | |
| } | |
| public void setLocation(int x, int y) { | |
| this.currentX = x; | |
| this.currentY = y; | |
| } | |
| public int getDirection() { | |
| return this.direction; | |
| } | |
| public void setDirection(int direction) { | |
| this.direction = direction; | |
| } | |
| } | |
| class Command { | |
| } | |
| class Move extends Command { | |
| int distance; | |
| public Move(int distance) { | |
| this.distance = distance; | |
| } | |
| public int getDistance() { | |
| return this.distance; | |
| } | |
| } | |
| class Rotate extends Command { | |
| int direction; //0 = Left, 1 = Right | |
| public Rotate(int direction) { | |
| this.direction = direction; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment