[LeetCode] Minimum Moves to Capture The Queen

10036. Minimum Moves to Capture The Queen

There is a 1-indexed 8 x 8 chessboard containing 3 pieces.

You are given 6 integers a, b, c, d, e, and f where:

  • (a, b) denotes the position of the white rook.
  • (c, d) denotes the position of the white bishop.
  • (e, f) denotes the position of the black queen.

Given that you can only move the white pieces, return the minimum number of moves required to capture the black queen.

Note that:

  • Rooks can move any number of squares either vertically or horizontally, but cannot jump over other pieces.
  • Bishops can move any number of squares diagonally, but cannot jump over other pieces.
  • A rook or a bishop can capture the queen if it is located in a square that they can move to.
  • The queen does not move.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
int res = 2;
if(a == e and b == f) res = min(res, 0);
if(c == e and d == f) res = min(res, 0);
if(a == e or b == f) {
if(a == e) {
int mi = min(b,f), ma = max(b,f);
if(a == c) {
if(mi <= d and d <= ma) res = min(res, 2);
else res = min(res, 1);
}
else res = min(res, 1);
}
if(b == f) {
int mi = min(a,e), ma = max(a,e);
if(d == b) {
if(mi <= c and c <= ma) res = min(res, 2);
else res = min(res, 1);
}
else res = min(res, 1);
}
}

if(((e+f) & 1) == ((c + d) & 1)) {
if((c-d) == (e-f)) {
if((a-c) == (b-d)) {
int mi = min(c,e), ma = max(c,e);
if(mi <= a and a <= ma) res = min(res, 2);
else res = min(res, 1);
}
else res = min(res, 1);
}
if((c+d) == (e+f)) {
if((a+b) == (c+d)) {
int mi = min(c,e), ma = max(c,e);
if(mi <= a and a <= ma) res = min(res, 2);
else res = min(res, 1);
} else res = min(res, 1);
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/01/07/PS/LeetCode/minimum-moves-to-capture-the-queen/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.