LeetCode 3001: Minimum Moves to Capture The Queen (Geometry / Chess Simulation)
LeetCode 3001GeometrySimulationSource: https://leetcode.com/problems/minimum-moves-to-capture-the-queen/
English
Check whether rook can capture queen in one move without bishop blocking on same row/column. Then check whether bishop can capture queen in one move on same diagonal without rook blocking. If neither works, answer is always 2.
class Solution {
public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
if (a == e && !between(b, d, f)) return 1;
if (b == f && !between(a, c, e)) return 1;
if (Math.abs(c - e) == Math.abs(d - f) && !betweenDiag(c, d, a, b, e, f)) return 1;
return 2;
}
private boolean between(int x, int m, int y) { return (x < m && m < y) || (y < m && m < x); }
private boolean betweenDiag(int x1,int y1,int xb,int yb,int x2,int y2){
return Math.abs(xb-x1)==Math.abs(yb-y1)&&Math.abs(xb-x2)==Math.abs(yb-y2)&&between(x1,xb,x2);
}
}func minMovesToCaptureTheQueen(a,b,c,d,e,f int) int {
between:=func(x,m,y int) bool { return (xclass Solution {
public:
bool between(int x,int m,int y){ return (xclass Solution:
def minMovesToCaptureTheQueen(self, a,b,c,d,e,f):
def between(x,m,y):
return (x < m < y) or (y < m < x)
if a == e and not between(b, d, f):
return 1
if b == f and not between(a, c, e):
return 1
on_diag = abs(c - e) == abs(d - f)
blocked = abs(a - c) == abs(b - d) and abs(a - e) == abs(b - f) and between(c, a, e)
return 1 if on_diag and not blocked else 2function minMovesToCaptureTheQueen(a,b,c,d,e,f){
const between=(x,m,y)=>(x中文
先判断车是否能一步吃后(同行/同列且象不在中间阻挡),再判断象是否能一步吃后(同对角线且车不在中间阻挡)。如果都不行,答案一定是 2。
class Solution {
public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
if (a == e && !between(b, d, f)) return 1;
if (b == f && !between(a, c, e)) return 1;
if (Math.abs(c - e) == Math.abs(d - f) && !betweenDiag(c, d, a, b, e, f)) return 1;
return 2;
}
private boolean between(int x, int m, int y) { return (x < m && m < y) || (y < m && m < x); }
private boolean betweenDiag(int x1,int y1,int xb,int yb,int x2,int y2){
return Math.abs(xb-x1)==Math.abs(yb-y1)&&Math.abs(xb-x2)==Math.abs(yb-y2)&&between(x1,xb,x2);
}
}func minMovesToCaptureTheQueen(a,b,c,d,e,f int) int {
between:=func(x,m,y int) bool { return (xclass Solution {
public:
bool between(int x,int m,int y){ return (xclass Solution:
def minMovesToCaptureTheQueen(self, a,b,c,d,e,f):
def between(x,m,y):
return (x < m < y) or (y < m < x)
if a == e and not between(b, d, f):
return 1
if b == f and not between(a, c, e):
return 1
on_diag = abs(c - e) == abs(d - f)
blocked = abs(a - c) == abs(b - d) and abs(a - e) == abs(b - f) and between(c, a, e)
return 1 if on_diag and not blocked else 2function minMovesToCaptureTheQueen(a,b,c,d,e,f){
const between=(x,m,y)=>(x
Comments