-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
202 additions
and
3 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/main/java/com/packt/datastructuresandalg/lesson6/activity/maze/Maze.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/com/packt/datastructuresandalg/lesson6/activity/maze/solution/Maze.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/packt/datastructuresandalg/lesson6/dfs/Graph.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/test/java/com/packt/datastructuresandalg/lesson6/activity/maze/MazeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters