在Java中检查机器人的给定移动顺序是否为圆形

问题描述 投票:1回答:1

我正在做以下任务:

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"

我找到了这个任务的解决方案:

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";
        }
    }
}

一切似乎都很完美,但我无法理解这个条件:

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

能帮助我理解为什么我们需要为这种情况返回NO&& (((x-initialX)*(x-initialX) + (y-initialY)*(y-initialY)) > 0公式表示什么?以及为什么条件只检查方向“北”而不是其他方向。

java algorithm geometry
1个回答
1
投票

最后一个条件可以缩短为:

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

这种情况意味着在给定的步骤序列之后,机器人具有初始方向而不是初始位置。在这种情况下,该序列通过(x, y)移动机器人的位置。这意味着在n重复这个序列机器人将有位置(n*x, n*y)。当x != 0 || y != 0时,这不受任何半径的约束。

xy都等于零时,机器人在给定的步骤序列之后具有初始方向和初始位置。这是一个循环,答案是“是”。

否则,在步骤序列之后机器人的方向已经改变。有两种可能性:

  1. 方向已改为相反(south)。让我们假设在步骤序列之后机器人转移到坐标(x, y)。但是当我们在相反方向重复这个序列时,坐标将被(-x, -y)改变,我们将返回到初始方向的(0, 0)坐标。
  2. 方向已改为westeast。在这种情况下,我们将在四个步骤序列之后以初始方向返回初始位置。例如,让我们假设方向在第一个序列之后变为east并且位置是(x, y)Direction | Coordinates -----------+-------------- north | (0, 0) east | (x, y) south | (x+y, y-x) west | (y, -x) north | (0, 0)
© www.soinside.com 2019 - 2024. All rights reserved.