Water Flow

0

0 votes
Problem

You are given an m x n integer matrix heights representing the height of each unit cell in a continent. The Pacific ocean touches the continent's left and top edges, and the Atlantic ocean touches the continent's right and bottom edges.

Water can only flow in four directions: up, down, left, and right. Water flows from a cell to an adjacent one with an equal or lower height.

Print all the grid coordinates where water can flow to both the Pacific and Atlantic oceans.

Constraints:

m == heights.length

n == heights[i].length

1 <= m, n <= 200

1 <= heights[i][j] <= 105

 

Input

Input is given from Standard Input in the following format.

m n

X00 X01   ....... X0n

.

.

Xm0 Xm1 .........Xmn

Output

Print all the coordinates(space separated) in new line.

 

 

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

Editor Image

?