#B3707. Chess Piece Capture Logic

    ID: 11366 Type: Default 1000ms 256MiB

Chess Piece Capture Logic

Chess Piece Capture Logic

You are helping to design a chess software with a special logic function. Consider an infinite board where every cell is identified by coordinates \((x, y)\) (i.e. row \(x\) and column \(y\)). There are only two pieces on the board.

The first piece has a given type \(p\) and is located at \((x_0, y_0)\). Its type \(p\) is one of the following four:

  • Chariot (\(p=1\)): Moves in horizontal or vertical directions. In one move, it can capture a piece at \((x_0+k, y_0)\), \((x_0-k, y_0)\), \((x_0, y_0+k)\) or \((x_0, y_0-k)\) where \(k\) is any positive integer.
  • Cannon (\(p=2\)): The cannon can capture a target piece only if both pieces lie on the same straight line AND there is exactly one other piece between them along that line. Note: Since the board only has these two pieces, the cannon will never capture.
  • Horse (\(p=3\)): Moves in an L-shape ("horse move"). In one move, it can capture a piece at one of the positions: \((x_0+2, y_0+1)\), \((x_0+2, y_0-1)\), \((x_0-2, y_0+1)\), \((x_0-2, y_0-1)\), \((x_0+1, y_0+2)\), \((x_0+1, y_0-2)\), \((x_0-1, y_0+2)\), or \((x_0-1, y_0-2)\).
  • Elephant (\(p=4\)): Moves diagonally by exactly two steps in both directions. That is, it can capture a piece at \((x_0+2, y_0+2)\), \((x_0+2, y_0-2)\), \((x_0-2, y_0+2)\), or \((x_0-2, y_0-2)\).

The second piece is located at \((x_1, y_1)\), and its type is irrelevant.

Your task is to decide whether the first piece, with a single move, can capture the second piece.

inputFormat

The input consists of a single line containing five space-separated integers: \(p\), \(x_0\), \(y_0\), \(x_1\), \(y_1\). \(p\) is the type of the first piece (1 for Chariot, 2 for Cannon, 3 for Horse, 4 for Elephant). \((x_0, y_0)\) is the position of the first piece, and \((x_1, y_1)\) is the position of the second piece.

outputFormat

Output YES if the first piece can capture the second piece in one move according to the specified rules; otherwise, output NO.

sample

1 1 1 1 5
YES