Problem p02961 (python)

Fixed submission:
# -*- coding: utf-8 -*-
import sys
from itertools import product
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    K=int(input())
    X,Y=map(int,input().split())
    
    def is_on_square(x0,y0,x1,y1):
        return (abs(x1-x0)==K and y0-K<=y1<=y0+K) or (abs(y1-y0)==K and x0-K<=x1<=x0+K)
    
    rX=X-Y
    rY=X+Y
    x,y=0,0
    ans=[]
    while True:
        # 残り一打
        if max(abs(x-rX),abs(y-rY))==K:
            ans.append((rX,rY))
            break
        # 残り二打以内の可能性あり
        elif abs(x-rX)<=K*2 and abs(y-rY)<=K*2:
            for nx,ny in list(product((x+K,x-K),(rY+K,rY-K)))+list(product((rX+K,rX-K),(y+K,y-K))):
                if is_on_square(x,y,nx,ny) and is_on_square(rX,rY,nx,ny) and (nx+ny)%2==0:
                    x,y=nx,ny
                    break
            else:
                # Kが奇数なら一打後残り二打になる
                if K%2:
                    if x<rX:
                        x+=K
                    else:
                        x-=K
                    if y<rY:
                        y+=K
                    else:
                        y-=K
                else:
                    ans=[]
                    break
        
        else:
            # x==rX or y==rYに寄せる
            if abs(x-rX)<abs(y-rY):
                if abs(x-rX)>=K:
                    if x>rX:
                        x-=K
                    else:
                        x+=K
                    if y>rY:
                        y-=K
                    else:
                        y+=K
                else:
                    x=rX
                    if y>rY:
                        y-=K
                    else:
                        y+=K
                    if (x+y)%2:
                        x+=1
            else:
                if abs(y-rY)>=K:
                    if x>rX:
                        x-=K
                    else:
                        x+=K
                    if y>rY:
                        y-=K
                    else:
                        y+=K
                else:
                    y=rY
                    if x>rX:
                        x-=K
                    else:
                        x+=K
                    if (x+y)%2:
                        y+=1
        ans.append((x,y))
    
    if not ans:
        print(-1)
    else:
        print(len(ans))
        for x,y in ans:
            print((y+x)//2,(y-x)//2)
                
        

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
K=int(input())
X,Y=map(int,input().split())
if K%2==1:
  print(-1)
else:
  raise Exception

Buggy submission (verdict: Runtime Error):
K=int(input())
X,Y=map(int,input().split())
if K%2==0:
  print(-1)
else:
  raise Exception

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    K=int(input())
    X,Y=map(int,input().split())
    rX=X-Y
    rY=X+Y
    x,y=0,0
    ans=[]
    if X+Y==K:
        ans.append((rX,rY))
    else:
        while len(ans)<10**7:
            # 残り一打
            if (abs(x-rX)==K and abs(y-rY)<=K) or (abs(y-rY)==K and abs(x-rX)<=K):
                ans.append((rX,rY))
                break
            
            if x==rX:
                # 残り二打
                if abs(y-rY)<K*2:
                    if y>rY:
                        x,y=x+K,y-K
                    else:
                        x,y=x+K,y+K
                # 残り三打以上
                else:
                    if y>rY:
                        y-=K
                    else:
                        y+=K
                    if (x+y)%2:
                        x+=1
            elif y==rY:
                if abs(x-rX)<K*2:
                    if x>rX:
                        x,y=x-K,y+K
                    else:
                        x,y=x+K,y+K
                else:
                    if x>rX:
                        x-=K
                    else:
                        x+=K
                    if (x+y)%2:
                        y+=1
            else:
                # x==rX or y==rYに寄せる
                if abs(x-rX)<abs(y-rY):
                    while len(ans)<10**7:
                        if abs(x-rX)>=K:
                            if x>rX:
                                x-=K
                            else:
                                x+=K
                            if y>rY:
                                y-=K
                            else:
                                y+=K
                            ans.append((x,y))
                        else:
                            x=rX
                            if y>rY:
                                y-=K
                            else:
                                y+=K
                            if (x+y)%2:
                                x+=1
                            break
                else:
                    while len(ans)<10**7:
                        if abs(y-rY)>=K:
                            if x>rX:
                                x-=K
                            else:
                                x+=K
                            if y>rY:
                                y-=K
                            else:
                                y+=K
                            ans.append((x,y))
                        else:
                            if x>rX:
                                x-=K
                            else:
                                x+=K
                            if (x+y)%2:
                                y+=1
                            y=rY
                            break
                ans.append((x,y))
                continue
            ans.append((x,y))
    
    if len(ans)>=10**7:
        print(-1)
    else:
        raise Exception
        print(len(ans))
        for x,y in ans:
            print((y+x)//2,(y-x)//2)
                
        

if __name__ == '__main__':
    main()