Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 610f831

Browse files
feat: add solutions to lc problem: No.10036 (doocs#2198)
No.10036.Minimum Moves to Capture The Queen
1 parent f175793 commit 610f831

File tree

7 files changed

+371
-6
lines changed

7 files changed

+371
-6
lines changed

‎solution/10000-10099/10036.Minimum Moves to Capture The Queen/README.md‎

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,149 @@
6868
<!-- 这里可写当前语言的特殊实现逻辑 -->
6969

7070
```python
71-
71+
class Solution:
72+
def minMovesToCaptureTheQueen(
73+
self, a: int, b: int, c: int, d: int, e: int, f: int
74+
) -> int:
75+
def check(dirs, sx, sy, bx, by) -> bool:
76+
for dx, dy in pairwise(dirs):
77+
for k in range(1, 8):
78+
x = sx + dx * k
79+
y = sy + dy * k
80+
if not (1 <= x <= 8 and 1 <= y <= 8) or (x, y) == (bx, by):
81+
break
82+
if (x, y) == (e, f):
83+
return True
84+
return False
85+
86+
dirs1 = (-1, 0, 1, 0, -1)
87+
dirs2 = (-1, 1, 1, -1, -1)
88+
return 1 if check(dirs1, a, b, c, d) or check(dirs2, c, d, a, b) else 2
7289
```
7390

7491
### **Java**
7592

7693
<!-- 这里可写当前语言的特殊实现逻辑 -->
7794

7895
```java
79-
96+
class Solution {
97+
private final int[] dirs1 = {-1, 0, 1, 0, -1};
98+
private final int[] dirs2 = {-1, 1, 1, -1, -1};
99+
private int e, f;
100+
101+
public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
102+
this.e = e;
103+
this.f = f;
104+
return check(dirs1, a, b, c, d) || check(dirs2, c, d, a, b) ? 1 : 2;
105+
}
106+
107+
private boolean check(int[] dirs, int sx, int sy, int bx, int by) {
108+
for (int d = 0; d < 4; ++d) {
109+
for (int k = 1; k < 8; ++k) {
110+
int x = sx + dirs[d] * k;
111+
int y = sy + dirs[d + 1] * k;
112+
if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) {
113+
break;
114+
}
115+
if (x == e && y == f) {
116+
return true;
117+
}
118+
}
119+
}
120+
return false;
121+
}
122+
}
80123
```
81124

82125
### **C++**
83126

84127
```cpp
85-
128+
class Solution {
129+
public:
130+
int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
131+
int dirs[2][5] = {{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}};
132+
auto check = [&](int i, int sx, int sy, int bx, int by) {
133+
for (int d = 0; d < 4; ++d) {
134+
for (int k = 1; k < 8; ++k) {
135+
int x = sx + dirs[i][d] * k;
136+
int y = sy + dirs[i][d + 1] * k;
137+
if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) {
138+
break;
139+
}
140+
if (x == e && y == f) {
141+
return true;
142+
}
143+
}
144+
}
145+
return false;
146+
};
147+
return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2;
148+
}
149+
};
86150
```
87151
88152
### **Go**
89153
90154
```go
155+
func minMovesToCaptureTheQueen(a int, b int, c int, d int, e int, f int) int {
156+
dirs := [2][5]int{{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}}
157+
check := func(i, sx, sy, bx, by int) bool {
158+
for d := 0; d < 4; d++ {
159+
for k := 1; k < 8; k++ {
160+
x := sx + dirs[i][d]*k
161+
y := sy + dirs[i][d+1]*k
162+
if x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by) {
163+
break
164+
}
165+
if x == e && y == f {
166+
return true
167+
}
168+
}
169+
}
170+
return false
171+
}
172+
if check(0, a, b, c, d) || check(1, c, d, a, b) {
173+
return 1
174+
}
175+
return 2
176+
}
177+
```
91178

179+
### **TypeScript**
180+
181+
```ts
182+
function minMovesToCaptureTheQueen(
183+
a: number,
184+
b: number,
185+
c: number,
186+
d: number,
187+
e: number,
188+
f: number,
189+
): number {
190+
const dirs: number[][] = [
191+
[-1, 0, 1, 0, -1],
192+
[-1, 1, 1, -1, -1],
193+
];
194+
const check = (i: number, sx: number, sy: number, bx: number, by: number): boolean => {
195+
for (let d = 0; d < 4; ++d) {
196+
for (let k = 1; k < 8; ++k) {
197+
const x = sx + dirs[i][d] * k;
198+
const y = sy + dirs[i][d + 1] * k;
199+
if (x < 1 || x > 8 || y < 1 || y > 8) {
200+
break;
201+
}
202+
if (x === bx && y === by) {
203+
break;
204+
}
205+
if (x === e && y === f) {
206+
return true;
207+
}
208+
}
209+
}
210+
return false;
211+
};
212+
return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2;
213+
}
92214
```
93215

94216
### **...**

‎solution/10000-10099/10036.Minimum Moves to Capture The Queen/README_EN.md‎

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,147 @@ It is impossible to capture the black queen in less than two moves since it is n
6060
### **Python3**
6161

6262
```python
63-
63+
class Solution:
64+
def minMovesToCaptureTheQueen(
65+
self, a: int, b: int, c: int, d: int, e: int, f: int
66+
) -> int:
67+
def check(dirs, sx, sy, bx, by) -> bool:
68+
for dx, dy in pairwise(dirs):
69+
for k in range(1, 8):
70+
x = sx + dx * k
71+
y = sy + dy * k
72+
if not (1 <= x <= 8 and 1 <= y <= 8) or (x, y) == (bx, by):
73+
break
74+
if (x, y) == (e, f):
75+
return True
76+
return False
77+
78+
dirs1 = (-1, 0, 1, 0, -1)
79+
dirs2 = (-1, 1, 1, -1, -1)
80+
return 1 if check(dirs1, a, b, c, d) or check(dirs2, c, d, a, b) else 2
6481
```
6582

6683
### **Java**
6784

6885
```java
69-
86+
class Solution {
87+
private final int[] dirs1 = {-1, 0, 1, 0, -1};
88+
private final int[] dirs2 = {-1, 1, 1, -1, -1};
89+
private int e, f;
90+
91+
public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
92+
this.e = e;
93+
this.f = f;
94+
return check(dirs1, a, b, c, d) || check(dirs2, c, d, a, b) ? 1 : 2;
95+
}
96+
97+
private boolean check(int[] dirs, int sx, int sy, int bx, int by) {
98+
for (int d = 0; d < 4; ++d) {
99+
for (int k = 1; k < 8; ++k) {
100+
int x = sx + dirs[d] * k;
101+
int y = sy + dirs[d + 1] * k;
102+
if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) {
103+
break;
104+
}
105+
if (x == e && y == f) {
106+
return true;
107+
}
108+
}
109+
}
110+
return false;
111+
}
112+
}
70113
```
71114

72115
### **C++**
73116

74117
```cpp
75-
118+
class Solution {
119+
public:
120+
int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
121+
int dirs[2][5] = {{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}};
122+
auto check = [&](int i, int sx, int sy, int bx, int by) {
123+
for (int d = 0; d < 4; ++d) {
124+
for (int k = 1; k < 8; ++k) {
125+
int x = sx + dirs[i][d] * k;
126+
int y = sy + dirs[i][d + 1] * k;
127+
if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) {
128+
break;
129+
}
130+
if (x == e && y == f) {
131+
return true;
132+
}
133+
}
134+
}
135+
return false;
136+
};
137+
return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2;
138+
}
139+
};
76140
```
77141
78142
### **Go**
79143
80144
```go
145+
func minMovesToCaptureTheQueen(a int, b int, c int, d int, e int, f int) int {
146+
dirs := [2][5]int{{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}}
147+
check := func(i, sx, sy, bx, by int) bool {
148+
for d := 0; d < 4; d++ {
149+
for k := 1; k < 8; k++ {
150+
x := sx + dirs[i][d]*k
151+
y := sy + dirs[i][d+1]*k
152+
if x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by) {
153+
break
154+
}
155+
if x == e && y == f {
156+
return true
157+
}
158+
}
159+
}
160+
return false
161+
}
162+
if check(0, a, b, c, d) || check(1, c, d, a, b) {
163+
return 1
164+
}
165+
return 2
166+
}
167+
```
81168

169+
### **TypeScript**
170+
171+
```ts
172+
function minMovesToCaptureTheQueen(
173+
a: number,
174+
b: number,
175+
c: number,
176+
d: number,
177+
e: number,
178+
f: number,
179+
): number {
180+
const dirs: number[][] = [
181+
[-1, 0, 1, 0, -1],
182+
[-1, 1, 1, -1, -1],
183+
];
184+
const check = (i: number, sx: number, sy: number, bx: number, by: number): boolean => {
185+
for (let d = 0; d < 4; ++d) {
186+
for (let k = 1; k < 8; ++k) {
187+
const x = sx + dirs[i][d] * k;
188+
const y = sy + dirs[i][d + 1] * k;
189+
if (x < 1 || x > 8 || y < 1 || y > 8) {
190+
break;
191+
}
192+
if (x === bx && y === by) {
193+
break;
194+
}
195+
if (x === e && y === f) {
196+
return true;
197+
}
198+
}
199+
}
200+
return false;
201+
};
202+
return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2;
203+
}
82204
```
83205

84206
### **...**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
4+
int dirs[2][5] = {{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}};
5+
auto check = [&](int i, int sx, int sy, int bx, int by) {
6+
for (int d = 0; d < 4; ++d) {
7+
for (int k = 1; k < 8; ++k) {
8+
int x = sx + dirs[i][d] * k;
9+
int y = sy + dirs[i][d + 1] * k;
10+
if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) {
11+
break;
12+
}
13+
if (x == e && y == f) {
14+
return true;
15+
}
16+
}
17+
}
18+
return false;
19+
};
20+
return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2;
21+
}
22+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func minMovesToCaptureTheQueen(a int, b int, c int, d int, e int, f int) int {
2+
dirs := [2][5]int{{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}}
3+
check := func(i, sx, sy, bx, by int) bool {
4+
for d := 0; d < 4; d++ {
5+
for k := 1; k < 8; k++ {
6+
x := sx + dirs[i][d]*k
7+
y := sy + dirs[i][d+1]*k
8+
if x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by) {
9+
break
10+
}
11+
if x == e && y == f {
12+
return true
13+
}
14+
}
15+
}
16+
return false
17+
}
18+
if check(0, a, b, c, d) || check(1, c, d, a, b) {
19+
return 1
20+
}
21+
return 2
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
private final int[] dirs1 = {-1, 0, 1, 0, -1};
3+
private final int[] dirs2 = {-1, 1, 1, -1, -1};
4+
private int e, f;
5+
6+
public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
7+
this.e = e;
8+
this.f = f;
9+
return check(dirs1, a, b, c, d) || check(dirs2, c, d, a, b) ? 1 : 2;
10+
}
11+
12+
private boolean check(int[] dirs, int sx, int sy, int bx, int by) {
13+
for (int d = 0; d < 4; ++d) {
14+
for (int k = 1; k < 8; ++k) {
15+
int x = sx + dirs[d] * k;
16+
int y = sy + dirs[d + 1] * k;
17+
if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) {
18+
break;
19+
}
20+
if (x == e && y == f) {
21+
return true;
22+
}
23+
}
24+
}
25+
return false;
26+
}
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def minMovesToCaptureTheQueen(
3+
self, a: int, b: int, c: int, d: int, e: int, f: int
4+
) -> int:
5+
def check(dirs, sx, sy, bx, by) -> bool:
6+
for dx, dy in pairwise(dirs):
7+
for k in range(1, 8):
8+
x = sx + dx * k
9+
y = sy + dy * k
10+
if not (1 <= x <= 8 and 1 <= y <= 8) or (x, y) == (bx, by):
11+
break
12+
if (x, y) == (e, f):
13+
return True
14+
return False
15+
16+
dirs1 = (-1, 0, 1, 0, -1)
17+
dirs2 = (-1, 1, 1, -1, -1)
18+
return 1 if check(dirs1, a, b, c, d) or check(dirs2, c, d, a, b) else 2

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /