Takashi is playing a puzzle game. The puzzle involves placing multiple puzzle pieces on a square
board to fill it completely without any gaps or overlaps. Your task is to write a program to check
whether the board and the given pieces can form a valid puzzle.
Rules & Definitions
The Board:
• The board is a square of size N * N, divided into 1 * 1 squares.
The Pieces:
• There are M pieces in total.
• Each piece consists of one or more 1 * 1 tiles.
• The tiles within a single piece must be connected by sharing an entire side (not just a
corner).
Placement Rules:
- All Pieces Used: All M pieces must be used.
- No Overlap: Multiple pieces must not overlap on any 1* 1 square.
- In-Bounds: A piece must not extend beyond the edge of the N* N board.
- Rotation Allowed: Pieces can be rotated (by 90, 180, or 270 degrees).
- No Flipping: Pieces can not be turned over (flipped).
- Exact Fit: The puzzle is only valid if the board is exactly filled by all the pieces, with no
gaps remaining.
Input Format
The input is provided via standard input in the following format:
- The first line contains two integers, N and M, separated by a space.
- The input is then followed by M blocks, one for each piece.
- Each block consists of N lines, and each line is a string of N characters.
o The j-th line of the i-th piece is denoted by Si, j.
o The character # indicates a square that is part of the piece.
o The character . indicates a square that is not part of the piece.
Input Structure:
N M
S_1,1
S_1,2
...
S_1,N
S_2,1
...
S_2,N
...
S_M,1
...
S_M,N
Output Format
Output Yes if the board can be filled exactly with all the given pieces according to the rules.
Otherwise, output No.