-
Notifications
You must be signed in to change notification settings - Fork 0
/
A176.java
49 lines (41 loc) · 1.58 KB
/
A176.java
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
import java.util.Scanner;
public class Buscaminas {
//metodo que cuenta las minas que hay alrededor de una posicion dada.
public static int contarMinasAlrededor(char[][] tablero, int fila, int columna) {
int recuento = 0;
for (int i=Math.max(0, fila-1); i < Math.min(tablero.length, fila+2); i++) {
for (int j=Math.max(0, columna-1); j < Math.min(tablero[i].length, columna+2); j++) {
if (tablero[i][j] == '*') {
recuento++;
}
}
}
return recuento;
}
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int columnas = sc.nextInt();
int filas = sc.nextInt();
while (columnas !=0 && filas !=0) {
char[][] tablero = new char[filas][columnas];
int minas = 0;
//Se carga el array tablero con la entrada.
for (int i=0; i < filas; i++) {
String lineaEntrada = sc.next();
for (int j=0; j < columnas; j++) {
tablero[i][j] = lineaEntrada.charAt(j);
}
}
for (int i=0; i < filas; i++) {
for (int j=0; j < columnas; j++) {
if (tablero[i][j] == '-' && contarMinasAlrededor(tablero,i,j) > 5) {
minas++;
}
}
}
System.out.println(minas);
columnas = sc.nextInt();
filas = sc.nextInt();
}
}
}