Labirinti
This commit is contained in:
30
LabirintoTest/.gitignore
vendored
Normal file
30
LabirintoTest/.gitignore
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
### IntelliJ IDEA ###
|
||||
out/
|
||||
!**/src/main/**/out/
|
||||
!**/src/test/**/out/
|
||||
.kotlin
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
bin/
|
||||
!**/src/main/**/bin/
|
||||
!**/src/test/**/bin/
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
8
LabirintoTest/.idea/.gitignore
generated
vendored
Normal file
8
LabirintoTest/.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
6
LabirintoTest/.idea/misc.xml
generated
Normal file
6
LabirintoTest/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
LabirintoTest/.idea/modules.xml
generated
Normal file
8
LabirintoTest/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/LabirintoTest.iml" filepath="$PROJECT_DIR$/LabirintoTest.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
LabirintoTest/.idea/vcs.xml
generated
Normal file
6
LabirintoTest/.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
11
LabirintoTest/LabirintoTest.iml
Normal file
11
LabirintoTest/LabirintoTest.iml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
127
LabirintoTest/src/MazeSolver.java
Normal file
127
LabirintoTest/src/MazeSolver.java
Normal file
@@ -0,0 +1,127 @@
|
||||
import java.util.*;
|
||||
|
||||
public class MazeSolver {
|
||||
private static final int ROWS = 15;
|
||||
private static final int COLS = 25;
|
||||
private static final char WALL = '█';
|
||||
private static final char PATH = ' ';
|
||||
private static final char START = 'S';
|
||||
private static final char END = 'E';
|
||||
private static final char SOLUTION = '·';
|
||||
private char[][] maze;
|
||||
private boolean[][] visited;
|
||||
private int startRow, startCol, endRow, endCol;
|
||||
public MazeSolver() {
|
||||
maze = new char[ROWS][COLS];
|
||||
visited = new boolean[ROWS][COLS];
|
||||
generateMaze();
|
||||
}
|
||||
private void generateMaze() {
|
||||
for (int i = 0; i < ROWS; i++) {
|
||||
for (int j = 0; j < COLS; j++) {
|
||||
maze[i][j] = WALL;
|
||||
}
|
||||
}
|
||||
Random rand = new Random();
|
||||
Stack<int[]> stack = new Stack<>();
|
||||
startRow = 1;
|
||||
startCol = 1;
|
||||
maze[startRow][startCol] = PATH;
|
||||
stack.push(new int[]{startRow, startCol});
|
||||
int[][] directions = {{-2, 0}, {2, 0}, {0, -2}, {0, 2}};
|
||||
while (!stack.isEmpty()) {
|
||||
int[] current = stack.peek();
|
||||
int row = current[0];
|
||||
int col = current[1];
|
||||
List<int[]> neighbors = new ArrayList<>();
|
||||
for (int[] dir : directions) {
|
||||
int newRow = row + dir[0];
|
||||
int newCol = col + dir[1];
|
||||
|
||||
if (newRow > 0 && newRow < ROWS - 1 &&
|
||||
newCol > 0 && newCol < COLS - 1 &&
|
||||
maze[newRow][newCol] == WALL) {
|
||||
neighbors.add(new int[]{newRow, newCol, row + dir[0]/2, col + dir[1]/2});
|
||||
}
|
||||
}
|
||||
if (!neighbors.isEmpty()) {
|
||||
int[] next = neighbors.get(rand.nextInt(neighbors.size()));
|
||||
maze[next[2]][next[3]] = PATH; // Rompi il muro
|
||||
maze[next[0]][next[1]] = PATH;
|
||||
stack.push(new int[]{next[0], next[1]});
|
||||
} else {
|
||||
stack.pop();
|
||||
}
|
||||
}
|
||||
endRow = ROWS - 2;
|
||||
endCol = COLS - 2;
|
||||
maze[startRow][startCol] = START;
|
||||
maze[endRow][endCol] = END;
|
||||
}
|
||||
|
||||
private void printMaze() {
|
||||
System.out.println("\n╔" + "═".repeat(COLS) + "╗");
|
||||
for (int i = 0; i < ROWS; i++) {
|
||||
System.out.print("║");
|
||||
for (int j = 0; j < COLS; j++) {
|
||||
System.out.print(maze[i][j]);
|
||||
}
|
||||
System.out.println("║");
|
||||
}
|
||||
System.out.println("╚" + "═".repeat(COLS) + "╝");
|
||||
}
|
||||
|
||||
private boolean solveMaze(int row, int col, List<int[]> path) {
|
||||
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (maze[row][col] == WALL || visited[row][col]) {
|
||||
return false;
|
||||
}
|
||||
visited[row][col] = true;
|
||||
path.add(new int[]{row, col});
|
||||
if (row == endRow && col == endCol) {
|
||||
return true;
|
||||
}
|
||||
if (solveMaze(row - 1, col, path) ||
|
||||
solveMaze(row + 1, col, path) ||
|
||||
solveMaze(row, col - 1, path) ||
|
||||
solveMaze(row, col + 1, path)) {
|
||||
return true;
|
||||
}
|
||||
path.remove(path.size() - 1);
|
||||
return false;
|
||||
}
|
||||
private void markSolution(List<int[]> path) {
|
||||
for (int[] pos : path) {
|
||||
int row = pos[0];
|
||||
int col = pos[1];
|
||||
if (maze[row][col] != START && maze[row][col] != END) {
|
||||
maze[row][col] = SOLUTION;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void solve() {
|
||||
System.out.println("LABIRINTO ORIGINALE:");
|
||||
printMaze();
|
||||
List<int[]> path = new ArrayList<>();
|
||||
visited = new boolean[ROWS][COLS];
|
||||
System.out.println("\nRisoluzione in corso...");
|
||||
if (solveMaze(startRow, startCol, path)) {
|
||||
markSolution(path);
|
||||
System.out.println("\nLABIRINTO RISOLTO:");
|
||||
printMaze();
|
||||
System.out.println("\nPercorso trovato! Lunghezza: " + path.size() + " passi");
|
||||
} else {
|
||||
System.out.println("\nNessuna soluzione trovata!");
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== GENERATORE E RISOLUTORE DI LABIRINTI ===");
|
||||
System.out.println("Legenda: " + START + " = Inizio, " + END + " = Fine, " +
|
||||
SOLUTION + " = Soluzione");
|
||||
MazeSolver solver = new MazeSolver();
|
||||
solver.solve();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user