Comets
Time limit1sMemory limit1024 MB
Find the top-left cell for a KxK station in an NxM grid of moving comets so the first hit happens as late as possible, breaking ties by row, then column.
- Level
Medium7 of 10
- Topics
- Sliding window, Matrix, Dynamic programming
- Solved
- No attempts yet
Problem
NASA wants to place a new station in space. For this problem, space is shown as a matrix of cells arranged in rows and columns. The station is square, with rows and columns. Its sides must be parallel to the sides of the space, and it must lie entirely inside the space.
Comets are in space. Each comet moves in one of four directions: up, down, left, or right. Each cell can initially hold at most one comet. Each comet moves one cell per second. Comets never change direction.
If a comet is on a cell occupied by the station at some moment, we say the comet hit the station. If a comet leaves the matrix, it disappears. At any moment after the start, several comets may share a cell, and they pass through each other.
The station cannot be placed so that a cell holds both the station and a comet. It is guaranteed that the station can be placed at least at one position. The position of the station is defined by the cell occupied by its upper left corner.
NASA wants the best position. They want the first comet hit to happen as late as possible (or never). If several positions are equally good, choose the one with the smallest row index, and among those, the smallest column index.
Input
The first line contains two natural numbers and ().
The second line contains a natural number ().
Each of the next lines contains characters describing the initial state. The character '.' means the cell has no comet. The characters 'U', 'D', 'L', and 'R' mean a comet is on that cell moving up, down, left, or right.
Output
Print one line with two natural numbers separated by a space: the row and column of the cell where the upper left corner of the station should be placed.
Hints
In the first sample, the station placed at row 3, column 2 is never hit by a comet.
In the third sample, placing the station at row 2, column 3 or at row 3, column 2 makes the first comet hit after three seconds. Every other valid cell gives an earlier hit. Since we want the topmost and then leftmost cell, we choose row 2, column 3.