Matrix Transformation
InterviewTime limit1sMemory limit1024 MB
Given an integer grid, decide whether repeatedly adding or subtracting 1 from both cells of an adjacent pair can turn every cell into zero.
- Level
Medium4 of 10
- Topics
- Math, Matrix, Implementation
- Solved
- No attempts yet
Problem
You have an integer matrix A with R rows and C columns. Each of the R rows contains C integers. Two integers are adjacent if their cells share an edge. For example, in the grid below, (0, 1), (4, 5), (1, 4), and (5, 2) are adjacent, but (0, 4), (2, 6), and (5, 7) are not.

You may perform only one kind of operation. In each step, you select two adjacent cells and either increase both values by 1 or decrease both values by 1.
Determine whether the matrix can be transformed into a zero matrix, where every entry is zero, using these operations.
Input
The first line contains a positive integer n, the number of matrices. Each matrix starts with a line containing R and C separated by a single space (, ). Each of the next R lines contains C integers, each between and inclusive. Each input matrix has at least one nonzero value.
Output
For each matrix (test case), first print "Case #i:", where i is the test case number starting at 1. Then print "YES" if the matrix can be transformed into a zero matrix, or "NO" otherwise. Leave a blank line after the output for each test case. Follow the format of the sample output.