-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q34.java
77 lines (66 loc) · 2.13 KB
/
Q34.java
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
import java.util.HashSet;
import java.util.Set;
public class Q34 {
static final int SIZE = 9;
public static void main (String[] args) {
final int MAX = SIZE * SIZE;
int ans = 0;
// 飛車の位置ループ
for (int pos_hi = 0; pos_hi < MAX; pos_hi++) {
int pos_x_hi = pos_hi / 9;
int pos_y_hi = pos_hi % 9;
// 角の位置ループ
for (int pos_ka = 0; pos_ka < MAX; pos_ka++) {
int pos_x_ka = pos_ka / 9;
int pos_y_ka = pos_ka % 9;
// 駒は1マスに1駒
if (pos_x_hi == pos_x_ka && pos_y_hi == pos_y_ka) continue;
// 効きの計算
ans += calc(pos_x_hi, pos_y_hi, pos_x_ka, pos_y_ka);
}
}
System.out.println(ans);
}
// 飛車の効き + 角の効き
static int calc (int pos_x_hi, int pos_y_hi, int pos_x_ka, int pos_y_ka) {
Set<Integer> kiki_hi = new HashSet<>();
int hi = calc_hi(pos_x_hi, pos_y_hi, pos_x_ka, pos_y_ka, kiki_hi);
int ka = calc_ka(pos_x_hi, pos_y_hi, pos_x_ka, pos_y_ka, kiki_hi);
//System.out.println(hi + ", " + ka);
return hi + ka;
}
// 飛車の効き
static int calc_hi (int pos_x_hi, int pos_y_hi, int pos_x_ka, int pos_y_ka, Set<Integer> kiki_hi) {
int ret = 0;
int[] dx = { 0, 0, -1, 1};
int[] dy = {-1, 1, 0, 0};
for (int di = 0; di < 4; di++) {
int pos_x = pos_x_hi + dx[di];
int pos_y = pos_y_hi + dy[di];
while (0 <= pos_x && pos_x < SIZE && 0 <= pos_y && pos_y < SIZE &&
(pos_x != pos_x_ka || pos_y != pos_y_ka)) {
kiki_hi.add(pos_x * 10 + pos_y);
ret++;
pos_x += dx[di];
pos_y += dy[di];
}
}
return ret;
}
// 角の効き
static int calc_ka (int pos_x_hi, int pos_y_hi, int pos_x_ka, int pos_y_ka, Set<Integer> kiki_hi) {
int ret = 0;
int[] dx = { 1, 1, -1, -1};
int[] dy = {-1, 1, 1, -1};
for (int di = 0; di < 4; di++) {
int pos_x = pos_x_ka + dx[di];
int pos_y = pos_y_ka + dy[di];
while (0 <= pos_x && pos_x < SIZE && 0 <= pos_y && pos_y < SIZE && (pos_x != pos_x_hi || pos_y != pos_y_hi)) {
if (!kiki_hi.contains(pos_x * 10 + pos_y)) ret++;
pos_x += dx[di];
pos_y += dy[di];
}
}
return ret;
}
}