Código de Java - Vidrios rotos en java

1.0

Publicado el 8 de Junio del 2020gráfica de visualizaciones de la versión: 1.0
950 visualizaciones desde el 8 de Junio del 2020
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
 

Comentar la versión: 1.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s6269