diff --git a/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/Solution.java b/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/Solution.java new file mode 100644 index 000000000..b978d34b1 --- /dev/null +++ b/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/Solution.java @@ -0,0 +1,25 @@ +package g3001_3100.s3001_minimum_moves_to_capture_the_queen; + +// #Medium #Array #Enumeration #2024_02_25_Time_0_ms_(100.00%)_Space_40.7_MB_(78.00%) + +public class Solution { + public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { + if (a == e || b == f) { + if (a == c && ((d > b && d < f) || (d > f && d < b))) { + return 2; + } + if (b == d && ((c > a && c < e) || (c > e && c < a))) { + return 2; + } + return 1; + } else if (Math.abs(c - e) == Math.abs(d - f)) { + if (Math.abs(a - c) == Math.abs(b - d) + && Math.abs(e - a) == Math.abs(f - b) + && ((a > e && a < c) || (a > c && a < e))) { + return 2; + } + return 1; + } + return 2; + } +} diff --git a/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/readme.md b/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/readme.md new file mode 100644 index 000000000..4978cdba0 --- /dev/null +++ b/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/readme.md @@ -0,0 +1,51 @@ +3001\. Minimum Moves to Capture The Queen + +Medium + +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. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2023/12/21/ex1.png) + +**Input:** a = 1, b = 1, c = 8, d = 8, e = 2, f = 3 + +**Output:** 2 + +**Explanation:** We can capture the black queen in two moves by moving the white rook to (1, 3) then to (2, 3). + +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. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2023/12/21/ex2.png) + +**Input:** a = 5, b = 3, c = 3, d = 4, e = 5, f = 2 + +**Output:** 1 + +**Explanation:** We can capture the black queen in a single move by doing one of the following: + +- Move the white rook to (5, 2). + +- Move the white bishop to (5, 2). + +**Constraints:** + +* `1 <= a, b, c, d, e, f <= 8` +* No two pieces are on the same square. \ No newline at end of file diff --git a/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java b/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java new file mode 100644 index 000000000..cd9769ca3 --- /dev/null +++ b/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java @@ -0,0 +1,53 @@ +package g3001_3100.s3001_minimum_moves_to_capture_the_queen; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minMovesToCaptureTheQueen() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 8, 8, 2, 3), equalTo(2)); + } + + @Test + void minMovesToCaptureTheQueen2() { + assertThat(new Solution().minMovesToCaptureTheQueen(5, 3, 3, 4, 5, 2), equalTo(1)); + } + + @Test + void minMovesToCaptureTheQueen3() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 1, 5, 1), equalTo(2)); + } + + @Test + void minMovesToCaptureTheQueen4() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 1, 3, 1, 5), equalTo(2)); + } + + @Test + void minMovesToCaptureTheQueen5() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 3, 5, 5), equalTo(1)); + } + + @Test + void minMovesToCaptureTheQueen6() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 1, 5, 3), equalTo(1)); + } + + @Test + void minMovesToCaptureTheQueen7() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 1, 3, 3, 5), equalTo(1)); + } + + @Test + void minMovesToCaptureTheQueen8() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 3, 5, 1), equalTo(1)); + } + + @Test + void minMovesToCaptureTheQueen9() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 2, 3, 5, 5), equalTo(2)); + } +}