我的 if/else 不能正常工作

My if/else doesn't work correct

我正在尝试解决王走(国际象棋)任务。我发现king只有坐标-1 <= x1-x2 and y1-y2 <= 1才能移动。 我的代码是:

x1 = int(input())              #cuurent x-position
y1 = int(input())              #current y-position
x2 = int(input())              #estimated x-position
y2 = int(input())              #estimated y-position
if -1 <= x1-x2 and y1-y2 <= 1: #king can move to the x2-y2 from x1-y1
    print('YES') 
else:                          #king can't move to the x2-y2 from x1-y1
    print('NO') 

我对我能找到的所有 'YES' 动作都很好,但对某些 'NO' 动作不起作用,例如:

x1=4, y1=4, x2=2, y2=6 or x1=4, y1=4, x2=4, y2=6

。我不知道为什么,因为:4-4=0,但 4-6=-2 和 -2 小于 -1。

如果坐标之间的绝对差小于或等于1,则国王可以移动。

所以,写:

if abs(x1-x2) <= 1 and abs(y1-y2) <= 1: #king can move to the x2-y2 from x1-y1
    print('YES') 
else:                          #king can't move to the x2-y2 from x1-y1
    print('NO') 

这是因为您要移动的坐标可能 greater/lesser 比您要移动到的坐标,但您知道它们最多相差 1。