#C242. Minimum Moves in a Grid

    ID: 45734 Type: Default 1000ms 256MiB

Minimum Moves in a Grid

Minimum Moves in a Grid

Given a grid with dimensions \( m \times n \), a starting position, and a destination position, compute the minimum number of movements required to travel from the start to the destination using Manhattan distance. Each move is one step in one of the four cardinal directions (up, down, left, or right).

The program must validate the input. The grid dimensions must be positive integers, and both the starting and destination coordinates must lie within the grid boundaries. If any of these conditions is violated, output the corresponding error message as described below.

Error messages:

  • If \( m \) or \( n \) is not positive: "Grid dimensions must be positive integers".
  • If the starting coordinates are out of bounds: "Start coordinates out of grid boundaries".
  • If the destination coordinates are out of bounds: "Destination coordinates out of grid boundaries".

inputFormat

The input is read from standard input (stdin) and consists of three lines:

  • The first line contains two integers representing the number of rows and columns \( m \) and \( n \) of the grid.
  • The second line contains two integers representing the row and column indices of the starting position.
  • The third line contains two integers representing the row and column indices of the destination position.

All indices are 0-indexed.

outputFormat

Output a single line to standard output (stdout). If the input is valid, output an integer which is the minimum number of moves required to travel from the start to the destination using the Manhattan distance. If the input is invalid, output the corresponding error message exactly as described.

## sample
5 5
0 0
4 4
8