#P1275. Magic Board Transformation
Magic Board Transformation
Magic Board Transformation
Consider an n × m magic board divided into n rows and m columns. Each cell contains a light bulb which can be either on (1) or off (0). You are given two states of the board, represented as binary matrices A and B.
You can perform the following two operations any number of times:
- Row Flip: Choose any row and toggle every bulb in that row (turning 1 to 0 and 0 to 1).
- Column Swap: Choose any two columns and swap their positions.
The task is to determine whether it is possible to transform board A into board B by applying a sequence of these operations.
Mathematically, there must exist a permutation \(\pi\) on \(\{1,2,\ldots,m\}\) and a binary function \(f:\{1,2,\ldots,n\}\to\{0,1\}\) (where \(f(i)=1\) if row \(i\) is flipped and 0 otherwise) such that for every \(1 \le i \le n\) and \(1 \le j \le m\) the following holds:
\( \displaystyle B_{i,j} = A_{i,\pi(j)} \oplus f(i) \)
where \(\oplus\) denotes the XOR operation.
inputFormat
The first line contains two integers n
and m
(the number of rows and columns).
The next n
lines each contain a binary string of length m
representing the initial board state A. Each character is either 0
or 1
.
This is followed by another n
lines, each containing a binary string of length m
representing the target board state B.
outputFormat
Output a single line containing YES
if it is possible to transform board A into board B using the allowed operations; otherwise, output NO
.
sample
2 2
01
10
01
10
YES