Peaks
Time limit2sMemory limit256 MB
Count grid cells that stay locally maximal when reachability is restricted to cells within height d of the starting cell, for many test cases up to 500x500.
- Level
Hard8 of 10
- Topics
- Union-find, Sorting, Graph
- Solved
- No attempts yet
Problem
Sang-geun works at a company that makes large maps. His current task is to find the peaks of mountains in a landscape.
You are given a grid map where each cell has a height. Consider a cell whose height is . Starting from this cell, you may move between orthogonally adjacent cells (up, down, left, right), but you are never allowed to step onto a cell whose height is or lower. If, under this restriction, it is impossible to reach any cell taller than , then the cell is called a -peak. In other words, a cell is a -peak if, using only cells with height greater than , you cannot walk to any cell higher than itself.
For example, a cell that is only slightly lower than a summit is not a peak if it connects directly to a higher summit without ever descending to low ground. Conversely, if reaching any higher cell forces you to pass through some sufficiently low cell (height or lower), then the cell is a -peak.
Given the height of every cell, write a program that counts how many cells are -peaks.
Input
The first line contains the number of test cases ().
The first line of each test case contains the map's number of rows , number of columns , and an integer (, ). Each of the next lines contains integers, the heights of the cells ().
Output
For each test case, print the number of -peaks on its own line.