This repository was archived by the owner on Apr 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths_in_a_labyrinth.java
More file actions
96 lines (84 loc) · 2.71 KB
/
paths_in_a_labyrinth.java
File metadata and controls
96 lines (84 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Find All Paths in a Labyrinth
// You are given a labyrinth. Your goal is to find all paths from the start (cell 0, 0) to the exit, marked with 'e'.
// • Empty cells are marked with a dash '-'.
// • Walls are marked with a star '*'.
// On the first line, you will receive the dimensions of the labyrinth. Next, you will receive the actual labyrinth.
// The order of the paths does not matter.
// Examples
// Input Output
// 3
// 3
// ---
// -*-
// --e
// RRDD
// DDRR
// 3
// 5
// -**-e
// -----
// *****
// DRRRRU
// DRRRUR
// Hints
// Create methods for reading and finding all paths in the labyrinth.
// Create a static list that will hold every direction (basically the path).
// Finding all paths should be recursive.
// Implement all helper methods that are present in the code above.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class PathsInALabyrinth {
private static char[][] labyrinth;
private static List<Character> path = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int rows = Integer.parseInt(scanner.nextLine());
int cols = Integer.parseInt(scanner.nextLine());
labyrinth = new char[rows][cols];
for (int row = 0; row < rows; row++) {
labyrinth[row] = scanner.nextLine().toCharArray();
}
findPath(0, 0, 'S');
}
private static void findPath(int row, int col, char direction) {
if (!isInBounds(row, col)) {
return;
}
path.add(direction);
if (labyrinth[row][col] == 'e') {
printPath();
} else if (!isVisited(row, col) && !isWall(row, col)) {
mark(row, col);
findPath(row, col + 1, 'R');
findPath(row + 1, col, 'D');
findPath(row, col - 1, 'L');
findPath(row - 1, col, 'U');
unmark(row, col);
}
path.remove(path.size() - 1);
}
private static void unmark(int row, int col) {
labyrinth[row][col] = '-';
}
private static void mark(int row, int col) {
labyrinth[row][col] = 'v';
}
private static boolean isWall(int row, int col) {
return labyrinth[row][col] == '*';
}
private static boolean isVisited(int row, int col) {
return labyrinth[row][col] == 'v';
}
private static boolean isInBounds(int row, int col) {
return row < labyrinth.length && row >= 0 && col < labyrinth[row].length && col >= 0;
}
private static void printPath() {
for (Character direction : path) {
if (direction != 'S') {
System.out.print(direction);
}
}
System.out.println();
}
}