Skip to content

Commit 5570e51

Browse files
authored
Added task 3001
1 parent 4fb05e4 commit 5570e51

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3001_3100.s3001_minimum_moves_to_capture_the_queen;
2+
3+
// #Medium #Array #Enumeration #2024_02_25_Time_0_ms_(100.00%)_Space_40.7_MB_(78.00%)
4+
5+
public class Solution {
6+
public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
7+
if (a == e || b == f) {
8+
if (a == c && ((d > b && d < f) || (d > f && d < b))) {
9+
return 2;
10+
}
11+
if (b == d && ((c > a && c < e) || (c > e && c < a))) {
12+
return 2;
13+
}
14+
return 1;
15+
} else if (Math.abs(c - e) == Math.abs(d - f)) {
16+
if (Math.abs(a - c) == Math.abs(b - d)
17+
&& Math.abs(e - a) == Math.abs(f - b)
18+
&& ((a > e && a < c) || (a > c && a < e))) {
19+
return 2;
20+
}
21+
return 1;
22+
}
23+
return 2;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
3001\. Minimum Moves to Capture The Queen
2+
3+
Medium
4+
5+
There is a **1-indexed** `8 x 8` chessboard containing `3` pieces.
6+
7+
You are given `6` integers `a`, `b`, `c`, `d`, `e`, and `f` where:
8+
9+
* `(a, b)` denotes the position of the white rook.
10+
* `(c, d)` denotes the position of the white bishop.
11+
* `(e, f)` denotes the position of the black queen.
12+
13+
Given that you can only move the white pieces, return _the **minimum** number of moves required to capture the black queen_.
14+
15+
**Note** that:
16+
17+
* Rooks can move any number of squares either vertically or horizontally, but cannot jump over other pieces.
18+
* Bishops can move any number of squares diagonally, but cannot jump over other pieces.
19+
* A rook or a bishop can capture the queen if it is located in a square that they can move to.
20+
* The queen does not move.
21+
22+
**Example 1:**
23+
24+
![](https://assets.leetcode.com/uploads/2023/12/21/ex1.png)
25+
26+
**Input:** a = 1, b = 1, c = 8, d = 8, e = 2, f = 3
27+
28+
**Output:** 2
29+
30+
**Explanation:** We can capture the black queen in two moves by moving the white rook to (1, 3) then to (2, 3).
31+
32+
It is impossible to capture the black queen in less than two moves since it is not being attacked by any of the pieces at the beginning.
33+
34+
**Example 2:**
35+
36+
![](https://assets.leetcode.com/uploads/2023/12/21/ex2.png)
37+
38+
**Input:** a = 5, b = 3, c = 3, d = 4, e = 5, f = 2
39+
40+
**Output:** 1
41+
42+
**Explanation:** We can capture the black queen in a single move by doing one of the following:
43+
44+
- Move the white rook to (5, 2).
45+
46+
- Move the white bishop to (5, 2).
47+
48+
**Constraints:**
49+
50+
* `1 <= a, b, c, d, e, f <= 8`
51+
* No two pieces are on the same square.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g3001_3100.s3001_minimum_moves_to_capture_the_queen;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minMovesToCaptureTheQueen() {
11+
assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 8, 8, 2, 3), equalTo(2));
12+
}
13+
14+
@Test
15+
void minMovesToCaptureTheQueen2() {
16+
assertThat(new Solution().minMovesToCaptureTheQueen(5, 3, 3, 4, 5, 2), equalTo(1));
17+
}
18+
19+
@Test
20+
void minMovesToCaptureTheQueen3() {
21+
assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 1, 5, 1), equalTo(2));
22+
}
23+
24+
@Test
25+
void minMovesToCaptureTheQueen4() {
26+
assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 1, 3, 1, 5), equalTo(2));
27+
}
28+
29+
@Test
30+
void minMovesToCaptureTheQueen5() {
31+
assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 3, 5, 5), equalTo(1));
32+
}
33+
34+
@Test
35+
void minMovesToCaptureTheQueen6() {
36+
assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 1, 5, 3), equalTo(1));
37+
}
38+
39+
@Test
40+
void minMovesToCaptureTheQueen7() {
41+
assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 1, 3, 3, 5), equalTo(1));
42+
}
43+
44+
@Test
45+
void minMovesToCaptureTheQueen8() {
46+
assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 3, 5, 1), equalTo(1));
47+
}
48+
49+
@Test
50+
void minMovesToCaptureTheQueen9() {
51+
assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 2, 3, 5, 5), equalTo(2));
52+
}
53+
}

0 commit comments

Comments
 (0)