-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicSquare.java
More file actions
100 lines (95 loc) · 2.14 KB
/
MagicSquare.java
File metadata and controls
100 lines (95 loc) · 2.14 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
package algorithm.hacker
public class MagicSquare {
public static int[][] test() {
int size = 3;
int maxIdx = size - 1;
int[][] magic = new int[size][size];
int number = 1;
int rowIdx = 0;
int colIdx = 1;
magic[rowIdx][colIdx] = number;
int maxCnt = size*size;
while (number < maxCnt) {
number++;
rowIdx--;
colIdx++;
if (rowIdx < 0) {
rowIdx = maxIdx;
}
if (colIdx > maxIdx) {
colIdx = 0;
}
if (magic[rowIdx][colIdx] == 0) {
magic[rowIdx][colIdx] = number;
} else {
rowIdx++;
colIdx--;
if (rowIdx > maxIdx) {
rowIdx = 0;
}
if (colIdx < 0) {
colIdx = maxIdx;
}
rowIdx++;
magic[rowIdx][colIdx] = number;
}
}
for(int[] row : magic) {
// System.out.println(String.format("%d %d %d", row[0], row[1], row[2]));
}
return magic;
}
public static int[][][] test1(int[][] magic) {
int size = 3;
int maxPossible = ((size*((size^2)+1))/2)+1;
int[][] baseMagic = magic;
int[][][] possible = new int[maxPossible*2][size][size];
possible[0] = magic;
for(int i=1; i<maxPossible; i++) {
for(int j=0; j<size; j++) {
int rowIdx = size-1;
for(int k=0; k<size; k++) {
possible[i][j][k] = baseMagic[rowIdx][j];
rowIdx--;
}
}
baseMagic = possible[i];
}
baseMagic = test2(magic);
possible[maxPossible] = baseMagic;
for(int i=maxPossible+1; i<maxPossible*2; i++) {
for(int j=0; j<size; j++) {
int rowIdx = size-1;
for(int k=0; k<size; k++) {
possible[i][j][k] = baseMagic[rowIdx][j];
rowIdx--;
}
}
baseMagic = possible[i];
}
return possible;
}
public static int[][] test2(int[][] magic) {
int size = 3;
int[][] opp = new int[size][size];
for(int i=0; i<size; i++) {
int idx = 2;
for(int j=0; j<size; j++) {
opp[i][j] = magic[i][idx];
idx--;
}
}
return opp;
}
public static void main(String[] args) {
int[][] magic = test();
int[][][] possible = test1(magic);
for(int i=0; i<possible.length; i++) {
for(int j=0; j<3; j++) {
int[] pos = possible[i][j];
System.out.println(String.format("%d %d %d", pos[0], pos[1], pos[2]));
}
System.out.println();
}
}
}