1
+ import java .io .*;
2
+ import java .util .*;
3
+
4
+ class RatInMazeLoop {
5
+ static BufferedReader br ;
6
+ static StringTokenizer st ;
7
+
8
+ static int nextInt () throws IOException {
9
+ return Integer .parseInt (next ());
10
+ }
11
+
12
+ static String next () throws IOException {
13
+ while (st == null || !st .hasMoreTokens ()) {
14
+ st = new StringTokenizer (br .readLine ());
15
+ }
16
+ return st .nextToken ();
17
+ }
18
+
19
+ static long nextLong () throws IOException {
20
+ return Long .parseLong (next ());
21
+ }
22
+
23
+ public static void main (String args []) throws IOException {
24
+ br = new BufferedReader (new InputStreamReader (System .in ));
25
+ int t = nextInt ();
26
+ // int t = 1;
27
+ while (t -- > 0 ) {
28
+ input ();
29
+ }
30
+ br .close ();
31
+ }
32
+
33
+ public static void input () throws IOException {
34
+ int n = nextInt ();
35
+ // int n = 4;
36
+ int m [][] = new int [n ][n ];
37
+ int vis [][] = new int [n ][n ];
38
+ // int[][] m = { { 1, 0, 0, 0 },
39
+ // { 1, 1, 0, 1 },
40
+ // { 1, 1, 0, 0 },
41
+ // { 0, 1, 1, 1 } };
42
+ for (int i = 0 ; i < n ; i ++) {
43
+ for (int j = 0 ; j < n ; j ++) {
44
+ m [i ][j ] = nextInt ();
45
+ }
46
+ }
47
+
48
+ System .out .println (findPath (m , n , vis ));
49
+
50
+ }
51
+
52
+ static void solve (int i , int j , int [][] m , int n , List <String > ans , String move , int [][] vis , int [] di ,
53
+ int [] dj ) {
54
+ if (i == n - 1 && j == n - 1 ) {
55
+ ans .add (move );
56
+ return ;
57
+ }
58
+ String dir = "DLRU" ;
59
+
60
+ for (int ind = 0 ; ind < 4 ; ind ++) {
61
+ int nexti = i + di [ind ];
62
+ int nextj = j + dj [ind ];
63
+
64
+ if (nexti >= 0 && nextj >= 0 && nexti < n && nextj < n && vis [nexti ][nextj ] == 0
65
+ && m [nexti ][nextj ] == 1 ) {
66
+ vis [i ][j ] = 1 ;
67
+ solve (nexti , nextj , m , n , ans , move + dir .charAt (ind ), vis , di , dj );
68
+ vis [i ][j ] = 0 ;
69
+ }
70
+
71
+ }
72
+ }
73
+
74
+ static List <String > findPath (int [][] m , int n , int [][] vis ) {
75
+ int di [] = { 1 , 0 , 0 , -1 };
76
+ int dj [] = { 0 , -1 , 1 , 0 };
77
+ for (int i = 0 ; i < n ; i ++) {
78
+ for (int j = 0 ; j < n ; j ++) {
79
+ vis [i ][j ] = 0 ;
80
+ }
81
+ }
82
+ List <String > ans = new ArrayList <>();
83
+ if (m [0 ][0 ] == 1 ) {
84
+ solve (0 , 0 , m , n , ans , "" , vis , di , dj );
85
+ }
86
+ return ans ;
87
+ }
88
+
89
+ }
0 commit comments