#K6026. Pacific Atlantic Water Flow
Pacific Atlantic Water Flow
Pacific Atlantic Water Flow
You are given an m x n matrix of non-negative integers representing the height of each unit cell in a continent. The "Pacific Ocean" touches the left and top edges of the matrix and the "Atlantic Ocean" touches the right and bottom edges.
Water can flow from a cell to another one with an equal or lower height. Water can flow in the four cardinal directions (north, south, east, and west).
Your task is to find all coordinates (row, column) from which water can flow to both the Pacific and Atlantic oceans.
Note: When comparing heights, water only flows from a cell with a given height to an adjacent cell with a height greater than or equal to it. Formally, for adjacent cells at coordinates (i, j) and (k, l), water flows if
You should output the coordinates sorted in increasing order by row and then column.
inputFormat
The first line of input contains two integers m and n representing the number of rows and columns of the matrix. The next m lines each contain n space-separated integers representing the heights of the cells.
outputFormat
Output the coordinates of all cells (one per line) that can flow to both the Pacific and Atlantic oceans. Each line should contain two integers representing the row and column of the cell. The coordinates must be sorted in increasing order first by the row index and then by the column index.## sample
5 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>