Collecting Apples
Time limit1sMemory limit512 MB
Find the Kth best monotone path from (1,1) to (R,C) in a grid, ranking paths by total apple count then by lexicographic order of the move string.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Combinatorics, Greedy, Sorting
- Solved
- No attempts yet
Problem
Santi is playing a game on a grid with rows (from top to bottom) and columns (from left to right). The cell in the th row and th column is called cell . Cell contains apples.
Santi's character Nano starts on cell and wants to move to cell . On each turn Nano can move only one cell down or one cell right, which brings it closer to the goal. Nano's path to the goal can be written as a string of characters, of which are ‘D’ and are ‘R’. This means that Nano moves one cell down on the th turn if the th character is ‘D’, and one cell right on the th turn if the th character is ‘R’.
Two paths are different if their string representations are different. There are different paths. Path is better than path if either of the following holds:
- Path collects more apples than . The number of apples Nano collects along a path is the total number of apples in the cells the path passes through, including cell and cell .
- Path collects the same number of apples as , and the string representation of is lexicographically smaller than the string representation of .
Finding the best path is too boring for Santi, so she wants to find the th best path instead. In other words, she wants to find a path such that paths are better than and paths are worse than .
For example, let , , , , and . Then there are three different paths to the goal, listed from best to worst.
- The path represented by the string
RRD. It collects apples. - The path represented by the string
DRR. It collects apples. - The path represented by the string
RDR. It collects apples.
Therefore, the rd best path is the path represented by the string RDR.
Input
The first line contains three integers (), which are the number of rows, the number of columns, and the index of the path Santi wants to find. The next lines each contain integers giving the number of apples in each cell. The th integer on the th line is ().
Output
Print one line containing the string that represents the th best path to the goal.