Skip to content

3001. Minimum Moves to Capture The Queen 👎

  • Time: $O(1)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
 public:
  int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
    // The rook is in the same row as the queen.
    if (a == e)
      // The bishop blocks the rook or not.
      return (c == a && (b < d && d < f || b > d && d > f)) ? 2 : 1;
    // The rook is in the same column as the queen.
    if (b == f)
      // The bishop blocks the rook or not.
      return (d == f && (a < c && c < e || a > c && c > e)) ? 2 : 1;
    // The bishop is in the same up-diagonal as the queen.
    if (c + d == e + f)
      // The rook blocks the bishop or not.
      return (a + b == c + d && (c < a && a < e || c > a && a > e)) ? 2 : 1;
    // The bishop is in the same down-diagonal as the queen.
    if (c - d == e - f)
      // The rook blocks the bishop or not.
      return (a - b == c - d && (c < a && a < e || c > a && a > e)) ? 2 : 1;
    // The rook can always get the green in two steps.
    return 2;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
  public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
    // The rook is in the same row as the queen.
    if (a == e)
      // The bishop blocks the rook or not.
      return (c == a && (b < d && d < f || b > d && d > f)) ? 2 : 1;
    // The rook is in the same column as the queen.
    if (b == f)
      // The bishop blocks the rook or not.
      return (d == f && (a < c && c < e || a > c && c > e)) ? 2 : 1;
    // The bishop is in the same up-diagonal as the queen.
    if (c + d == e + f)
      // The rook blocks the bishop or not.
      return (a + b == c + d && (c < a && a < e || c > a && a > e)) ? 2 : 1;
    // The bishop is in the same down-diagonal as the queen.
    if (c - d == e - f)
      // The rook blocks the bishop or not.
      return (a - b == c - d && (c < a && a < e || c > a && a > e)) ? 2 : 1;
    // The rook can always get the green in two steps.
    return 2;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
  def minMovesToCaptureTheQueen(
      self, a: int, b: int, c: int, d: int, e: int, f: int,
  ) -> int:
    # The rook is in the same row as the queen.
    if a == e:
      # The bishop blocks the rook or not.
      return 2 if c == a and (b < d < f or b > d > f) else 1
    # The rook is in the same column as the queen.
    if b == f:
      # The bishop blocks the rook or not.
      return 2 if d == f and (a < c < e or a > c > e) else 1
    # The bishop is in the same up-diagonal as the queen.
    if c + d == e + f:
      # The rook blocks the bishop or not.
      return 2 if a + b == c + d and (c < a < e or c > a > e) else 1
    # The bishop is in the same down-diagonal as the queen.
    if c - d == e - f:
      # The rook blocks the bishop or not.
      return 2 if a - b == c - d and (c < a < e or c > a > e) else 1
    # The rook can always get the green in two steps.
    return 2