forked from RiviereGregory/CodinGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeJeuDeLaVie.txt
71 lines (67 loc) · 2.66 KB
/
LeJeuDeLaVie.txt
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
import java.util.*;
import java.io.*;
import java.math.*;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int width = in.nextInt();
int height = in.nextInt();
int[][] tableau = new int[height][width];
int[][] tableauResultat = new int[height][width];
for (int i = 0; i < height; i++) {
String line = in.next();
//System.err.println(line);
for(int j=0; j<width; j++){
tableau[i][j] = Character.getNumericValue(line.charAt(j));
}
}
for (int i = 0; i < height; i++) {
for(int j=0; j<width; j++){
int celluleVivanteAutour = 0;
//System.err.println("debut " +celluleVivanteAutour);
if(j-1>=0){
celluleVivanteAutour += tableau[i][j-1];
}
if(j+1<width){
celluleVivanteAutour += tableau[i][j+1];
}
if(i-1>=0){
celluleVivanteAutour += tableau[i-1][j];
}
if(i+1<height){
celluleVivanteAutour += tableau[i+1][j];
}
if(i-1>=0 && j-1>=0){
celluleVivanteAutour += tableau[i-1][j-1];
}
if(i-1>=0 && j+1<width){
celluleVivanteAutour += tableau[i-1][j+1];
}
if(i+1<height && j-1>=0){
celluleVivanteAutour += tableau[i+1][j-1];
}
if(i+1<height && j+1<width){
celluleVivanteAutour += tableau[i+1][j+1];
}
//System.err.println(celluleVivanteAutour);
if(tableau[i][j] == 1 &&(celluleVivanteAutour>=2 && celluleVivanteAutour<=3)){
// System.err.println("vivante " +tableauResultat[i][j]);
tableauResultat[i][j] = 1;
}else if(tableau[i][j] == 0 && celluleVivanteAutour==3){
// System.err.println("résurection " +tableauResultat[i][j]);
tableauResultat[i][j] = 1;
}else{
// System.err.println("mort " +tableauResultat[i][j]);
tableauResultat[i][j] = 0;
}
//System.err.println("Fin " +tableauResultat[i][j]);
System.out.print(tableauResultat[i][j]);
}
if(i < height-1) System.out.println("");
}
}
}