3
string kthSmallestPath(vector<int> &destination, int k) {4
int n = destination[0] + 1, m = destination[1] + 1;6
// this code is straight forward it calculate and stores no. ways to go to7
// destination from i,j8
vector<vector<int>> ways(n, vector<int>(m));10
for (int i = n - 1; i >= 0; i--) {11
for (int j = m - 1; j >= 0; j--) {12
if (i == n - 1 || j == m - 1) {16
ways[i][j] = ways[i][j + 1] + ways[i + 1][j];20
// with the help of above table ans can be generated21
// suppose we are at (i, j) we need to decide whether to go Horizontal or27
while (i < n - 1 && j < m - 1) {28
// if k is less than no of ways to go from selecting horizontal path29
if (k <= ways[i][j + 1]) {33
// else we chose vertical for greater k35
k = k - ways[i][j + 1];40
while (j++ < m - 1) ans = ans + 'H';41
while (i++ < n - 1) ans = ans + 'V';