Vidrios rotos en java
Java
Publicado el 8 de Junio del 2020 por Adan (10 códigos)
1.149 visualizaciones desde el 8 de Junio del 2020



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class vidriosRotos1 {
int resultado;
public static void main(String args [])throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
char mapa[][];
String res="";
String numeroEntrada=br.readLine();
StringTokenizer st = new StringTokenizer(numeroEntrada);
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
if ((n<1||n>=100)||(m<1||m>=100))
{
return;
}
mapa=new char[n][m];
//llenamos la mapa
for (int x=0;x<mapa.length;x++)
{
String fila=br.readLine();
for (int y=0;y<mapa[x].length;y++)
{
char c=fila.charAt(y);
mapa[x][y]=c;
}
}
mapa[6][9]=' ';
String lecturaVr=br.readLine();
int vr=Integer.parseInt(lecturaVr);
vidriosRotos1 v=new vidriosRotos1();
char copy[][]=copiarMatriz(mapa);
res=v.MayorZonaDejadez(copy)+" ";
if (vr<=0||vr>100)
{
return;
}
while (vr>0)
{
String lecturaXY=br.readLine();
StringTokenizer stXY = new StringTokenizer(lecturaXY);
int X = Integer.parseInt(stXY.nextToken());
int Y = Integer.parseInt(stXY.nextToken());
insetarVidrioRoto(mapa,X,Y);
// imprimirMapa(mapa);
char copiaMap[][]=copiarMatriz(mapa);
res=res+v.MayorZonaDejadez(copiaMap)+" ";
//System.out.println("mapa original");
// imprimirMapa(mapa);
vr--;
}
System.out.println(res);
}
public int MayorZonaDejadez(char m[][])
{
int numMayorZonaDejadez=resultado;
for (int i=0;i<m.length;i++)
{
for (int j=0;j<m[i].length;j++)
{
if (i>0&&j>0)
{
if (m[i][j]=='1'){
resuelve(m,i,j);
}
if (resultado>numMayorZonaDejadez)
{
numMayorZonaDejadez=resultado;
resultado=0;
}else
{
resultado=0;
}
}
}
}
return numMayorZonaDejadez;
}
public void resuelve(char [][]mapa,int x, int y){
if (paso(mapa,x, y)) {
mapa[x][y] = 'S';
}
}
private boolean paso(char mapa[][],int x, int y) {
if (mapa[x][y]=='X'){
return true;
}
if (mapa[x][y]=='0'||mapa[x][y]=='*') {
return false;
}
mapa[x][y]='*';
resultado++;
boolean result;
result=paso(mapa,x, y+1);
if (result)
{
return true;
}
result=paso(mapa,x-1,y+1);
if (result)
{
return true;
}
result=paso(mapa,x-1, y);
if (result)
{
return true;
}
result=paso(mapa,x-1,y-1);
if (result)
{
return true;
}
result=paso(mapa,x, y-1);
if (result)
{
return true;
}
result=paso(mapa,x+1,y-1);
if (result)
{
return true;
}
result=paso(mapa,x+1, y);
if (result)
{
return true;
}
result=paso(mapa,x+1,y+1);
if (result)
{
return true;
}
return false;
}
public static void insetarVidrioRoto(char mapa[][],int x,int y)
{
mapa[x-1][y-1]='1';
}
public static void imprimirMapa(char mapa[][])
{
for (int i=0;i<mapa.length;i++)
{
for (int j=0;j<mapa[i].length;j++)
{
System.out.print(mapa[i][j]);
}
System.out.println();
}
}
public static char[][] copiarMatriz(final char[][] array)
{
if (array != null)
{
final char[][] copiaMatrix = new char[array.length][];
for (int i = 0; i < array.length; i++)
{
final char[] row = array[i]; copiaMatrix[i] = new char[row.length];
System.arraycopy(row, 0, copiaMatrix[i], 0, row.length);
}
return copiaMatrix;
}
return null;
}
}
Comentarios sobre la versión: 1.0 (0)
No hay comentarios