#C6585. Special Grid Transformation

    ID: 50361 Type: Default 1000ms 256MiB

Special Grid Transformation

Special Grid Transformation

You are given one or more grids consisting of 0s and 1s. Your task is to determine if each grid can be transformed into a special pattern, i.e. a checkerboard pattern where no two adjacent cells (horizontally or vertically) have the same value.

A grid is said to match the special pattern if it is identical to one of the two possible checkerboard configurations. In mathematical terms, the two valid patterns can be defined as:

  • Pattern 1: \( pattern(i,j) = (i+j) \mod 2 \)
  • Pattern 2: \( pattern(i,j) = (i+j+1) \mod 2 \)

Check whether the given grid matches either of these patterns. If it does, output YES; otherwise, output NO for that test case.

inputFormat

The first line contains an integer \( T \) representing the number of test cases. Each test case begins with a line containing two space-separated integers \( R \) and \( C \) representing the number of rows and columns, respectively. The next \( R \) lines each contain \( C \) space-separated integers (either 0 or 1) representing the grid.

Example:

3
2 2
0 1
1 0
3 2
1 1
1 0
0 1
2 3
1 0 1
0 1 0

outputFormat

For each test case, output a single line containing YES if the grid can be transformed into the special pattern, or NO otherwise.

Example:

YES
NO
YES
## sample
3
2 2
0 1
1 0
3 2
1 1
1 0
0 1
2 3
1 0 1
0 1 0
YES

NO YES

</p>