Decide if row and column cuts of a mower that only lowers grass can produce the given target heights from a uniform lawn.
Medium4GreedyMatrixInterviewNo attempts yetTime limit5sMemory limit512 MBAlice and Bob have a lawn in front of their house, an N metre by M metre rectangle divided into 1m x 1m squares. Every square starts with grass 100 millimetres high.
Their new lawnmower has a height setting. Set it to any integer height h between 1 and 100 millimetres and it cuts every square it passes over whose grass is higher than h down to height h, leaving squares that are already at most h untouched. You run it by entering the lawn at any point on its edge; the mower then goes in a straight line perpendicular to that edge, cutting a swath 1 metre wide, until it exits on the other side. One run therefore cuts exactly one whole row or one whole column. The height can be changed only while the mower is off the lawn, so a single run uses a single height, but you may make as many runs as you want, in any order, at any heights.
You are given a pattern that states the desired height of the grass on every square. Decide whether some sequence of runs turns the lawn into exactly that pattern.
The first line holds the number of test cases T. Each test case begins with a line holding two integers N and M. The next N lines follow, and the i-th of them holds M integers ai,j, the desired height of the grass on the j-th square of the i-th row.
Limits
For each test case, print one line in the form Case #x: y, where x is the case number starting from 1 and y is YES if the pattern can be produced with the lawnmower and NO if it cannot.