#K45302. Validate Battleship Ship Positions

    ID: 27723 Type: Default 1000ms 256MiB

Validate Battleship Ship Positions

Validate Battleship Ship Positions

You are given a grid of fixed size \(10 \times 10\) with indices ranging from \(0\) to \(9\) along both rows and columns. Several battleship ships are placed on this grid. Each ship occupies exactly \(4\) contiguous grid cells in either a horizontal or vertical line. No two ships should overlap.

Your task is to check whether a given set of ship positions is valid. A valid configuration satisfies the following conditions:

  • All positions are within the bounds of the grid.
  • No cell is used by more than one ship.
  • The positions form connected groups. Each connected component must contain exactly \(4\) cells.
  • Each connected component representing a ship must form a straight line (all cells in the same row with consecutive columns, or all cells in the same column with consecutive rows).

If all ships are valid according to the above rules, the output is True, otherwise False.

Input is read from standard input and output is printed to standard output.

inputFormat

The first line contains an integer (n) representing the number of ship positions. The next (n) lines each contain two integers separated by space, representing the row and column of a ship cell.

outputFormat

Print "True" if the ship positions form valid ships according to the above rules; otherwise print "False".## sample

4
0 0
0 1
0 2
0 3
True