-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLaberinto.java
More file actions
86 lines (84 loc) · 2.52 KB
/
Laberinto.java
File metadata and controls
86 lines (84 loc) · 2.52 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
import java.util.Scanner;
public class Laberinto {
static int nMatriz;
static int contador;
static char[][] mat;
static char[][] mat2;
static Scanner sn = new Scanner(System.in);
public static void main(String args[]) {
contador = 0;
nMatriz = sn.nextInt();
if (nMatriz >= 3 && nMatriz <= 33) {
mat = new char[nMatriz + 2][nMatriz + 2];
} else {
System.out.println("0");
}
for (int i = 0; i < nMatriz + 2; i++) {
if (i == 0 || i == nMatriz + 1) {
for (int j = 0; j < nMatriz + 2; j++) {
mat[i][j] = '#';
}
} else {
String linea = sn.next();
char[] fila = linea.toCharArray();
int count = 0;
for (int j = 0; j < nMatriz + 2; j++) {
if (j == 0 || j == nMatriz + 1) {
mat[i][j] = '#';
} else {
mat[i][j] = fila[count];
count++;
}
}
}
}
mat2=copiarMat(mat);
int result=(recorrer(1,1)-4)*9;
boolean conectado = false;
if(mat[nMatriz][nMatriz]=='v'){
conectado=true;
}
contador=0;
mat=copiarMat(mat2);
int result2 = (recorrer(nMatriz, nMatriz) - 4) * 9;
if(conectado){
System.out.println(result);
}
else{
System.out.println(result+result2);
}
}
private static int recorrer(int x, int y) {
mat[x][y] = 'v';
if (mat[x - 1][y] == '.') {
recorrer(x - 1, y);
} else if (mat[x - 1][y] == '#') {
contador++;
}
if (mat[x + 1][y] == '.') {
recorrer(x + 1, y);
} else if (mat[x + 1][y] == '#') {
contador++;
}
if (mat[x][y - 1] == '.') {
recorrer(x, y - 1);
} else if (mat[x][y - 1] == '#') {
contador++;
}
if (mat[x][y + 1] == '.') {
recorrer(x, y + 1);
} else if (mat[x][y + 1] == '#') {
contador++;
}
return contador;
}
private static char[][] copiarMat(char[][] origen){
char[][] resultado= new char[nMatriz+2][nMatriz+2];
for (int i = 0; i < nMatriz + 2; i++) {
for (int j = 0; j < nMatriz + 2; j++) {
resultado[i][j]=origen[i][j];
}
}
return resultado;
}
}