1. 解题思路
这一题披着国际象棋的皮,不过其实算是一道比较常规的题目了,毕竟要求后不走动挨吃实在是有点离谱了……
Anyway,显然无论针对何种情况,至多用车走两步就能确保吃掉后,因此我们只需要考察哪些情况下只需要1步就能吃掉后的情况就行。
而要一步吃掉后,那么就必须满足如下两个要求:
- 后在车或者相的直接行进路线上面;
- 另一个子不会挡住该子的行进。
我们将其翻译为代码语言即可。
2. 代码实现
给出python代码实现如下:
class Solution:
def minMovesToCaptureTheQueen(self, a: int, b: int, c: int, d: int, e: int, f: int) -> int:
# rook = [a, b]
# bishop = [c, d]
# queen = [e, f]
if a == e:
if not (c == a and (b <= d <= f or b >= d >= f)):
return 1
elif b == f:
if not (d == b and (a <= c <= e or a >= c >= e)):
return 1
elif c-d == e-f:
if not (c-d == a-b and (c <= a <= e or c >= a >= e)):
return 1
elif c+d == e+f:
if not (c+d == a+b and (c <= a <= e or c >= a >= e)):
return 1
return 2
提交代码评测得到:耗时36ms,占用内存17.4MB。