Tetris Game Validator#1877

Tetris Game Validator

2 seconds
256 megabytes
Hard

Recall the Tetris problem statement:

In the game of Tetris, connected figures (connected if they can move in four directions) fall into a well with rows and columns. Each figure is represented by a unique letter from the English alphabet, ranging from A to Z. While falling, the figures cannot be moved or rotated. Your program must determine the sequence in which the blocks fell, based on the well's description.

Input

The first line of the input file contains an integer (0 \leq N \leq 50) — the number of rows in the well. Each of the following rows contains characters, each being either a letter from A to Z or the character . (ASCII ), representing an empty cell.

You must write a test validator for this problem.

The input file contains multiple tests. Each test is provided in the following format:

A number (1 \leq N \leq 50) and rows of characters each.

Characters may have ASCII codes from to .

The total sum of across all tests will not exceed .

Output

For each test, output YES or NO — whether the test is valid.

A test is considered valid if all of the following conditions are met:

  • Only characters A..Z and . are used.

  • A figure (that is, the set of all cells with a given letter) forms a connected area.

  • No figure is hanging in the air.

  • The figures could actually have fallen in such an order.

Examples

Example 1

Loading
Loading