From 16a49daeefb05a966de75cae98afc2524ce561c0 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 5 Dec 2024 09:00:55 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3001 --- .../README.md | 206 +++++++++-------- .../README_EN.md | 208 ++++++++++-------- .../Solution.cj | 17 ++ .../Solution.cpp | 32 ++- .../Solution.go | 27 +-- .../Solution.java | 36 ++- .../Solution.py | 23 +- .../Solution.rs | 17 ++ .../Solution.ts | 36 ++- 9 files changed, 318 insertions(+), 284 deletions(-) create mode 100644 solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.cj create mode 100644 solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.rs diff --git a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/README.md b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/README.md index fc82a9351286c..c5dd7682f097b 100644 --- a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/README.md +++ b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/README.md @@ -76,7 +76,17 @@ tags: -### 方法一 +### 方法一:分类讨论 + +根据题意,我们可以将捕获黑皇后的情况分为以下几种: + +1. 白色车和黑皇后在同一行,且中间没有其他棋子,此时只需要移动白色车 $1$ 一次; +1. 白色车和黑皇后在同一列,且中间没有其他棋子,此时只需要移动白色车 $1$ 一次; +1. 白色象和黑皇后在对角线 `\` 上,且中间没有其他棋子,此时只需要移动白色象 $1$ 一次; +1. 白色象和黑皇后在对角线 `/` 上,且中间没有其他棋子,此时只需要移动白色象 $1$ 一次; +1. 其他情况,只需要移动两次。 + +时间复杂度 $O(1)$,空间复杂度 $O(1)$。 @@ -87,50 +97,35 @@ class Solution: def minMovesToCaptureTheQueen( self, a: int, b: int, c: int, d: int, e: int, f: int ) -> int: - def check(dirs, sx, sy, bx, by) -> bool: - for dx, dy in pairwise(dirs): - for k in range(1, 8): - x = sx + dx * k - y = sy + dy * k - if not (1 <= x <= 8 and 1 <= y <= 8) or (x, y) == (bx, by): - break - if (x, y) == (e, f): - return True - return False - - dirs1 = (-1, 0, 1, 0, -1) - dirs2 = (-1, 1, 1, -1, -1) - return 1 if check(dirs1, a, b, c, d) or check(dirs2, c, d, a, b) else 2 + if a == e and (c != a or (d - b) * (d - f) > 0): + return 1 + if b == f and (d != b or (c - a) * (c - e) > 0): + return 1 + if c - e == d - f and (a - e != b - f or (a - c) * (a - e) > 0): + return 1 + if c - e == f - d and (a - e != f - b or (a - c) * (a - e) > 0): + return 1 + return 2 ``` #### Java ```java class Solution { - private final int[] dirs1 = {-1, 0, 1, 0, -1}; - private final int[] dirs2 = {-1, 1, 1, -1, -1}; - private int e, f; - public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { - this.e = e; - this.f = f; - return check(dirs1, a, b, c, d) || check(dirs2, c, d, a, b) ? 1 : 2; - } - - private boolean check(int[] dirs, int sx, int sy, int bx, int by) { - for (int d = 0; d < 4; ++d) { - for (int k = 1; k < 8; ++k) { - int x = sx + dirs[d] * k; - int y = sy + dirs[d + 1] * k; - if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) { - break; - } - if (x == e && y == f) { - return true; - } - } - } - return false; + if (a == e && (c != a || (d - b) * (d - f) > 0)) { + return 1; + } + if (b == f && (d != b || (c - a) * (c - e) > 0)) { + return 1; + } + if (c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0)) { + return 1; + } + if (c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0)) { + return 1; + } + return 2; } } ``` @@ -141,23 +136,19 @@ class Solution { class Solution { public: int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { - int dirs[2][5] = {{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}}; - auto check = [&](int i, int sx, int sy, int bx, int by) { - for (int d = 0; d < 4; ++d) { - for (int k = 1; k < 8; ++k) { - int x = sx + dirs[i][d] * k; - int y = sy + dirs[i][d + 1] * k; - if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) { - break; - } - if (x == e && y == f) { - return true; - } - } - } - return false; - }; - return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2; + if (a == e && (c != a || (d - b) * (d - f) > 0)) { + return 1; + } + if (b == f && (d != b || (c - a) * (c - e) > 0)) { + return 1; + } + if (c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0)) { + return 1; + } + if (c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0)) { + return 1; + } + return 2; } }; ``` @@ -166,23 +157,16 @@ public: ```go func minMovesToCaptureTheQueen(a int, b int, c int, d int, e int, f int) int { - dirs := [2][5]int{{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}} - check := func(i, sx, sy, bx, by int) bool { - for d := 0; d < 4; d++ { - for k := 1; k < 8; k++ { - x := sx + dirs[i][d]*k - y := sy + dirs[i][d+1]*k - if x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by) { - break - } - if x == e && y == f { - return true - } - } - } - return false + if a == e && (c != a || (d-b)*(d-f) > 0) { + return 1 + } + if b == f && (d != b || (c-a)*(c-e) > 0) { + return 1 + } + if c-e == d-f && (a-e != b-f || (a-c)*(a-e) > 0) { + return 1 } - if check(0, a, b, c, d) || check(1, c, d, a, b) { + if c-e == f-d && (a-e != f-b || (a-c)*(a-e) > 0) { return 1 } return 2 @@ -200,29 +184,63 @@ function minMovesToCaptureTheQueen( e: number, f: number, ): number { - const dirs: number[][] = [ - [-1, 0, 1, 0, -1], - [-1, 1, 1, -1, -1], - ]; - const check = (i: number, sx: number, sy: number, bx: number, by: number): boolean => { - for (let d = 0; d < 4; ++d) { - for (let k = 1; k < 8; ++k) { - const x = sx + dirs[i][d] * k; - const y = sy + dirs[i][d + 1] * k; - if (x < 1 || x > 8 || y < 1 || y > 8) { - break; - } - if (x === bx && y === by) { - break; - } - if (x === e && y === f) { - return true; - } - } - } - return false; - }; - return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2; + if (a === e && (c !== a || (d - b) * (d - f) > 0)) { + return 1; + } + if (b === f && (d !== b || (c - a) * (c - e) > 0)) { + return 1; + } + if (c - e === d - f && (a - e !== b - f || (a - c) * (a - e) > 0)) { + return 1; + } + if (c - e === f - d && (a - e !== f - b || (a - c) * (a - e) > 0)) { + return 1; + } + return 2; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn min_moves_to_capture_the_queen(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) -> i32 { + if a == e && (c != a || (d - b) * (d - f) > 0) { + return 1; + } + if b == f && (d != b || (c - a) * (c - e) > 0) { + return 1; + } + if c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0) { + return 1; + } + if c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0) { + return 1; + } + return 2; + } +} +``` + +#### Cangjie + +```cj +class Solution { + func minMovesToCaptureTheQueen(a: Int64, b: Int64, c: Int64, d: Int64, e: Int64, f: Int64): Int64 { + if (a == e && (c != a || (d - b) * (d - f) > 0)) { + return 1 + } + if (b == f && (d != b || (c - a) * (c - e) > 0)) { + return 1 + } + if (c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0)) { + return 1 + } + if (c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0)) { + return 1 + } + 2 + } } ``` diff --git a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/README_EN.md b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/README_EN.md index 6b041652c9a05..72acba26428b0 100644 --- a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/README_EN.md +++ b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/README_EN.md @@ -55,7 +55,7 @@ It is impossible to capture the black queen in less than two moves since it is n
 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: 
+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).
 
@@ -74,7 +74,17 @@ It is impossible to capture the black queen in less than two moves since it is n -### Solution 1 +### Solution 1: Case Analysis + +According to the problem description, we can categorize the scenarios for capturing the black queen as follows: + +1. The white rook and the black queen are in the same row with no other pieces in between. In this case, the white rook only needs to move once. +2. The white rook and the black queen are in the same column with no other pieces in between. In this case, the white rook only needs to move once. +3. The white bishop and the black queen are on the same diagonal `\` with no other pieces in between. In this case, the white bishop only needs to move once. +4. The white bishop and the black queen are on the same diagonal `/` with no other pieces in between. In this case, the white bishop only needs to move once. +5. In other cases, only two moves are needed. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. @@ -85,50 +95,35 @@ class Solution: def minMovesToCaptureTheQueen( self, a: int, b: int, c: int, d: int, e: int, f: int ) -> int: - def check(dirs, sx, sy, bx, by) -> bool: - for dx, dy in pairwise(dirs): - for k in range(1, 8): - x = sx + dx * k - y = sy + dy * k - if not (1 <= x <= 8 and 1 <= y <= 8) or (x, y) == (bx, by): - break - if (x, y) == (e, f): - return True - return False - - dirs1 = (-1, 0, 1, 0, -1) - dirs2 = (-1, 1, 1, -1, -1) - return 1 if check(dirs1, a, b, c, d) or check(dirs2, c, d, a, b) else 2 + if a == e and (c != a or (d - b) * (d - f) > 0): + return 1 + if b == f and (d != b or (c - a) * (c - e) > 0): + return 1 + if c - e == d - f and (a - e != b - f or (a - c) * (a - e) > 0): + return 1 + if c - e == f - d and (a - e != f - b or (a - c) * (a - e) > 0): + return 1 + return 2 ``` #### Java ```java class Solution { - private final int[] dirs1 = {-1, 0, 1, 0, -1}; - private final int[] dirs2 = {-1, 1, 1, -1, -1}; - private int e, f; - public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { - this.e = e; - this.f = f; - return check(dirs1, a, b, c, d) || check(dirs2, c, d, a, b) ? 1 : 2; - } - - private boolean check(int[] dirs, int sx, int sy, int bx, int by) { - for (int d = 0; d < 4; ++d) { - for (int k = 1; k < 8; ++k) { - int x = sx + dirs[d] * k; - int y = sy + dirs[d + 1] * k; - if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) { - break; - } - if (x == e && y == f) { - return true; - } - } - } - return false; + if (a == e && (c != a || (d - b) * (d - f) > 0)) { + return 1; + } + if (b == f && (d != b || (c - a) * (c - e) > 0)) { + return 1; + } + if (c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0)) { + return 1; + } + if (c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0)) { + return 1; + } + return 2; } } ``` @@ -139,23 +134,19 @@ class Solution { class Solution { public: int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { - int dirs[2][5] = {{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}}; - auto check = [&](int i, int sx, int sy, int bx, int by) { - for (int d = 0; d < 4; ++d) { - for (int k = 1; k < 8; ++k) { - int x = sx + dirs[i][d] * k; - int y = sy + dirs[i][d + 1] * k; - if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) { - break; - } - if (x == e && y == f) { - return true; - } - } - } - return false; - }; - return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2; + if (a == e && (c != a || (d - b) * (d - f) > 0)) { + return 1; + } + if (b == f && (d != b || (c - a) * (c - e) > 0)) { + return 1; + } + if (c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0)) { + return 1; + } + if (c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0)) { + return 1; + } + return 2; } }; ``` @@ -164,23 +155,16 @@ public: ```go func minMovesToCaptureTheQueen(a int, b int, c int, d int, e int, f int) int { - dirs := [2][5]int{{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}} - check := func(i, sx, sy, bx, by int) bool { - for d := 0; d < 4; d++ { - for k := 1; k < 8; k++ { - x := sx + dirs[i][d]*k - y := sy + dirs[i][d+1]*k - if x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by) { - break - } - if x == e && y == f { - return true - } - } - } - return false + if a == e && (c != a || (d-b)*(d-f) > 0) { + return 1 + } + if b == f && (d != b || (c-a)*(c-e) > 0) { + return 1 + } + if c-e == d-f && (a-e != b-f || (a-c)*(a-e) > 0) { + return 1 } - if check(0, a, b, c, d) || check(1, c, d, a, b) { + if c-e == f-d && (a-e != f-b || (a-c)*(a-e) > 0) { return 1 } return 2 @@ -198,29 +182,63 @@ function minMovesToCaptureTheQueen( e: number, f: number, ): number { - const dirs: number[][] = [ - [-1, 0, 1, 0, -1], - [-1, 1, 1, -1, -1], - ]; - const check = (i: number, sx: number, sy: number, bx: number, by: number): boolean => { - for (let d = 0; d < 4; ++d) { - for (let k = 1; k < 8; ++k) { - const x = sx + dirs[i][d] * k; - const y = sy + dirs[i][d + 1] * k; - if (x < 1 || x > 8 || y < 1 || y > 8) { - break; - } - if (x === bx && y === by) { - break; - } - if (x === e && y === f) { - return true; - } - } - } - return false; - }; - return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2; + if (a === e && (c !== a || (d - b) * (d - f) > 0)) { + return 1; + } + if (b === f && (d !== b || (c - a) * (c - e) > 0)) { + return 1; + } + if (c - e === d - f && (a - e !== b - f || (a - c) * (a - e) > 0)) { + return 1; + } + if (c - e === f - d && (a - e !== f - b || (a - c) * (a - e) > 0)) { + return 1; + } + return 2; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn min_moves_to_capture_the_queen(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) -> i32 { + if a == e && (c != a || (d - b) * (d - f) > 0) { + return 1; + } + if b == f && (d != b || (c - a) * (c - e) > 0) { + return 1; + } + if c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0) { + return 1; + } + if c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0) { + return 1; + } + return 2; + } +} +``` + +#### Cangjie + +```cj +class Solution { + func minMovesToCaptureTheQueen(a: Int64, b: Int64, c: Int64, d: Int64, e: Int64, f: Int64): Int64 { + if (a == e && (c != a || (d - b) * (d - f) > 0)) { + return 1 + } + if (b == f && (d != b || (c - a) * (c - e) > 0)) { + return 1 + } + if (c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0)) { + return 1 + } + if (c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0)) { + return 1 + } + 2 + } } ``` diff --git a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.cj b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.cj new file mode 100644 index 0000000000000..5e435748a182b --- /dev/null +++ b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.cj @@ -0,0 +1,17 @@ +class Solution { + func minMovesToCaptureTheQueen(a: Int64, b: Int64, c: Int64, d: Int64, e: Int64, f: Int64): Int64 { + if (a == e && (c != a || (d - b) * (d - f) > 0)) { + return 1 + } + if (b == f && (d != b || (c - a) * (c - e) > 0)) { + return 1 + } + if (c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0)) { + return 1 + } + if (c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0)) { + return 1 + } + 2 + } +} diff --git a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.cpp b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.cpp index 6eac09a127bb4..86a4cf5260c22 100644 --- a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.cpp +++ b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.cpp @@ -1,22 +1,18 @@ class Solution { public: int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { - int dirs[2][5] = {{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}}; - auto check = [&](int i, int sx, int sy, int bx, int by) { - for (int d = 0; d < 4; ++d) { - for (int k = 1; k < 8; ++k) { - int x = sx + dirs[i][d] * k; - int y = sy + dirs[i][d + 1] * k; - if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) { - break; - } - if (x == e && y == f) { - return true; - } - } - } - return false; - }; - return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2; + if (a == e && (c != a || (d - b) * (d - f) > 0)) { + return 1; + } + if (b == f && (d != b || (c - a) * (c - e) > 0)) { + return 1; + } + if (c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0)) { + return 1; + } + if (c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0)) { + return 1; + } + return 2; } -}; \ No newline at end of file +}; diff --git a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.go b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.go index b85b4d1447e66..b9a994bafae3f 100644 --- a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.go +++ b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.go @@ -1,22 +1,15 @@ func minMovesToCaptureTheQueen(a int, b int, c int, d int, e int, f int) int { - dirs := [2][5]int{{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}} - check := func(i, sx, sy, bx, by int) bool { - for d := 0; d < 4; d++ { - for k := 1; k < 8; k++ { - x := sx + dirs[i][d]*k - y := sy + dirs[i][d+1]*k - if x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by) { - break - } - if x == e && y == f { - return true - } - } - } - return false + if a == e && (c != a || (d-b)*(d-f) > 0) { + return 1 + } + if b == f && (d != b || (c-a)*(c-e) > 0) { + return 1 + } + if c-e == d-f && (a-e != b-f || (a-c)*(a-e) > 0) { + return 1 } - if check(0, a, b, c, d) || check(1, c, d, a, b) { + if c-e == f-d && (a-e != f-b || (a-c)*(a-e) > 0) { return 1 } return 2 -} \ No newline at end of file +} diff --git a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.java b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.java index 412041f0ad455..2ff0aa773dfc8 100644 --- a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.java +++ b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.java @@ -1,27 +1,17 @@ class Solution { - private final int[] dirs1 = {-1, 0, 1, 0, -1}; - private final int[] dirs2 = {-1, 1, 1, -1, -1}; - private int e, f; - public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { - this.e = e; - this.f = f; - return check(dirs1, a, b, c, d) || check(dirs2, c, d, a, b) ? 1 : 2; - } - - private boolean check(int[] dirs, int sx, int sy, int bx, int by) { - for (int d = 0; d < 4; ++d) { - for (int k = 1; k < 8; ++k) { - int x = sx + dirs[d] * k; - int y = sy + dirs[d + 1] * k; - if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) { - break; - } - if (x == e && y == f) { - return true; - } - } + if (a == e && (c != a || (d - b) * (d - f) > 0)) { + return 1; + } + if (b == f && (d != b || (c - a) * (c - e) > 0)) { + return 1; + } + if (c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0)) { + return 1; + } + if (c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0)) { + return 1; } - return false; + return 2; } -} \ No newline at end of file +} diff --git a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.py b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.py index a0313777c4e29..d8d79e7ea6a77 100644 --- a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.py +++ b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.py @@ -2,17 +2,12 @@ class Solution: def minMovesToCaptureTheQueen( self, a: int, b: int, c: int, d: int, e: int, f: int ) -> int: - def check(dirs, sx, sy, bx, by) -> bool: - for dx, dy in pairwise(dirs): - for k in range(1, 8): - x = sx + dx * k - y = sy + dy * k - if not (1 <= x <= 8 and 1 <= y <= 8) or (x, y) == (bx, by): - break - if (x, y) == (e, f): - return True - return False - - dirs1 = (-1, 0, 1, 0, -1) - dirs2 = (-1, 1, 1, -1, -1) - return 1 if check(dirs1, a, b, c, d) or check(dirs2, c, d, a, b) else 2 + if a == e and (c != a or (d - b) * (d - f) > 0): + return 1 + if b == f and (d != b or (c - a) * (c - e) > 0): + return 1 + if c - e == d - f and (a - e != b - f or (a - c) * (a - e) > 0): + return 1 + if c - e == f - d and (a - e != f - b or (a - c) * (a - e) > 0): + return 1 + return 2 diff --git a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.rs b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.rs new file mode 100644 index 0000000000000..adeb3f12944a3 --- /dev/null +++ b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.rs @@ -0,0 +1,17 @@ +impl Solution { + pub fn min_moves_to_capture_the_queen(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) -> i32 { + if a == e && (c != a || (d - b) * (d - f) > 0) { + return 1; + } + if b == f && (d != b || (c - a) * (c - e) > 0) { + return 1; + } + if c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0) { + return 1; + } + if c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0) { + return 1; + } + return 2; + } +} diff --git a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.ts b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.ts index db4633c886027..73aeec6ab064e 100644 --- a/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.ts +++ b/solution/3000-3099/3001.Minimum Moves to Capture The Queen/Solution.ts @@ -6,27 +6,17 @@ function minMovesToCaptureTheQueen( e: number, f: number, ): number { - const dirs: number[][] = [ - [-1, 0, 1, 0, -1], - [-1, 1, 1, -1, -1], - ]; - const check = (i: number, sx: number, sy: number, bx: number, by: number): boolean => { - for (let d = 0; d < 4; ++d) { - for (let k = 1; k < 8; ++k) { - const x = sx + dirs[i][d] * k; - const y = sy + dirs[i][d + 1] * k; - if (x < 1 || x > 8 || y < 1 || y > 8) { - break; - } - if (x === bx && y === by) { - break; - } - if (x === e && y === f) { - return true; - } - } - } - return false; - }; - return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2; + if (a === e && (c !== a || (d - b) * (d - f) > 0)) { + return 1; + } + if (b === f && (d !== b || (c - a) * (c - e) > 0)) { + return 1; + } + if (c - e === d - f && (a - e !== b - f || (a - c) * (a - e) > 0)) { + return 1; + } + if (c - e === f - d && (a - e !== f - b || (a - c) * (a - e) > 0)) { + return 1; + } + return 2; }