|
| 1 | +package sandPile; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | + |
| 6 | +public class Solution { |
| 7 | + |
| 8 | + public static Integer[][] sandpile(Integer[][] pile, int n) { |
| 9 | + int size = pile[0].length; |
| 10 | + int middle = middleIndex(size); |
| 11 | + for (int i = 0; i < n; i++) { |
| 12 | + if (pile[middle][middle] != 4) |
| 13 | + pile[middle][middle] += 1; |
| 14 | + else { |
| 15 | + pile[middle][middle] = 0; |
| 16 | + addOne(middle, middle, pile, size); |
| 17 | + |
| 18 | + while (is_Four_Present(pile)) { |
| 19 | + remove_four(pile, size); |
| 20 | + |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + while (is_Four_Present(pile)) { |
| 25 | + remove_four(pile, size); |
| 26 | + |
| 27 | + } |
| 28 | + return pile; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * |
| 33 | + * @param pile |
| 34 | + * @param size |
| 35 | + * @return pile without four in grid |
| 36 | + */ |
| 37 | + public static Integer[][] remove_four(Integer[][] pile, int size) { |
| 38 | + for (int k = 0; k < size; k++) { |
| 39 | + |
| 40 | + for (int l = 0; l < size; l++) { |
| 41 | + if (pile[k][l] == 4) { |
| 42 | + pile[k][l] = 0; |
| 43 | + addOne(k, l, pile, size); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return pile; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * check if 4 is present in the grid |
| 53 | + * |
| 54 | + * @param pile |
| 55 | + * @return boolean |
| 56 | + */ |
| 57 | + public static boolean is_Four_Present(Integer[][] pile) { |
| 58 | + |
| 59 | + ArrayList<Integer> list = new ArrayList<>(); |
| 60 | + for (Integer[] array : pile) { |
| 61 | + list.addAll(Arrays.asList(array)); |
| 62 | + } |
| 63 | + |
| 64 | + return list.contains(4); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * |
| 70 | + * @param i |
| 71 | + * @param j |
| 72 | + * @param pile |
| 73 | + * @return pile with value +1 to neighbors |
| 74 | + */ |
| 75 | + public static Integer[][] addOne(int i, int j, Integer[][] pile, int size) { |
| 76 | + if (i - 1 >= 0) { |
| 77 | + pile[i - 1][j] += 1; |
| 78 | + } |
| 79 | + if (j - 1 >= 0) { |
| 80 | + pile[i][j - 1] += 1; |
| 81 | + } |
| 82 | + if (i + 1 < size) { |
| 83 | + pile[i + 1][j] += 1; |
| 84 | + } |
| 85 | + if (j + 1 < size) { |
| 86 | + pile[i][j + 1] += 1; |
| 87 | + } |
| 88 | + return pile; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * |
| 93 | + * @param taille |
| 94 | + * @return the index of the middle in the grid |
| 95 | + */ |
| 96 | + public static int middleIndex(int taille) { |
| 97 | + return (taille - 1) / 2; |
| 98 | + } |
| 99 | + |
| 100 | + public static void main(String[] args) { |
| 101 | + |
| 102 | + Integer[][] pile = new Integer[][] { { 0, 1, 0 }, { 0, 2, 3 }, { 1, 0, 1 } }; |
| 103 | + |
| 104 | + System.out.println("before"); |
| 105 | + System.out.print(pile[0][0]); |
| 106 | + System.out.print(pile[0][1]); |
| 107 | + System.out.println(pile[0][2]); |
| 108 | + System.out.print(pile[1][0]); |
| 109 | + System.out.print(pile[1][1]); |
| 110 | + System.out.println(pile[1][2]); |
| 111 | + System.out.print(pile[2][0]); |
| 112 | + System.out.print(pile[2][1]); |
| 113 | + System.out.println(pile[2][2]); |
| 114 | + |
| 115 | + sandpile(pile, 2); |
| 116 | + System.out.println("after"); |
| 117 | + System.out.print(pile[0][0]); |
| 118 | + System.out.print(pile[0][1]); |
| 119 | + System.out.println(pile[0][2]); |
| 120 | + System.out.print(pile[1][0]); |
| 121 | + System.out.print(pile[1][1]); |
| 122 | + System.out.println(pile[1][2]); |
| 123 | + System.out.print(pile[2][0]); |
| 124 | + System.out.print(pile[2][1]); |
| 125 | + System.out.print(pile[2][2]); |
| 126 | + } |
| 127 | +} |
0 commit comments