#K58232. Pacific Atlantic Water Flow
Pacific Atlantic Water Flow
Pacific Atlantic Water Flow
Given a grid of dimensions \(m \times n\) where each cell contains a positive integer representing its elevation, determine all cells from which water can flow to both the Pacific and Atlantic Oceans. Water can only flow from a cell to its adjacent cells (up, down, left, right) if the adjacent cell's height is greater than or equal to the current cell's height.
The Pacific Ocean touches the left and top borders of the grid, and the Atlantic Ocean touches the right and bottom borders. A cell is considered to be able to flow to an ocean if there exists a path (following the above rule) from the cell to any cell on that ocean's border.
Formally, if \(h_{i,j}\) is the height of cell \((i,j)\), water can flow from \((i,j)\) to \((i',j')\) if \(h_{i',j'} \geq h_{i,j}\). The task is to find all cells from which water can reach both oceans.
inputFormat
The first line contains two space-separated integers \(m\) and \(n\) (\(m,n \in \mathbb{N}\)) representing the number of rows and columns of the grid. Each of the following \(m\) lines contains \(n\) space-separated integers representing the elevation heights.
outputFormat
Output the coordinates of all cells from which water can flow to both the Pacific and Atlantic Oceans. Each line should contain two integers, representing the row and column indices (0-indexed) of a cell. The cells must be printed in lexicographical order (sorted first by row, then by column).
## sample5 5
1 2 2 3 5
3 2 3 4 4
2 4 5 3 1
6 7 1 4 5
5 1 1 2 4
0 4
1 3
1 4
2 2
3 0
3 1
4 0
</p>