Loading...
Loading...
You are given a list of strings grid describing a maze with n rows and m columns, and an integer k. The cell in row i and column j is empty when grid[i][j] is '0', and blocked when it is 'X'.
A rat starts at the top-left cell (0, 0) and walks to the bottom-right cell (n - 1, m - 1). Each step moves one cell right or one cell down, and the rat never enters a blocked cell. Write a valid path as the string of its moves: 'E' for a step right and 'S' for a step down.
Return the k-th valid path when all valid paths are sorted in alphabetical order, where 'E' comes before 'S'. The paths are numbered starting from 1. When the start cell is also the destination, the only valid path is the empty string "".
You can assume there are at least k distinct valid paths from the top-left cell to the bottom-right cell.
Input: grid = ["00000","00X00","000X0","0X000"], k = 3
Output: "ESSESEE"
Explanation: There are five valid paths, in alphabetical order: `EEEESSS`, `EEESESS`, `ESSESEE`, `SESESEE`, `SSEESEE`. The third is `ESSESEE`.
Input: grid = ["00","00"], k = 2
Output: "SE"
Explanation: The two paths are `ES` and `SE`, in that order, so the second is `SE`.
Input: grid = ["000","000","000"], k = 6
Output: "SSEE"
Explanation: With no blocked cells there are 6 paths, and the last one alphabetically is `SSEE`.
grid.length ≤30grid[i].length ≤30grid have the same length.grid[i][j] is either '0' or 'X'.grid[0][0] is '0' and grid[n - 1][m - 1] is '0'.k ≤109k distinct valid paths from (0, 0) to (n - 1, m - 1).