Skip to content

Commit aa452cd

Browse files
author
Adrian Catalan
committed
Maze solver
1 parent bb332bb commit aa452cd

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ Depth-first search (DFS) is an algorithm for traversing or searching tree or gra
2222

2323
Una Búsqueda en profundidad (en inglés DFS o Depth First Search) es un algoritmo que permite recorrer todos los nodos de un grafo o árbol (teoría de grafos) de manera ordenada, pero no uniforme. Su funcionamiento consiste en ir expandiendo todos y cada uno de los nodos que va localizando, de forma recurrente, en un camino concreto. Cuando ya no quedan más nodos que visitar en dicho camino, regresa (Backtracking), de modo que repite el mismo proceso con cada uno de los hermanos del nodo ya procesado.
2424

25+
****Maze Solver****
26+
27+
***[en_GB]***
28+
29+
There are a number of different maze solving algorithms, that is, automated methods for the solving of mazes. The random mouse, wall follower, Pledge, and Trémaux's algorithms are designed to be used inside the maze by a traveler with no prior knowledge of the maze, whereas the dead-end filling and shortest path algorithms are designed to be used by a person or computer program that can see the whole maze at once.
30+
31+
Mazes containing no loops are known as "simply connected", or "perfect" mazes, and are equivalent to a tree in graph theory. Thus many maze solving algorithms are closely related to graph theory. Intuitively, if one pulled and stretched out the paths in the maze in the proper way, the result could be made to resemble a tree.
32+
33+
***[es_ES]***
34+
35+
Hay una serie de algoritmos de resolución de laberintos. Con un previo conocimiento del laberinto, busca un trayecto más corto y están diseñados para ser utilizados por una persona o un programa informático que pueda ver todo el laberinto a la vez.
36+
Los laberintos que no contienen bucles se conocen como laberintos "simplemente conectados" o "perfectos", y son equivalentes a un árbol en la teoría de grafos. Así, muchos algoritmos de resolución de laberintos están estrechamente relacionados con la teoría de grafos.
37+
38+
2539
**03 - Shortest Path**
2640

2741
**04 - Artificial Intelligence**

src/main/java/mazeSolver/App.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package mazeSolver;
2+
3+
4+
/**
5+
* Created by Adrian on 10/06/2017.
6+
*/
7+
public class App {
8+
9+
/**
10+
* Maze solver Main
11+
* @param args
12+
*/
13+
public static void main (String[] args) {
14+
FileReader fileReader = new FileReader("C:\\Users\\Adrian\\IdeaProjects\\algorithm\\src\\main\\java\\mazeSolver\\map.txt",4, 4);
15+
fileReader.parseFile();
16+
MazeSolver mazeSolver = new MazeSolver(fileReader.getMap(),fileReader.getStartPositionRow(), fileReader.getStartPositionCol());
17+
mazeSolver.findWayOut();
18+
}
19+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package mazeSolver;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.util.Scanner;
6+
7+
/**
8+
* Created by Adrian on 10/06/2017.
9+
*/
10+
public class FileReader {
11+
private int[][] map;
12+
private String fileName;
13+
private int numOfRows;
14+
private int numOfColums;
15+
private int startPositionCol;
16+
private int startPositionRow;
17+
18+
public FileReader(String fileName, int numOfRows, int numOfColums) {
19+
this.fileName = fileName;
20+
this.numOfRows = numOfRows;
21+
this.numOfColums = numOfColums;
22+
this.map = new int[numOfRows][numOfColums];
23+
}
24+
25+
/**
26+
* Parse file with routes
27+
*/
28+
public void parseFile() {
29+
30+
Scanner scanner = null;
31+
try {
32+
scanner = new Scanner(new File(this.fileName));
33+
34+
for (int i = 0; i < this.numOfRows; i++) {
35+
for (int j = 0; j < this.numOfColums; j++) {
36+
map[i][j] = scanner.nextInt();
37+
if (map[i][j] == 2) {
38+
startPositionCol = j;
39+
startPositionRow = i;
40+
41+
}
42+
}
43+
}
44+
scanner.close();
45+
} catch (FileNotFoundException e) {
46+
e.printStackTrace();
47+
}
48+
}
49+
50+
public int[][] getMap() {
51+
return map;
52+
}
53+
54+
public int getStartPositionCol() {
55+
return startPositionCol;
56+
}
57+
58+
public int getStartPositionRow() {
59+
return startPositionRow;
60+
}
61+
}
62+
63+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package mazeSolver;
2+
3+
/**
4+
* Created by Adrian on 10/06/2017.
5+
*/
6+
public class MazeSolver {
7+
private int[][] mazeMap;
8+
private boolean[][] visited;
9+
private int startPositionCol;
10+
private int startPositionRow;
11+
12+
public MazeSolver(int[][] mazeMap, int startPositionCol, int startPositionRow) {
13+
this.mazeMap = mazeMap;
14+
this.visited = new boolean[mazeMap.length][mazeMap.length];
15+
this.startPositionCol = startPositionCol;
16+
this.startPositionRow = startPositionRow;
17+
}
18+
19+
20+
/**
21+
* Find way
22+
*/
23+
public void findWayOut() {
24+
try {
25+
dfs(startPositionRow, startPositionCol + 1);
26+
System.out.println("No solution found...");
27+
} catch (RuntimeException e){
28+
System.out.println("ROUTE FOUND TO EXIT!!!");
29+
}
30+
}
31+
32+
/**
33+
* Find correct route
34+
* @param i
35+
* @param j
36+
*/
37+
private void dfs(int i, int j) {
38+
System.out.println("Visiting i = "+i+" j= "+j);
39+
40+
if (this.mazeMap[i][j] == 3) {
41+
//EXIT FOUND
42+
throw new RuntimeException();
43+
}
44+
45+
int endOfMap = this.mazeMap.length - 1;
46+
47+
if(visited[i][j]) {
48+
// position visited
49+
return;
50+
} else if (i < 0 || i >= endOfMap) {
51+
//Out of map
52+
return;
53+
} else if (j < 0 || j >= endOfMap) {
54+
//Out of map
55+
return;
56+
} else if (this.mazeMap[i][j] == 1) {
57+
// Route not valid.
58+
return;
59+
} else {
60+
// Route valid.
61+
this.visited[i][j] = true;
62+
63+
dfs(i+1,j); // DOWN
64+
dfs(i,j+1); // RIGHT
65+
dfs(i,j-1); // LEFT
66+
dfs(i-1,j); // UP
67+
68+
}
69+
}
70+
}

src/main/java/mazeSolver/map.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1 1 1 1 1
2+
1 2 0 1 1
3+
1 0 0 1 1
4+
1 0 0 3 1
5+
1 1 1 1 1

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy