Sunday, March 06, 2011

110102 Buscaminas

/**
* Buscaminas
*/

/**
* @author BreakDark
* @version 1.0 beta
* @link BreakDark666@yahoo.es
*/

import java.util.*;

public class Main
{
public static void main(String[] args)
{
Scanner Lee;
short n, m, x = 0;
char[][] sal = new char[100][100];
String linea;
Lee = new Scanner(System.in);
n = Lee.nextShort();
m = Lee.nextShort();
while (n != 0 || m != 0)
{
x++;
// aqui se lee la n filas
for (short i = 0; i < n; i++)
{
if (m != 0)
{
linea = Lee.next();
for (short j = 0; j < m; j++)
// Se introduce los caracteres en una
// matriz
sal[i][j] = linea.charAt(j);
}
}
short[][] dat = modMat(sal, n, m);
// se muestra el resultado
System.out.println("Field #" + x + ":");
for (short i = 0; i < n; i++)
{
for (short j = 0; j < m; j++)
{
if (dat[i][j] != -1)
System.out.print(dat[i][j]);
else
System.out.print('*');
}
if (m != 0)
System.out.println();
}
n = Lee.nextShort();
m = Lee.nextShort();
if (n != 0 || m != 0)
System.out.println();
}
}

// Método para devolver una matriz de enteros con valores
public static short[][] modMat(char[][] dat, int fil, int col)
{
short[][] res = new short[100][100];
for (short i = 0; i < fil; i++)
{
for (short j = 0; j < col; j++)
{
if (dat[i][j] != '*')
{
int c = 0; // contador de minas adiacentes
// fila superior
if (i - 1 >= 0)
{
if (j - 1 >= 0)
c = (dat[i - 1][j - 1] == '*')? c + 1 : c;
c = (dat[i - 1][j] == '*')? c + 1 : c;
if (j + 1 < col)
c = (dat[i - 1][j + 1] == '*')? c + 1 : c;
}
// fila central
if (j - 1 >= 0)
c = (dat[i][j - 1] == '*')? c + 1 : c;
if (j + 1 < col)
c = (dat[i][j + 1] == '*')? c + 1 : c;
// fila inferior
if (i + 1 < fil)
{
if (j - 1 >= 0)
c = (dat[i + 1][j - 1] == '*')? c + 1 : c;
c = (dat[i + 1][j] == '*')? c + 1 : c;
if (j + 1 < col)
c = (dat[i + 1][j + 1] == '*')? c + 1 : c;
}
res[i][j] = (short)c;// aux.charAt(0);
}
else
res[i][j] = -1;
}
}
return res;
}
}

No comments: