-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution2382_미생물격리.java
124 lines (105 loc) · 3.03 KB
/
Solution2382_미생물격리.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
//2019-08-08
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
public class Solution2382_미생물격리 {
private static int N, M, K;
private static int[][] map;
private static List<microbe> list;
private static int[] dy = { 0, -1, 1, 0, 0 };
private static int[] dx = { 0, 0, 0, -1, 1 };
// 1:상 / 2:하 / 3:왼 / 4:오
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
StringTokenizer st;
for (int tc = 1; tc <= T; tc++) {
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken()); // 맵 크기
M = Integer.parseInt(st.nextToken()); // M시간 후 남아있는 미생물의 수 출력
K = Integer.parseInt(st.nextToken()); // 미생물 군집 개수
map = new int[N][N];
list = new ArrayList<>();
for (int i = 0; i < K; i++) {
st = new StringTokenizer(br.readLine());
microbe m = new microbe(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
list.add(m);
}
Collections.sort(list);
for (int t = 0; t < M; t++) {
// 이동
for (microbe m : list) {
m.y += dy[m.dir];
m.x += dx[m.dir];
if (m.y == 0 || m.x == 0 || m.y == N - 1 || m.x == N - 1) {
m.cnt /= 2;
if (m.dir == 1)
m.dir = 2;
else if (m.dir == 2)
m.dir = 1;
else if (m.dir == 3)
m.dir = 4;
else if (m.dir == 4)
m.dir = 3;
}
}
Collections.sort(list);
//같은 위치 검사
microbe before = null;
List<microbe> beDeleted = new ArrayList<>();
for (microbe now : list) {
if (before == null) {
before = now;
continue;
}
if (now.y == before.y && now.x == before.x) { // 같은 위치에 있으면
before.cnt += now.cnt;
beDeleted.add(now);
//System.out.println(now.toString());
} else { //위치가 바뀌면
before = now;
}
}
for(microbe deleted : beDeleted) {
//System.out.println("delete // "+deleted.toString());
list.remove(deleted);
}
}
int ans = 0;
for(microbe m : list) {
ans += m.cnt;
}
System.out.println("#" + tc + " "+ans);
} // end of tc
}// end of main
static class microbe implements Comparable<microbe> {
int y;
int x;
int dir;
int cnt;
public microbe(int y, int x, int cnt, int dir) {
this.y = y;
this.x = x;
this.dir = dir;
this.cnt = cnt;
}
@Override
public int compareTo(microbe o) {
if (o.y != this.y) {
return this.y - o.y;
} else if (o.y == this.y && this.x != o.x) {
return this.x - o.x;
}
// 같은 위치라면 미생물 개수 내림차순
return o.cnt - this.cnt;
}
@Override
public String toString() {
return "microbe [y=" + y + ", x=" + x + ", dir=" + dir + ", cnt=" + cnt + "]";
}
}
}