Skip to content

Commit 46963f0

Browse files
committed
Add Maze exercise and solution
1 parent 7eb0a06 commit 46963f0

File tree

5 files changed

+202
-3
lines changed

5 files changed

+202
-3
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.packt.datastructuresandalg.lesson6.activity.maze;
2+
3+
public class Maze {
4+
private int H;
5+
private int W;
6+
private int exitI;
7+
private int exitJ;
8+
private String[] maze;
9+
10+
public Maze(String[] maze) {
11+
this.maze = maze;
12+
this.H = maze.length;
13+
this.W = maze[0].length();
14+
for (int i = 0; i < W; i++) {
15+
if (maze[0].charAt(i) == '.') {
16+
this.exitI = 0;
17+
this.exitJ = i;
18+
return;
19+
}
20+
if (maze[H - 1].charAt(i) == '.') {
21+
this.exitI = H - 1;
22+
this.exitJ = i;
23+
return;
24+
}
25+
}
26+
for (int i = 1; i < H - 1; i++) {
27+
if (maze[i].charAt(0) == '.') {
28+
this.exitI = i;
29+
this.exitJ = 0;
30+
return;
31+
}
32+
if (maze[i].charAt(W - 1) == '.') {
33+
this.exitI = i;
34+
this.exitJ = W - 1;
35+
return;
36+
}
37+
}
38+
}
39+
40+
public int distToExit(int i, int j) {
41+
return -1;
42+
}
43+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.packt.datastructuresandalg.lesson6.activity.maze.solution;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
public class Maze {
7+
private int H;
8+
private int W;
9+
private int exitI;
10+
private int exitJ;
11+
private String[] maze;
12+
13+
public Maze(String[] maze) {
14+
this.maze = maze;
15+
this.H = maze.length;
16+
this.W = maze[0].length();
17+
for (int i = 0; i < W; i++) {
18+
if (maze[0].charAt(i) == '.') {
19+
this.exitI = 0;
20+
this.exitJ = i;
21+
return;
22+
}
23+
if (maze[H - 1].charAt(i) == '.') {
24+
this.exitI = H - 1;
25+
this.exitJ = i;
26+
return;
27+
}
28+
}
29+
for (int i = 1; i < H - 1; i++) {
30+
if (maze[i].charAt(0) == '.') {
31+
this.exitI = i;
32+
this.exitJ = 0;
33+
return;
34+
}
35+
if (maze[i].charAt(W - 1) == '.') {
36+
this.exitI = i;
37+
this.exitJ = W - 1;
38+
return;
39+
}
40+
}
41+
}
42+
43+
private class Pos {
44+
int i;
45+
int j;
46+
public Pos(int i, int j) {
47+
this.i = i;
48+
this.j = j;
49+
}
50+
}
51+
52+
public int distToExit(int i, int j) {
53+
int[][] dist = new int[H][W];
54+
int[][] directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
55+
56+
for (int h = 0; h < H; h++) {
57+
for (int w = 0; w < W; w++) {
58+
dist[h][w] = -1;
59+
}
60+
}
61+
62+
Queue<Pos> q = new LinkedList<>();
63+
dist[i][j] = 0;
64+
q.add(new Pos(i, j));
65+
while (!q.isEmpty()) {
66+
Pos current = q.remove();
67+
if (current.i == this.exitI && current.j == this.exitJ)
68+
return dist[current.i][current.j];
69+
for (int d = 0; d < directions.length; d++) {
70+
int ni = current.i + directions[d][0];
71+
int nj = current.j + directions[d][1];
72+
if (this.maze[ni].charAt(nj) == '.' && dist[ni][nj] == -1) {
73+
dist[ni][nj] = dist[current.i][current.j] + 1;
74+
q.add(new Pos(ni, nj));
75+
}
76+
}
77+
}
78+
return -1;
79+
}
80+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.packt.datastructuresandalg.lesson6.dfs;
2+
3+
import java.util.ArrayList;
4+
5+
public class Graph {
6+
ArrayList<Integer> adj[];
7+
8+
public Graph(int nodes) {
9+
this.adj = new ArrayList[nodes];
10+
for (int i = 0; i < nodes; i++) {
11+
this.adj[i] = new ArrayList<>();
12+
}
13+
}
14+
15+
public void addEdge(int u, int v) {
16+
this.adj[u].add(v);
17+
}
18+
19+
public int[] dfs() {
20+
boolean[] visited = new boolean[adj.length];
21+
int[] parent = new int[adj.length];
22+
for (int i = 0; i < adj.length; i++) {
23+
visited[i] = false;
24+
parent[i] = -1;
25+
}
26+
for (int i = 0; i < adj.length; i++) {
27+
if (!visited[i])
28+
dfsVisit(i, visited, parent);
29+
}
30+
return parent;
31+
}
32+
33+
public void dfsVisit(int u, boolean[] visited, int[] parent) {
34+
visited[u] = true;
35+
for (int i = 0; i < adj[u].size(); i++) {
36+
int next = adj[u].get(i);
37+
if (!visited[next]) {
38+
parent[next] = u;
39+
dfsVisit(next, visited, parent);
40+
}
41+
}
42+
}
43+
44+
public static void main(String[] args) {
45+
Graph g = new Graph(6);
46+
g.addEdge(0, 1);
47+
g.addEdge(0, 3);
48+
g.addEdge(1, 4);
49+
g.addEdge(2, 4);
50+
g.addEdge(2, 5);
51+
g.addEdge(3, 1);
52+
g.addEdge(4, 3);
53+
g.addEdge(5, 5);
54+
int[] p = g.dfs();
55+
for (int i = 0; i < 6; i++) {
56+
System.out.println("Parent of " + i + " in DFS forest is " + p[i]);
57+
}
58+
}
59+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.packt.datastructuresandalg.lesson6.activity.maze;
2+
3+
import junit.framework.TestCase;
4+
5+
public class MazeTest extends TestCase {
6+
public void test1() {
7+
Maze m = new Maze(new String[]{
8+
"####.##",
9+
"#....##",
10+
"#.#.#.#",
11+
"#.#...#",
12+
"#.###.#",
13+
"#.....#",
14+
"#######"
15+
});
16+
assertTrue(m.distToExit(1, 1) == 4);
17+
assertTrue(m.distToExit(5, 1) == 8);
18+
assertTrue(m.distToExit(2, 5) == 7);
19+
}
20+
}

src/test/java/com/packt/datastructuresandalg/lesson6/activity/weightedundirected/AdjacencyMatrixWeightedUndirectedTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
import junit.framework.TestCase;
44

5-
import java.util.ArrayList;
6-
import java.util.Arrays;
7-
85
public class AdjacencyMatrixWeightedUndirectedTest extends TestCase {
96
public void test1() {
107
AdjacencyMatrixWeightedUndirected g = new AdjacencyMatrixWeightedUndirected(5);

0 commit comments

Comments
 (0)