-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabirinto.java
More file actions
108 lines (93 loc) · 4.2 KB
/
Labirinto.java
File metadata and controls
108 lines (93 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package Etapa1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Labirinto {
// Constantes para representar os tipos de célula
private static final char PAREDE = 'X';
private static final char CAMINHO_ABERTO = ' ';
private static final char SAIDA = 'D';
private static final char CAMINHO_SOLUCAO = '#';
private char[][] labirinto; // Estrutura interna do labirinto
public void criaLabirinto(String filename) {
// Lista de possíveis caminhos onde o arquivo pode estar
String[] candidatos = new String[] {
filename, // Caminho padrão: src/Etapa1/labirinto.txt
new File("out", filename).getPath(), // Se rodar a partir de out/
new File("bin", filename).getPath(), // Se rodar a partir de bin/
new File("..", filename).getPath(), // Se rodar de dentro de out/, volta para raiz
new File("../..", filename).getPath() // Mais um nível acima, se necessário
};
File file = null;
for (String c : candidatos) {
File f = new File(c);
if (f.exists() && f.isFile()) {
file = f;
break;
}
}
if (file == null) { // Se nenhum arquivo foi encontrado, lança exceção
String cwd = new File(".").getAbsolutePath();
throw new IllegalArgumentException(
"Arquivo do labirinto não encontrado. Tentativas: "
+ String.join(" | ", candidatos)
+ " (cwd=" + cwd + ")"
);
}
List<char[]> linhas = new ArrayList<>(); // Lê as linhas do arquivo e monta a matriz
int larguraMax = 0;
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// Remove BOM, se existir
if (!line.isEmpty() && line.charAt(0) == '\ufeff') {
line = line.substring(1);
}
if (line.length() > larguraMax) larguraMax = line.length();
linhas.add(line.toCharArray());
}
labirinto = new char[linhas.size()][larguraMax]; // Cria a matriz preenchendo espaços onde não há caracteres
for (int i = 0; i < linhas.size(); i++) {
char[] src = linhas.get(i);
for (int j = 0; j < larguraMax; j++) {
labirinto[i][j] = (j < src.length) ? src[j] : CAMINHO_ABERTO;
}
}
} catch (IOException e) {
throw new IllegalArgumentException("Erro ao ler o arquivo do labirinto: " + file.getPath(), e);
}
}
public boolean percorreLabirinto() {
if (labirinto == null || labirinto.length == 0) return false;
return resolverLabirinto(0, 0);
}
public void imprimeLabirinto() {
if (labirinto == null) return;
for (int i = 0; i < labirinto.length; i++) {
int end = labirinto[i].length - 1;
while (end >= 0 && labirinto[i][end] == CAMINHO_ABERTO) end--;
System.out.println(new String(labirinto[i], 0, Math.max(0, end + 1)));
}
}
private boolean resolverLabirinto(int x, int y) {
if (!dentro(x, y)) return false;
char atual = labirinto[x][y];
if (atual == SAIDA) return true; // Saída encontrada
if (atual != CAMINHO_ABERTO) return false; // Não é caminho livre
labirinto[x][y] = CAMINHO_SOLUCAO; // Marca o caminho como parte da solução
// Tenta recursivamente todas as direções
if (resolverLabirinto(x + 1, y)) return true; // baixo
if (resolverLabirinto(x - 1, y)) return true; // cima
if (resolverLabirinto(x, y + 1)) return true; // direita
if (resolverLabirinto(x, y - 1)) return true; // esquerda
labirinto[x][y] = CAMINHO_ABERTO; // se não deu certo, desfaz a marcação
return false;
}
private boolean dentro(int x, int y) {
return x >= 0 && y >= 0 && labirinto != null &&
x < labirinto.length && y < labirinto[0].length;
}
}