fork download
  1. # your code goes here
  2. def count_attack_positions(xK, yK, xQ, yQ, a, b):
  3. valid_positions = 0
  4.  
  5. moves = [(a, b), (-a, b), (a, -b), (-a, -b), (b, a), (-b, a), (b, -a), (-b, -a)]
  6.  
  7. for move in moves:
  8. newX, newY = xK + move[0], yK + move[1]
  9.  
  10. if abs(newX - xQ) + abs(newY - yQ) == 3:
  11. valid_positions += 1
  12.  
  13. return valid_positions
  14.  
  15. # Example usage:
  16. xK, yK = 0, 0 # King's position
  17. xQ, yQ = 4, 4 # Queen's position
  18. a, b = 1, 2 # Knight's move
  19.  
  20. result = count_attack_positions(xK, yK, xQ, yQ, a, b)
  21. print(f"The number of positions where a knight can attack both king and queen is: {result}")
  22.  
Success #stdin #stdout 0.03s 9544KB
stdin
Standard input is empty
stdout
The number of positions where a knight can attack both king and queen is: 0