fb pixel

Algorithms and Data Structures

  • Profound Academy

    • Status
      • 1
      • 2
        Bitwise operations
      • 3
        Prefix Sums
      • 4
        Sliding window / Two pointers
      • 5
        Modular Arithmetic
      • 6
        Number Theory
      • 7
        Binary Search
      • 8
        Basic Sorting
      • 9
        Greedy Algorithms

  • Capture the knight

    Given an chessboard, one white knight at position and one black knight at position, you are asked if one can capture the other in 1 or 2 moves.
    A knight captures another knight if it moves to the same location as the other one.
    As a reminder, in one move, the knight moves 2 cells in one direction and 1 cell in another perpendicular to the first direction. So, it can move 2 cells up and 1 cell left or right. It can move 2 cells right and 1 cell up or down. It can move 2 cells down and one cell left or right, etc.
    notion image
    In this problem, the second knight stays in the same place, while the first one does exactly two moves.


    The input contains 2 lines. The first line contains two coordinates indicating the first knight , while the second line contains the coordinates of the second knight (1 ≤ ≤ 8). It’s guaranteed that is different from .


    The program should print Yes in case the first knight can capture the second one in 1 or 2 moves and No otherwise.


    1 1 1 3
    1 1 8 8


    notion image
    Impossible in 2 moves
    notion image
    To check your solution you need to sign in
    Sign in to continue