Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 540e8d1

Browse files
recursion
1 parent bf19682 commit 540e8d1

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class RatInAMaze {
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+
while (t-- > 0) {
27+
input();
28+
}
29+
br.close();
30+
}
31+
32+
public static void input() throws IOException {
33+
int n = nextInt();
34+
int m[][] = new int[n][n];
35+
boolean vis[][] = new boolean[n][n];
36+
// {1, 0, 0, 0},
37+
// {1, 1, 0, 1},
38+
// {1, 1, 0, 0},
39+
// {0, 1, 1, 1}
40+
for (int i = 0; i < n; i++) {
41+
for (int j = 0; j < n; j++) {
42+
m[i][j] = nextInt();
43+
}
44+
}
45+
46+
if (m[0][0] == 1) {
47+
vis[0][0] = true;
48+
System.out.println(findPath(m, n, vis));
49+
}
50+
}
51+
52+
static ArrayList<String> findPath(int[][] m, int n, boolean[][] vis) {
53+
String pos = "";
54+
ArrayList<String> ans = new ArrayList<>();
55+
56+
dfs(pos, 0, 0, m, ans, vis, n);
57+
58+
return ans;
59+
60+
}
61+
62+
static void dfs(String pos, int row, int col, int[][] m, ArrayList<String> ans, boolean[][] vis,
63+
int n) {
64+
65+
if (col == n - 1 && row == n - 1) {
66+
ans.add(pos.toString());
67+
return;
68+
}
69+
if (row < n - 1 && m[row + 1][col] == 1 && vis[row + 1][col] == false) {
70+
71+
vis[row][col] = true;
72+
dfs(pos + "D", row + 1, col, m, ans, vis, n);
73+
vis[row][col] = false;
74+
75+
}
76+
if (col > 0 && m[row][col - 1] == 1 && vis[row][col - 1] == false) {
77+
78+
vis[row][col] = true;
79+
dfs(pos + "L", row, col - 1, m, ans, vis, n);
80+
vis[row][col] = false;
81+
82+
}
83+
if (col < n - 1 && m[row][col + 1] == 1 && vis[row][col + 1] == false) {
84+
85+
vis[row][col] = true;
86+
dfs(pos + "R", row, col + 1, m, ans, vis, n);
87+
vis[row][col] = false;
88+
89+
}
90+
if (row > 0 && m[row - 1][col] == 1 && vis[row - 1][col] == false) {
91+
92+
vis[row][col] = true;
93+
dfs(pos + "U", row - 1, col, m, ans, vis, n);
94+
vis[row][col] = false;
95+
96+
}
97+
98+
}
99+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /