7676
7777<!-- solution:start -->
7878
79- ### 方法一
79+ ### 方法一:分类讨论
80+
81+ 根据题意,我们可以将捕获黑皇后的情况分为以下几种:
82+
83+ 1 . 白色车和黑皇后在同一行,且中间没有其他棋子,此时只需要移动白色车 $1$ 一次;
84+ 1 . 白色车和黑皇后在同一列,且中间没有其他棋子,此时只需要移动白色车 $1$ 一次;
85+ 1 . 白色象和黑皇后在对角线 ` \ ` 上,且中间没有其他棋子,此时只需要移动白色象 $1$ 一次;
86+ 1 . 白色象和黑皇后在对角线 ` / ` 上,且中间没有其他棋子,此时只需要移动白色象 $1$ 一次;
87+ 1 . 其他情况,只需要移动两次。
88+
89+ 时间复杂度 $O(1)$,空间复杂度 $O(1)$。
8090
8191<!-- tabs:start -->
8292
@@ -87,50 +97,35 @@ class Solution:
8797 def minMovesToCaptureTheQueen (
8898 self , a : int , b : int , c : int , d : int , e : int , f : int
8999 ) -> int :
90- def check (dirs , sx , sy , bx , by ) -> bool :
91- for dx, dy in pairwise(dirs):
92- for k in range (1 , 8 ):
93- x = sx + dx * k
94- y = sy + dy * k
95- if not (1 <= x <= 8 and 1 <= y <= 8 ) or (x, y) == (bx, by):
96- break
97- if (x, y) == (e, f):
98- return True
99- return False
100-
101- dirs1 = (- 1 , 0 , 1 , 0 , - 1 )
102- dirs2 = (- 1 , 1 , 1 , - 1 , - 1 )
103- return 1 if check(dirs1, a, b, c, d) or check(dirs2, c, d, a, b) else 2
100+ if a == e and (c != a or (d - b) * (d - f) > 0 ):
101+ return 1
102+ if b == f and (d != b or (c - a) * (c - e) > 0 ):
103+ return 1
104+ if c - e == d - f and (a - e != b - f or (a - c) * (a - e) > 0 ):
105+ return 1
106+ if c - e == f - d and (a - e != f - b or (a - c) * (a - e) > 0 ):
107+ return 1
108+ return 2
104109```
105110
106111#### Java
107112
108113``` java
109114class Solution {
110- private final int [] dirs1 = {- 1 , 0 , 1 , 0 , - 1 };
111- private final int [] dirs2 = {- 1 , 1 , 1 , - 1 , - 1 };
112- private int e, f;
113-
114115 public int minMovesToCaptureTheQueen (int a , int b , int c , int d , int e , int f ) {
115- this . e = e;
116- this . f = f;
117- return check(dirs1, a, b, c, d) || check(dirs2, c, d, a, b) ? 1 : 2 ;
118- }
119-
120- private boolean check (int [] dirs , int sx , int sy , int bx , int by ) {
121- for (int d = 0 ; d < 4 ; ++ d) {
122- for (int k = 1 ; k < 8 ; ++ k) {
123- int x = sx + dirs[d] * k;
124- int y = sy + dirs[d + 1 ] * k;
125- if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) {
126- break ;
127- }
128- if (x == e && y == f) {
129- return true ;
130- }
131- }
132- }
133- return false ;
116+ if (a == e && (c != a || (d - b) * (d - f) > 0 )) {
117+ return 1 ;
118+ }
119+ if (b == f && (d != b || (c - a) * (c - e) > 0 )) {
120+ return 1 ;
121+ }
122+ if (c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0 )) {
123+ return 1 ;
124+ }
125+ if (c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0 )) {
126+ return 1 ;
127+ }
128+ return 2 ;
134129 }
135130}
136131```
@@ -141,23 +136,19 @@ class Solution {
141136class Solution {
142137public:
143138 int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
144- int dirs[ 2] [ 5 ] = {{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}};
145- auto check = [ &] (int i, int sx, int sy, int bx, int by) {
146- for (int d = 0; d < 4; ++d) {
147- for (int k = 1; k < 8; ++k) {
148- int x = sx + dirs[ i] [ d ] * k;
149- int y = sy + dirs[ i] [ d + 1 ] * k;
150- if (x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by)) {
151- break;
152- }
153- if (x == e && y == f) {
154- return true;
155- }
156- }
157- }
158- return false;
159- };
160- return check(0, a, b, c, d) || check(1, c, d, a, b) ? 1 : 2;
139+ if (a == e && (c != a || (d - b) * (d - f) > 0)) {
140+ return 1;
141+ }
142+ if (b == f && (d != b || (c - a) * (c - e) > 0)) {
143+ return 1;
144+ }
145+ if (c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0)) {
146+ return 1;
147+ }
148+ if (c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0)) {
149+ return 1;
150+ }
151+ return 2;
161152 }
162153};
163154```
@@ -166,23 +157,16 @@ public:
166157
167158```go
168159func minMovesToCaptureTheQueen(a int, b int, c int, d int, e int, f int) int {
169- dirs := [2][5]int{{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}}
170- check := func(i, sx, sy, bx, by int) bool {
171- for d := 0; d < 4; d++ {
172- for k := 1; k < 8; k++ {
173- x := sx + dirs[i][d]*k
174- y := sy + dirs[i][d+1]*k
175- if x < 1 || x > 8 || y < 1 || y > 8 || (x == bx && y == by) {
176- break
177- }
178- if x == e && y == f {
179- return true
180- }
181- }
182- }
183- return false
160+ if a == e && (c != a || (d-b)*(d-f) > 0) {
161+ return 1
162+ }
163+ if b == f && (d != b || (c-a)*(c-e) > 0) {
164+ return 1
165+ }
166+ if c-e == d-f && (a-e != b-f || (a-c)*(a-e) > 0) {
167+ return 1
184168 }
185- if check(0, a, b, c, d) || check(1, c, d, a, b ) {
169+ if c-e == f-d && (a-e != f-b || (a-c)*(a-e) > 0 ) {
186170 return 1
187171 }
188172 return 2
@@ -200,29 +184,63 @@ function minMovesToCaptureTheQueen(
200184 e : number ,
201185 f : number ,
202186): number {
203- const dirs: number [][] = [
204- [- 1 , 0 , 1 , 0 , - 1 ],
205- [- 1 , 1 , 1 , - 1 , - 1 ],
206- ];
207- const check = (i : number , sx : number , sy : number , bx : number , by : number ): boolean => {
208- for (let d = 0 ; d < 4 ; ++ d ) {
209- for (let k = 1 ; k < 8 ; ++ k ) {
210- const x = sx + dirs [i ][d ] * k ;
211- const y = sy + dirs [i ][d + 1 ] * k ;
212- if (x < 1 || x > 8 || y < 1 || y > 8 ) {
213- break ;
214- }
215- if (x === bx && y === by ) {
216- break ;
217- }
218- if (x === e && y === f ) {
219- return true ;
220- }
221- }
222- }
223- return false ;
224- };
225- return check (0 , a , b , c , d ) || check (1 , c , d , a , b ) ? 1 : 2 ;
187+ if (a === e && (c !== a || (d - b ) * (d - f ) > 0 )) {
188+ return 1 ;
189+ }
190+ if (b === f && (d !== b || (c - a ) * (c - e ) > 0 )) {
191+ return 1 ;
192+ }
193+ if (c - e === d - f && (a - e !== b - f || (a - c ) * (a - e ) > 0 )) {
194+ return 1 ;
195+ }
196+ if (c - e === f - d && (a - e !== f - b || (a - c ) * (a - e ) > 0 )) {
197+ return 1 ;
198+ }
199+ return 2 ;
200+ }
201+ ```
202+
203+ #### Rust
204+
205+ ``` rust
206+ impl Solution {
207+ pub fn min_moves_to_capture_the_queen (a : i32 , b : i32 , c : i32 , d : i32 , e : i32 , f : i32 ) -> i32 {
208+ if a == e && (c != a || (d - b ) * (d - f ) > 0 ) {
209+ return 1 ;
210+ }
211+ if b == f && (d != b || (c - a ) * (c - e ) > 0 ) {
212+ return 1 ;
213+ }
214+ if c - e == d - f && (a - e != b - f || (a - c ) * (a - e ) > 0 ) {
215+ return 1 ;
216+ }
217+ if c - e == f - d && (a - e != f - b || (a - c ) * (a - e ) > 0 ) {
218+ return 1 ;
219+ }
220+ return 2 ;
221+ }
222+ }
223+ ```
224+
225+ #### Cangjie
226+
227+ ``` cj
228+ class Solution {
229+ func minMovesToCaptureTheQueen(a: Int64, b: Int64, c: Int64, d: Int64, e: Int64, f: Int64): Int64 {
230+ if (a == e && (c != a || (d - b) * (d - f) > 0)) {
231+ return 1
232+ }
233+ if (b == f && (d != b || (c - a) * (c - e) > 0)) {
234+ return 1
235+ }
236+ if (c - e == d - f && (a - e != b - f || (a - c) * (a - e) > 0)) {
237+ return 1
238+ }
239+ if (c - e == f - d && (a - e != f - b || (a - c) * (a - e) > 0)) {
240+ return 1
241+ }
242+ 2
243+ }
226244}
227245```
228246
0 commit comments