forked from sherxon/AlgoDS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralMemory_3.java
More file actions
116 lines (112 loc) · 3.68 KB
/
Copy pathSpiralMemory_3.java
File metadata and controls
116 lines (112 loc) · 3.68 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
package adventofcode;
/**
* Why Did you create this class? what does it do?
*/
public class SpiralMemory_3 {
public static void main(String[] args) {
// System.out.println(solve(277678));
// System.out.println(solve(1024));
// System.out.println(solve(1));
// System.out.println(solve(12));
// System.out.println(solve(23));
// System.out.println(solve(26));
System.out.println(solve2(277678));
}
private static int solve2(int n) {
int d = 2;
int[][] a = new int[101][101];
int i = 50;
int j = 50;
int k = -1;
int c = 2;
a[i][j] = 1;
a[i][j + 1] = 1;
a[i - 1][j + 1] = 2;
j++;
i--;
while (d <= n) {
if (k == 1) { // go right
for (int l = 0; l < c; l++) {
a[i][++j] = a[i][j + 1] + a[i][j - 1] + a[i + 1][j] + a[i - 1][j] +
a[i + 1][j + 1] + a[i - 1][j - 1] + a[i + 1][j - 1] + a[i - 1][j + 1];
d = Math.max(d, a[i][j]);
if (d > n)
break;
}
k = -2;
} else if (k == -2) { // go up
for (int l = 0; l < c; l++) {
a[--i][j] = a[i][j + 1] + a[i][j - 1] + a[i + 1][j] + a[i - 1][j] +
a[i + 1][j + 1] + a[i - 1][j - 1] + a[i + 1][j - 1] + a[i - 1][j + 1];
d = Math.max(d, a[i][j]);
if (d > n)
break;
}
c++;
k = -1;
} else if (k == -1) { // go left
for (int l = 0; l < c; l++) {
a[i][--j] = a[i][j + 1] + a[i][j - 1] + a[i + 1][j] + a[i - 1][j] +
a[i + 1][j + 1] + a[i - 1][j - 1] + a[i + 1][j - 1] + a[i - 1][j + 1];
d = Math.max(d, a[i][j]);
if (d > n)
break;
}
k = 2;
} else { // go down
for (int l = 0; l < c; l++) {
a[++i][j] = a[i][j + 1] + a[i][j - 1] + a[i + 1][j] + a[i - 1][j] +
a[i + 1][j + 1] + a[i - 1][j - 1] + a[i + 1][j - 1] + a[i - 1][j + 1];
d = Math.max(d, a[i][j]);
if (d > n)
break;
}
k = 1;
c++;
}
printa(a, c + 4);
System.out.println();
System.out.println("#####################");
System.out.println();
}
return d;
}
private static void printa(int[][] a, int x) {
for (int i = 50 - x; i < 50 + x; i++) {
for (int j = 50 - x; j < 50 + x; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
private static int solve(int n) {
if (n == 1)
return 0;
int k = 0;
int d = 1;
int i = 1;
while (d < n) {
k += 8;
d += k;
i += 2;
}
int ii, jj;
int lbc = d - i + 1;
int luc = lbc - i + 1;
int ruc = luc - i + 1;
if (n <= d && n >= lbc) {
ii = i - 1;
jj = n - lbc;
} else if (n <= lbc && n >= luc) {
ii = n - luc;
jj = 0;
} else if (n <= luc && n >= ruc) {
ii = 0;
jj = luc - n;
} else {
ii = ruc - n;
jj = i - 1;
}
return Math.abs(ii - i / 2) + Math.abs(jj - i / 2);
}
}