Skip to content

Added task 3001 #1690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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));
}
}