Skip to content

Commit

Permalink
Add Maze exercise and solution
Browse files Browse the repository at this point in the history
  • Loading branch information
jcazevedo committed Apr 15, 2018
1 parent 7eb0a06 commit 46963f0
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.packt.datastructuresandalg.lesson6.activity.maze;

public class Maze {
private int H;
private int W;
private int exitI;
private int exitJ;
private String[] maze;

public Maze(String[] maze) {
this.maze = maze;
this.H = maze.length;
this.W = maze[0].length();
for (int i = 0; i < W; i++) {
if (maze[0].charAt(i) == '.') {
this.exitI = 0;
this.exitJ = i;
return;
}
if (maze[H - 1].charAt(i) == '.') {
this.exitI = H - 1;
this.exitJ = i;
return;
}
}
for (int i = 1; i < H - 1; i++) {
if (maze[i].charAt(0) == '.') {
this.exitI = i;
this.exitJ = 0;
return;
}
if (maze[i].charAt(W - 1) == '.') {
this.exitI = i;
this.exitJ = W - 1;
return;
}
}
}

public int distToExit(int i, int j) {
return -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.packt.datastructuresandalg.lesson6.activity.maze.solution;

import java.util.LinkedList;
import java.util.Queue;

public class Maze {
private int H;
private int W;
private int exitI;
private int exitJ;
private String[] maze;

public Maze(String[] maze) {
this.maze = maze;
this.H = maze.length;
this.W = maze[0].length();
for (int i = 0; i < W; i++) {
if (maze[0].charAt(i) == '.') {
this.exitI = 0;
this.exitJ = i;
return;
}
if (maze[H - 1].charAt(i) == '.') {
this.exitI = H - 1;
this.exitJ = i;
return;
}
}
for (int i = 1; i < H - 1; i++) {
if (maze[i].charAt(0) == '.') {
this.exitI = i;
this.exitJ = 0;
return;
}
if (maze[i].charAt(W - 1) == '.') {
this.exitI = i;
this.exitJ = W - 1;
return;
}
}
}

private class Pos {
int i;
int j;
public Pos(int i, int j) {
this.i = i;
this.j = j;
}
}

public int distToExit(int i, int j) {
int[][] dist = new int[H][W];
int[][] directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

for (int h = 0; h < H; h++) {
for (int w = 0; w < W; w++) {
dist[h][w] = -1;
}
}

Queue<Pos> q = new LinkedList<>();
dist[i][j] = 0;
q.add(new Pos(i, j));
while (!q.isEmpty()) {
Pos current = q.remove();
if (current.i == this.exitI && current.j == this.exitJ)
return dist[current.i][current.j];
for (int d = 0; d < directions.length; d++) {
int ni = current.i + directions[d][0];
int nj = current.j + directions[d][1];
if (this.maze[ni].charAt(nj) == '.' && dist[ni][nj] == -1) {
dist[ni][nj] = dist[current.i][current.j] + 1;
q.add(new Pos(ni, nj));
}
}
}
return -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.packt.datastructuresandalg.lesson6.dfs;

import java.util.ArrayList;

public class Graph {
ArrayList<Integer> adj[];

public Graph(int nodes) {
this.adj = new ArrayList[nodes];
for (int i = 0; i < nodes; i++) {
this.adj[i] = new ArrayList<>();
}
}

public void addEdge(int u, int v) {
this.adj[u].add(v);
}

public int[] dfs() {
boolean[] visited = new boolean[adj.length];
int[] parent = new int[adj.length];
for (int i = 0; i < adj.length; i++) {
visited[i] = false;
parent[i] = -1;
}
for (int i = 0; i < adj.length; i++) {
if (!visited[i])
dfsVisit(i, visited, parent);
}
return parent;
}

public void dfsVisit(int u, boolean[] visited, int[] parent) {
visited[u] = true;
for (int i = 0; i < adj[u].size(); i++) {
int next = adj[u].get(i);
if (!visited[next]) {
parent[next] = u;
dfsVisit(next, visited, parent);
}
}
}

public static void main(String[] args) {
Graph g = new Graph(6);
g.addEdge(0, 1);
g.addEdge(0, 3);
g.addEdge(1, 4);
g.addEdge(2, 4);
g.addEdge(2, 5);
g.addEdge(3, 1);
g.addEdge(4, 3);
g.addEdge(5, 5);
int[] p = g.dfs();
for (int i = 0; i < 6; i++) {
System.out.println("Parent of " + i + " in DFS forest is " + p[i]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.packt.datastructuresandalg.lesson6.activity.maze;

import junit.framework.TestCase;

public class MazeTest extends TestCase {
public void test1() {
Maze m = new Maze(new String[]{
"####.##",
"#....##",
"#.#.#.#",
"#.#...#",
"#.###.#",
"#.....#",
"#######"
});
assertTrue(m.distToExit(1, 1) == 4);
assertTrue(m.distToExit(5, 1) == 8);
assertTrue(m.distToExit(2, 5) == 7);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import junit.framework.TestCase;

import java.util.ArrayList;
import java.util.Arrays;

public class AdjacencyMatrixWeightedUndirectedTest extends TestCase {
public void test1() {
AdjacencyMatrixWeightedUndirected g = new AdjacencyMatrixWeightedUndirected(5);
Expand Down

0 comments on commit 46963f0

Please sign in to comment.