Two-dimensional domino
Each piece of a two-dimensional domino is a square, with sides numbered from 0 to 3 in binary notation. A domino is correctly assembled if every pair of adjacent domino squares has the same number on their shared side.
Your task is to determine if the domino is correctly assembled and whether it's possible to add another specified element so that it shares a side with at least one of the existing squares. The new element can be rotated in any direction but cannot be flipped.
Input Data
The first line of the input specifies the dimensions of the rectangle containing the already assembled domino: 2 ≤ N
, M ≤ 100
. Each of the following N
lines describes M
positions in the rectangle in the following format:
An empty position is denoted by ZZ.
A non-empty position is represented by a hexadecimal text of one byte, where each pair of bits indicates the number on one of the sides. The sides are listed clockwise, starting from the left.
The last line contains a description of the element proposed to be added to the assembled set. It is assumed that the addition occurs within the same rectangle N×M.
Output Data
The output should be one of the following three words:
ERROR (the domino is incorrectly assembled);
YES (the domino is correctly assembled, and the element can be added);
NO (the domino is correctly assembled, but the element cannot be added).