-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeMagicSquare.java
More file actions
128 lines (117 loc) · 3.17 KB
/
TreeMagicSquare.java
File metadata and controls
128 lines (117 loc) · 3.17 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
128
import java.util.ArrayList;
public class TreeMagicSquare {
private int squareSize;
private ArrayList<Integer> combination;
public static int cpt = 0;
//private ArrayList<TreeMagicSquare> sons;
public TreeMagicSquare(int n) {
this.combination = new ArrayList<Integer>();
//this.sons = new ArrayList<TreeMagicSquare>();
this.squareSize = n;
}
private void setCombination(ArrayList<Integer> c) {
this.combination = c;
}
public int getCpt() {
return cpt;
}
public void generate() {
for (int i = 1; i <= Math.pow(this.squareSize, 2); i++) {
if (!this.combination.contains(i)) {
ArrayList<Integer> sonCombination = new ArrayList<Integer>();
sonCombination.addAll(this.combination);
sonCombination.add(i);
if (sonCombination.size() <= 16) {
if (this.check(sonCombination)) {
TreeMagicSquare son = new TreeMagicSquare(this.squareSize);
son.setCombination(sonCombination);
//this.sons.add(son);
son.generate();
}
}
}
}
}
private int sumLine(ArrayList<Integer> line, int start, int end) {
int sum = 0;
for (int i = start; i <= end; i++) {
sum += line.get(i);
}
return sum;
}
private int sumColumn(ArrayList<Integer> column, int start, int end, int step) {
int sum = 0;
for (int i = start; i <= end; i = i + step) {
sum += column.get(i);
}
return sum;
}
private boolean check(ArrayList<Integer> combination) {
int index = combination.size() - 1;
switch(index) {
case 2:
return sumLine(combination, 0, 2) + 16 >= 34;
case 3:
return sumLine(combination, 0, 3) == 34;
case 6:
return sumLine(combination, 4, 6) + 16 >= 34;
case 7:
return sumLine(combination, 4, 7) == 34;
case 8:
return sumColumn(combination, 0, 8, 4) + 16 >= 34;
case 9:
if (sumColumn(combination, 1, 9, 4) + 16 >= 34) {
return combination.get(3) + combination.get(6) + combination.get(9) + 16 >= 34;
} else {
return false;
}
case 10:
if (sumLine(combination, 8, 10) + 16 >= 34) {
if (combination.get(0) + combination.get(5) + combination.get(10) + 16 >= 34) {
return sumColumn(combination, 2, 10, 4) + 16 >= 34;
} else {
return false;
}
} else {
return false;
}
case 11:
if (sumLine(combination, 8, 11) == 34) {
return sumColumn(combination, 3, 11, 4) + 16 >= 34;
} else {
return false;
}
case 12:
if (sumColumn(combination, 0, 12, 4) == 34) {
return combination.get(3) + combination.get(6) + combination.get(9) + combination.get(12) == 34;
} else {
return false;
}
case 13:
return sumColumn(combination, 1, 13, 4) == 34;
case 14:
if (sumLine(combination, 12, 14) + 16 >= 34) {
return sumColumn(combination, 2, 14, 4) == 34;
} else {
return false;
}
case 15:
if (sumLine(combination, 12, 15) == 34) {
if (sumColumn(combination, 3, 15, 4) == 34) {
if(combination.get(0) + combination.get(5) + combination.get(10) + combination.get(15) == 34) {
cpt++;
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
default:
return true;
}
}
}