Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigDecimal;
- 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();
- double result = processCommands(robot, commands, obstacles);
- System.out.println(result);
- }
- public static double processCommands(Robot robot, List<Command> commands, List<int[]> obstacles) {
- double maxEuclideanDistance = 0;
- //robot initially 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 = 0;
- int projectedY = 0;
- 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;
- }
- }
- }
- //System.out.println("finalX : " + finalX + " finalY: " + finalY);
- robot.setLocation(finalX, finalY);
- if(!metObstacle) {
- robot.setLocation(projectedX, projectedY);
- }
- }
- //compute distance from origin after move
- double expr = Math.pow(robot.getCurrentX(), 2) + Math.pow(robot.getCurrentY(), 2);
- double currentDistance = Math.sqrt(expr);
- //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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement