-
Notifications
You must be signed in to change notification settings - Fork 6
/
Hyekyoung.java
155 lines (143 loc) · 3.55 KB
/
Hyekyoung.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import java.util.StringTokenizer;
public class Hyekyoung {
static int[] dr = { -1, -1, 0, 1, 1, 1, 0, -1 }, dc = { 0, 1, 1, 1, 0, -1, -1, -1 };
static List<Fireball>[] fireballs;
static Queue<Fireball> queue = new ArrayDeque<>();
static Set<Integer> numSet = new HashSet<>();
static int N, M, K, nr, nc, num, m, s, d;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int r, c;
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
fireballs = new ArrayList[N * N + 1];
for (int i = 0; i <= N * N; i++) {
fireballs[i] = new ArrayList<>();
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
r = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
s = Integer.parseInt(st.nextToken());
d = Integer.parseInt(st.nextToken());
queue.add(new Fireball(r, c, m, s, d));
}
move(0);
}
static void move(int cnt) {
Fireball ball;
if (cnt == K) {
//k번 후에 질량 합 구하기
int mSum=0;
while(!queue.isEmpty()) {
ball = queue.poll();
mSum+=ball.m;
}
System.out.println(mSum);
return;
}
while (!queue.isEmpty()) {
ball = queue.poll();
m = ball.m;
s = ball.s;
d = ball.d;
nr = ball.r + dr[d] * (s % N);
nc = ball.c + dc[d] * (s % N);
//n번열과 n번행과 연결돼있는,,,머시기
if (nr > N) nr = (nr-1) % N + 1;
else if (nr <= 0) nr = N - Math.abs(nr);
if (nc > N) nc = (nc-1) % N + 1;
else if (nc <= 0) nc = N - Math.abs(nc);
num = N * (nr - 1) + nc;
// 자리 번호 구하고 set에 넣기(set에서 중복제거)
numSet.add(num);
fireballs[num].add(new Fireball(nr, nc, m, s, d));
}
mergeBalls();
move(cnt+1);
}
/*
4 4 2
1 2 13 4 3
1 4 12 3 7
4 1 2 5 7
4 2 6 3 0
ans:25
4 6 4
1 1 5 1 1
3 3 5 1 5
1 3 5 1 3
3 1 5 1 7
2 2 5 1 3
3 2 5 1 2
ans:4
4 4 2
1 2 13 4 3
1 4 12 3 7
4 1 2 5 7
4 2 6 3 0
ans:33
* */
static void mergeBalls() {
int n;
Iterator<Integer> it = numSet.iterator();
while (it.hasNext()) {
//set에 들어있는 번호 인덱스에 접근해서 몇갠지 보고 합치기
n = it.next();
Fireball ball = fireballs[n].get(0);
if (fireballs[n].size() > 1) {
boolean same = true;
int mSum = 0, sSum = 0;
int dir = fireballs[n].get(0).d % 2;
for (Fireball f : fireballs[n]) {
if (dir != f.d % 2) {
//다 짝순지 홀순지,,검사
same = false;
}
mSum+=f.m;
sSum+=f.s;
}
if(mSum/5>0) {
if(same) {
for(int i=0; i<4; i++) {
queue.add(new Fireball((n-1)/N+1, (n-1)%N+1, mSum/5, sSum/fireballs[n].size(), 2*i));
}
}
else {
for(int i=0; i<4; i++) {
queue.add(new Fireball((n-1)/N+1, (n-1)%N+1, mSum/5, sSum/fireballs[n].size(), 2*i+1));
}
}
}
}else {
queue.add(new Fireball((n-1)/N+1, (n-1)%N+1, ball.m, ball.s, ball.d));
}
fireballs[n].clear();
}
numSet.clear();
}
static class Fireball {
int r, c, m, s, d;
public Fireball(int r, int c, int m, int s, int d) {
super();
this.r = r;
this.c = c;
this.m = m;
this.s = s;
this.d = d;
}
}
}