Associated Course(s):
Must Do Interview Preparation

Consider a rat placed at (**0, 0**) in a square matrix **m[][]** of order n and has to reach the destination at (**n-1, n-1**). Your task is to complete the function **printPath()** which returns a sorted array of strings denoting all the possible directions which the rat can take to reach the destination at (n-1, n-1). The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right).

**Input:**

The first line of input contains an integer **T** denoting the no of test cases. Then T test cases follow. Each test case contains two lines. The first line contains an integer n denoting the size of the square matrix. The next line contains n*n space separated values of the matrix m where 0's represents blocked paths and 1 represent valid paths.

**Output:**

For each test case output will be space separated sorted strings denoting all directions, which the rat could take to reach the destination.

**User Task:**

Since this is functional problem you don't have to worry about input, you just have to complete the function **printPath()**

**Constraints:**

1 <= T <= 10

2 <= n <= 10

0 <= m[][] <= 1

**Example
Input:**

2

4

1 0 0 0 1 1 0 1 0 1 0 0 0 1 1 1

4

1 0 0 0 1 1 0 1 1 1 0 0 0 1 1 1

DRDDRR

DDRDRR DRDDRR

**Explanation:
Testcase 2: **The given input is in the form

1 0 0 0

1 1 0 1

1 1 0 0

0 1 1 1

For the above matrix the rat can reach the destination at (3, 3) from (0, 0) by two paths ie DRDDRR and DDRDRR when printed in sorted order we get DDRDRR DRDDRR.

Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

bruceewayne | 193 |

manvirag982 | 182 |

Core_Ka_Bachha | 180 |

shivam123ahuja | 175 |

Sanjana Singh 1 | 173 |

mr_kksparrow | 433 |

manvirag982 | 398 |

TheHandle | 291 |

rajupraaa1234 | 276 |

Exception_404 | 268 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.