Learner
Learner

Reputation: 21425

Check if a given sequence of moves for a robot is circular or not in Java

I am working on below task:

Given a sequence of moves for a robot, check if the sequence is circular or not. A sequence of moves is circular if first and last positions of robot are same. 
A move can be on of the following.

  G - Go one unit
  L - Turn left
  R - Turn right 

Examples:

Input: path[] = "GLGLGLG"
Output: Given sequence of moves is circular 

Input: path[] = "GLLG"
Output: Given sequence of moves is circular 

The movements described in the input string are repeated for an infinite time. Your task is to find if there exists a circle, whose radius is some positive real number R,
such that the robot never leaves it. If such a circle exists return "YES" otherwise "NO"

I found a solution for this task :

public class Circle {


    String check(String commands) {

        int initialX = 0;
        int initialY = 0;

        int x = 0;
        int y = 0;
        String direction = "north";

        for (int i = 0; i < commands.length(); i++) {

            if (direction.equals("north")) {
                if (commands.charAt(i) == 'G') {
                    y++;
                } else if (commands.charAt(i) == 'L') {
                    direction = "west";
                } else if (commands.charAt(i) == 'R') {
                    direction = "east";
                } else {
                    System.out.println("Wrong command");
                }
            } else if (direction.equals("east")) {
                if (commands.charAt(i) == 'G') {
                    x++;
                } else if (commands.charAt(i) == 'L') {
                    direction = "north";
                } else if (commands.charAt(i) == 'R') {
                    direction = "south";
                } else {
                    System.out.println("Wrong command");
                }
            } else if (direction.equals("south")) {
                if (commands.charAt(i) == 'G') {
                    y--;
                } else if (commands.charAt(i) == 'L') {
                    direction = "east";
                } else if (commands.charAt(i) == 'R') {
                    direction = "west";
                } else {
                    System.out.println("Wrong command");
                }
            } else if (direction.equals("west")) {
                if (commands.charAt(i) == 'G') {
                    x--;
                } else if (commands.charAt(i) == 'L') {
                    direction = "south";
                } else if (commands.charAt(i) == 'R') {
                    direction = "north";
                } else {
                    System.out.println("Wrong command");
                }
            }
        }

        if (direction.equals("north") && (((x-initialX)*(x-initialX) + (y-initialY)*(y-initialY)) > 0)) {
            return "NO";
        } else {
            return "YES";
        }
    }
}

Everything seems perfect, but I am not able to understand the condition:

if (direction.equals("north") && (((x-initialX)*(x-initialX) + (y-initialY)*(y-initialY)) > 0))

Can you please help me in understanding why we need to return NO for this case, what the formula && (((x-initialX)*(x-initialX) + (y-initialY)*(y-initialY)) > 0 indicates? and why the condition is checking only for direction "north" and not for other directions.

Upvotes: 1

Views: 2137

Answers (1)

DAle
DAle

Reputation: 9127

The last condition could be shortened to:

if (direction.equals("north") && (x != 0 || y != 0))

This condition means that after given sequence of steps robot has initial direction but not the initial position. In this case, this sequence shifts the position of the robot by (x, y). That means that after n repetitions of this sequence robot will have position (n*x, n*y). That is not bound by any radius when x != 0 || y != 0.

When both x and y are equal to zero, the robot has the initial direction and initial position after the given sequence of steps. That is a cycle and the answer is "Yes".

Otherwise, the direction of the robot has changed after the sequence of steps. There are two possibilities:

  1. The direction has changed to the opposite (south). Let's assume that after the sequence of steps robot shifted to coordinates (x, y). But when we repeat this sequence in opposite direction coordinates will be changed by (-x, -y) and we will return to the (0, 0) coordinates with the initial direction.
  2. The direction has changed to west or east. In this case, we will return to the initial position with the initial direction after four sequences of steps. For example, let's assume that direction was changed to east and position is (x, y) after the first sequence then:

    Direction  |  Coordinates
    -----------+--------------
    north      |  (0, 0)
    east       |  (x, y)
    south      |  (x+y, y-x)
    west       |  (y, -x)
    north      |  (0, 0)
    

Upvotes: 1

Related Questions