-
Notifications
You must be signed in to change notification settings - Fork 6
/
sandpileAddition.txt
103 lines (94 loc) · 3.2 KB
/
sandpileAddition.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
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
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 n = in.nextInt();
if (in.hasNextLine()) {
in.nextLine();
}
int[][] row1 = new int[n][n];
int[][] row2 = new int[n][n];
int[][] rowAdd = new int[n][n];
int[][] rowResult = new int[n][n];
System.err.println("Row1");
for (int i = 0; i < n; i++) {
String row = in.nextLine();
//System.err.println("row1 "+ row);
for (int j = 0; j < n; j++) {
row1[i][j] = Character.getNumericValue(row.charAt(j));
System.err.print(row1[i][j]);
}
System.err.println("");
}
System.err.println("Row2");
for (int i = 0; i < n; i++) {
String row = in.nextLine();
//System.err.println("row1 "+ row);
for (int j = 0; j < n; j++) {
row2[i][j] = Character.getNumericValue(row.charAt(j));
System.err.print(row2[i][j]);
}
System.err.println("");
}
//System.err.println("somme");
rowAdd = somme(row1,row2,n);
rowResult = distributes(rowAdd,n);
System.err.println("rowResult");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(rowResult[i][j]);
}
System.out.println("");
}
}
public static int[][] somme(int[][] row1, int[][] row2, int n){
int[][] rowAdd = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int somme = row2[i][j] +row1[i][j] ;
//System.err.println("somme "+ somme);
rowAdd[i][j] = somme;
//System.err.print(rowAdd[i][j]);
}
//System.err.println("");
}
return rowAdd;
}
public static int[][] distributes(int[][] rowAdd, int n){
boolean four = false;
do{
four = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int valeur = rowAdd[i][j];
// System.err.println("i "+i+" j "+j+" v "+valeur);
if(valeur > 3){
four = true;
rowAdd[i][j] = valeur-4;
if(i-1 >=0){
rowAdd[i-1][j] += 1;
}
if(i+1<n){
rowAdd[i+1][j] += 1;
}
if(j-1 >=0){
rowAdd[i][j-1] += 1;
}
if(j+1 <n){
rowAdd[i][j+1] += 1;
}
}else{
rowAdd[i][j] = valeur;
}
}
}
}while(four);
return rowAdd;
}
}