/
BAEKJOON 17143
124 lines (108 loc) · 2.47 KB
/
BAEKJOON 17143
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main {
static int R, C, M, ans;
static Shark[][] map;
static ArrayList<Shark> list = new ArrayList<Shark>();
static int[] dy = { -1, 1, 0, 0 };
static int[] dx = { 0, 0, 1,-1 };
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new Shark[R+1][C+1];
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int s = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
int z = Integer.parseInt(st.nextToken());
Shark shark = new Shark(r,c,s,d-1,z);
list.add(shark);
map[r][c] = shark;
}
for (int i = 1; i <= C; i++) {
fish(i);
move();
delete();
}
System.out.println(ans);
}
static void fish(int x) {
for (int y = 1; y <= R; y++) {
if( map[y][x] != null ) {
ans += map[y][x].z;
list.remove(map[y][x]);
break;
}
}
}
static void move() {
for (Shark shark : list) {
int r = shark.r;
int c = shark.c;
int s = shark.s;
int d = shark.d;
switch(d) {
case 0:
case 1:
s = s%(R*2 -2);
for (int i = 0; i < s; i++) {
if( r == 1 ) d = 1;
else if( r == R ) d = 0;
r += dy[d];
}
shark.d = d;
shark.r = r;
break;
case 2:
case 3:
s = s%(C*2 -2);
for (int i = 0; i < s; i++) {
if( c == 1 ) d = 2;
else if( c == C ) d = 3;
c += dx[d];
}
shark.d = d;
shark.c = c;
break;
}
}
}
static void delete() {
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
map[i][j] = null;
}
}
int size = list.size();
for (int i = size-1; i >= 0; i--) {
Shark s = list.get(i);
if( map[s.r][s.c] == null ) {
map[s.r][s.c] = s;
}else {
if( s.z > map[s.r][s.c].z ) {
list.remove(map[s.r][s.c]);
map[s.r][s.c] = s;
}else {
list.remove(s);
}
}
}
}
static class Shark{
int r, c, s, d, z;
public Shark(int r, int c, int s, int d, int z) {
this.r = r;
this.c = c;
this.s = s;
this.d = d;
this.z = z;
}
}
}