Skip to content

Commit f3c963f

Browse files
authored
Added task 3001
1 parent a33b060 commit f3c963f

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3001_3100.s3001_minimum_moves_to_capture_the_queen
2+
3+
// #Medium #Array #Enumeration #2024_02_25_Time_128_ms_(94.59%)_Space_34_MB_(48.65%)
4+
5+
import kotlin.math.abs
6+
7+
class Solution {
8+
fun minMovesToCaptureTheQueen(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int): Int {
9+
if (a == e || b == f) {
10+
if (a == c && (d > b && d < f || d > f && d < b)) {
11+
return 2
12+
}
13+
if (b == d && (c > a && c < e || c > e && c < a)) {
14+
return 2
15+
}
16+
return 1
17+
} else if (abs(c - e) == abs(d - f)) {
18+
if (abs(a - c) == abs(b - d) &&
19+
abs(e - a) == abs(f - b) &&
20+
(a > e && a < c || a > c && a < e)
21+
) {
22+
return 2
23+
}
24+
return 1
25+
}
26+
return 2
27+
}
28+
}
Lines changed: 51 additions & 0 deletions
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.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package g3001_3100.s3001_minimum_moves_to_capture_the_queen
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun minMovesToCaptureTheQueen() {
10+
assertThat(Solution().minMovesToCaptureTheQueen(1, 1, 8, 8, 2, 3), equalTo(2))
11+
}
12+
13+
@Test
14+
fun minMovesToCaptureTheQueen2() {
15+
assertThat(Solution().minMovesToCaptureTheQueen(5, 3, 3, 4, 5, 2), equalTo(1))
16+
}
17+
18+
@Test
19+
fun minMovesToCaptureTheQueen3() {
20+
assertThat(Solution().minMovesToCaptureTheQueen(1, 1, 3, 1, 5, 1), equalTo(2))
21+
}
22+
23+
@Test
24+
fun minMovesToCaptureTheQueen4() {
25+
assertThat(Solution().minMovesToCaptureTheQueen(1, 1, 1, 3, 1, 5), equalTo(2))
26+
}
27+
28+
@Test
29+
fun minMovesToCaptureTheQueen5() {
30+
assertThat(Solution().minMovesToCaptureTheQueen(1, 1, 3, 3, 5, 5), equalTo(1))
31+
}
32+
33+
@Test
34+
fun minMovesToCaptureTheQueen6() {
35+
assertThat(Solution().minMovesToCaptureTheQueen(1, 1, 3, 1, 5, 3), equalTo(1))
36+
}
37+
38+
@Test
39+
fun minMovesToCaptureTheQueen7() {
40+
assertThat(Solution().minMovesToCaptureTheQueen(1, 1, 1, 3, 3, 5), equalTo(1))
41+
}
42+
43+
@Test
44+
fun minMovesToCaptureTheQueen8() {
45+
assertThat(Solution().minMovesToCaptureTheQueen(1, 1, 3, 3, 5, 1), equalTo(1))
46+
}
47+
48+
@Test
49+
fun minMovesToCaptureTheQueen9() {
50+
assertThat(Solution().minMovesToCaptureTheQueen(1, 1, 2, 3, 5, 5), equalTo(2))
51+
}
52+
}

0 commit comments

Comments
 (0)