Phoenix and Conquering the Earth
Time limit2sMemory limit1024 MB
Decide whether grid A can be turned into grid B by repeatedly adding or subtracting 1 on both cells of a free domino, where volcano cells cannot be used.
- Level
Medium6 of 10
- Topics
- Graph, Union-find, Matrix
- Solved
- No attempts yet
Problem
Phoenix wants to reshape the terrain of the Earth as he likes so that he becomes its true ruler. The Earth is square, so it is represented as a grid with rows and columns, and the value of each cell is the height of that land. All heights are integers.
Using his palm, Phoenix can press down or lift the land, changing the heights of two adjacent cells forming a or shape by or at the same time.
However, cells contain erupted volcanoes. If either of the two cells to be changed contains a volcano, Phoenix is afraid his hand will get burned and does not perform that operation. Phoenix may perform the operation many times to change every height to the value he wants. Determine whether Phoenix can change the heights of the Earth as he wants and become its true ruler.
Input
The first line contains , , and , separated by spaces. (, )
From the second line, the next lines each contain integers , separated by spaces. is the current height of the cell in row and column .
From line , the next lines each contain integers , separated by spaces. is the height that Phoenix wants the cell in row and column to have. ()
From line , the next lines each contain two integers and , separated by a space, giving the position of a volcano. (, )
All volcano positions are distinct.
Output
If Phoenix can perform the operations appropriately to change the heights from to , print Yes. Otherwise, print No.