From 48ef65f70d29ef0518f7448ecd07940cc4d384f5 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 8 Jan 2024 15:40:55 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.10036 No.10036.Minimum Moves to Capture The Queen --- .../README.md | 128 +++++++++++++++++- .../README_EN.md | 128 +++++++++++++++++- .../Solution.cpp | 22 +++ .../Solution.go | 22 +++ .../Solution.java | 27 ++++ .../Solution.py | 18 +++ .../Solution.ts | 32 +++++ 7 files changed, 371 insertions(+), 6 deletions(-) create mode 100644 solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.cpp create mode 100644 solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.go create mode 100644 solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.java create mode 100644 solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.py create mode 100644 solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.ts diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README.md b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README.md index dbabbdf85d957..301f55c4fb0a6 100644 --- a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README.md +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README.md @@ -68,7 +68,24 @@ ```python - +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 ``` ### **Java** @@ -76,19 +93,124 @@ ```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; + } +} ``` ### **C++** ```cpp - +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; + } +}; ``` ### **Go** ```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 check(0, a, b, c, d) || check(1, c, d, a, b) { + return 1 + } + return 2 +} +``` +### **TypeScript** + +```ts +function minMovesToCaptureTheQueen( + a: number, + b: number, + c: number, + d: number, + 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; +} ``` ### **...** diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README_EN.md b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README_EN.md index 8340757c22f7f..077d2c6fb6f86 100644 --- a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README_EN.md +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README_EN.md @@ -60,25 +60,147 @@ It is impossible to capture the black queen in less than two moves since it is n ### **Python3** ```python - +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 ``` ### **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; + } +} ``` ### **C++** ```cpp - +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; + } +}; ``` ### **Go** ```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 check(0, a, b, c, d) || check(1, c, d, a, b) { + return 1 + } + return 2 +} +``` +### **TypeScript** + +```ts +function minMovesToCaptureTheQueen( + a: number, + b: number, + c: number, + d: number, + 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; +} ``` ### **...** diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.cpp b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.cpp new file mode 100644 index 0000000000000..c01429a83a927 --- /dev/null +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.cpp @@ -0,0 +1,22 @@ +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; + } +}; \ No newline at end of file diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.go b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.go new file mode 100644 index 0000000000000..39014a52e806c --- /dev/null +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.go @@ -0,0 +1,22 @@ +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 check(0, a, b, c, d) || check(1, c, d, a, b) { + return 1 + } + return 2 +} \ No newline at end of file diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.java b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.java new file mode 100644 index 0000000000000..b5d21f96bcd21 --- /dev/null +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.java @@ -0,0 +1,27 @@ +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; + } +} \ No newline at end of file diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.py b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.py new file mode 100644 index 0000000000000..486ccf66eb134 --- /dev/null +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.py @@ -0,0 +1,18 @@ +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 diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.ts b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.ts new file mode 100644 index 0000000000000..db4633c886027 --- /dev/null +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/Solution.ts @@ -0,0 +1,32 @@ +function minMovesToCaptureTheQueen( + a: number, + b: number, + c: number, + d: number, + 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; +}