forked from JosefNafaa/CodingGameJavaExos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
127 lines (108 loc) · 2.5 KB
/
Copy pathSolution.java
File metadata and controls
127 lines (108 loc) · 2.5 KB
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
package sandPile;
import java.util.ArrayList;
import java.util.Arrays;
public class Solution {
public static Integer[][] sandpile(Integer[][] pile, int n) {
int size = pile[0].length;
int middle = middleIndex(size);
for (int i = 0; i < n; i++) {
if (pile[middle][middle] != 4)
pile[middle][middle] += 1;
else {
pile[middle][middle] = 0;
addOne(middle, middle, pile, size);
while (is_Four_Present(pile)) {
remove_four(pile, size);
}
}
}
while (is_Four_Present(pile)) {
remove_four(pile, size);
}
return pile;
}
/**
*
* @param pile
* @param size
* @return pile without four in grid
*/
public static Integer[][] remove_four(Integer[][] pile, int size) {
for (int k = 0; k < size; k++) {
for (int l = 0; l < size; l++) {
if (pile[k][l] == 4) {
pile[k][l] = 0;
addOne(k, l, pile, size);
}
}
}
return pile;
}
/**
* check if 4 is present in the grid
*
* @param pile
* @return boolean
*/
public static boolean is_Four_Present(Integer[][] pile) {
ArrayList<Integer> list = new ArrayList<>();
for (Integer[] array : pile) {
list.addAll(Arrays.asList(array));
}
return list.contains(4);
}
/**
*
* @param i
* @param j
* @param pile
* @return pile with value +1 to neighbors
*/
public static Integer[][] addOne(int i, int j, Integer[][] pile, int size) {
if (i - 1 >= 0) {
pile[i - 1][j] += 1;
}
if (j - 1 >= 0) {
pile[i][j - 1] += 1;
}
if (i + 1 < size) {
pile[i + 1][j] += 1;
}
if (j + 1 < size) {
pile[i][j + 1] += 1;
}
return pile;
}
/**
*
* @param taille
* @return the index of the middle in the grid
*/
public static int middleIndex(int taille) {
return (taille - 1) / 2;
}
public static void main(String[] args) {
Integer[][] pile = new Integer[][] { { 0, 1, 0 }, { 0, 2, 3 }, { 1, 0, 1 } };
System.out.println("before");
System.out.print(pile[0][0]);
System.out.print(pile[0][1]);
System.out.println(pile[0][2]);
System.out.print(pile[1][0]);
System.out.print(pile[1][1]);
System.out.println(pile[1][2]);
System.out.print(pile[2][0]);
System.out.print(pile[2][1]);
System.out.println(pile[2][2]);
sandpile(pile, 2);
System.out.println("after");
System.out.print(pile[0][0]);
System.out.print(pile[0][1]);
System.out.println(pile[0][2]);
System.out.print(pile[1][0]);
System.out.print(pile[1][1]);
System.out.println(pile[1][2]);
System.out.print(pile[2][0]);
System.out.print(pile[2][1]);
System.out.print(pile[2][2]);
}
}