Hate Stagnant Water
InterviewTime limit1sMemory limit1024 MB
Count the h by w rectangles on an N by M grid that contain no standing water, where drained cells spread to neighbors at equal or higher height.
- Level
Medium4 of 10
- Topics
- BFS, Prefix sum, Matrix
- Solved
- No attempts yet
Problem
On a rainy day, Sejong went out to buy water for his parents. While waiting at a crosswalk, he saw water pooling on it because the crosswalk was not flat.
The crosswalk is an rectangular grid. The cell at the front row on Sejong's side, far left, is row 1, column 1. The cell far right in that row is row 1, column .
Drains are installed in cells of the crosswalk. A cell drains water if one of its vertically or horizontally adjacent cells has a drain or is already drained, and that adjacent cell's height is lower than or equal to its own. A cell without a drain that does not drain is called a standing water cell.
Sejong solved A+B and got new shoes. He does not want to dirty them, so he tries to cross without stepping on any standing water cell. His new shoes are a rectangle that covers consecutive rows and consecutive columns. When Sejong steps, the shoe always fits exactly onto the grid cells, and he never turns his foot.
Given the height of each cell, write a program that counts the number of ways Sejong can place one foot on the crosswalk without dirtying his shoes.
Input
The first line contains two integers and , separated by a space. ()
The second line contains two integers and , the length and width of Sejong's shoes, separated by a space. (, )
The next lines contain integers each. On line , the integers are separated by spaces. is the height of the cell at row , column . ()
Line contains an integer , the number of drains. ()
The next lines each contain two integers and , separated by a space, meaning there is a drain at row , column . No position is repeated. (, )
Output
Print the number of ways Sejong can place one foot on the crosswalk.