티스토리 뷰

알고리즘 문제/BOJ

[백준] 17143. 낚시왕

하뉘하뉘~ 2020. 10. 12. 21:20

www.acmicpc.net/problem/17143

 

17143번: 낚시왕

낚시왕이 상어 낚시를 하는 곳은 크기가 R×C인 격자판으로 나타낼 수 있다. 격자판의 각 칸은 (r, c)로 나타낼 수 있다. r은 행, c는 열이고, (R, C)는 아래 그림에서 가장 오른쪽 아래에 있는 칸이다.

www.acmicpc.net

 

 

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
156
157
158
159
160
161
162
163
164
165
import java.util.*;
import java.io.*;
 
public class Main {
 
    static int R, C, M;
 
    public static void main(String[] args) throws IOException {
 
        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());
 
        Shark[][] sharks = new Shark[R + 1][C + 1];
 
        for (int i = 0; i < sharks.length; i++)
            Arrays.fill(sharks[i], null);
 
        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine());
 
            int r = Integer.parseInt(st.nextToken());
            int c = Integer.parseInt(st.nextToken());
 
            sharks[r][c] = new Shark(r, c, Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()),
                    Integer.parseInt(st.nextToken()));
        }
 
        int answer = 0;
 
        PriorityQueue<Shark> pq = new PriorityQueue<>();
 
        for (int c = 1; c <= C; c++) {
            // c 열 가장 위에 있는 상어 catch
            for (int r = 1; r <= R; r++) {
                if (sharks[r][c] != null) {
                    answer += sharks[r][c].size;
                    sharks[r][c] = null;
                    break;
                }
            }
 
            // 상어 이동
            for (int i = 1; i <= R; i++) {
                for (int j = 1; j <= C; j++) {
                    if (sharks[i][j] != null) {
                        sharks[i][j].move();
                        pq.add(sharks[i][j]);
                        sharks[i][j] = null;
                    }
                }
            }
 
            while (!pq.isEmpty()) {
                Shark tmp = pq.poll();
 
                if (sharks[tmp.r][tmp.c] == null)
                    sharks[tmp.r][tmp.c] = tmp;
            }
        }
 
        System.out.println(answer);
 
        br.close();
 
    }
 
    public static class Shark implements Comparable<Shark> {
        int r, c;
        int speed;
        int dir;
        int size;
 
        public Shark(int s, int d, int z) {
            this.speed = s;
            this.dir = d;
            this.size = z;
        }
 
        public Shark(int r, int c, int s, int d, int z) {
            this.r = r;
            this.c = c;
            this.speed = s;
            this.dir = d;
            this.size = z;
        }
 
        public void move() {
 
            int tmp = speed;
            switch (dir) {
            case 1// 위
 
                if (this.r - tmp > 1) {
                    this.r = this.r - tmp;
                } else {
                    tmp -= this.r - 1;
 
                    if ((tmp / (R - 1)) % 2 == 0) {
                        dir = 2;
                        this.r = tmp % (R - 1+ 1;
                    } else {
                        this.r = R - tmp % (R - 1);
                    }
                }
                break;
            case 2// 아래
                if (tmp + this.r < R) {
                    this.r = this.r + tmp;
                } else {
                    tmp -= R - this.r;
 
                    if ((tmp / (R - 1)) % 2 == 0) {
                        dir = 1;
                        this.r = R - tmp % (R - 1);
                    } else {
                        this.r = tmp % (R - 1+ 1;
                    }
                }
                break;
            case 3// 오른쪽
                if (tmp + this.c < C) {
                    this.c = this.c + tmp;
                } else {
                    tmp -= C - this.c;
 
                    if ((tmp / (C - 1)) % 2 == 0) {
                        dir = 4;
                        this.c = C - tmp % (C - 1);
                    } else {
                        this.c = tmp % (C - 1+ 1;
                    }
                }
                break;
            case 4// 왼쪽
                if (this.c - tmp > 1) {
                    this.c = this.c - tmp;
                } else {
                    tmp -= this.c - 1;
 
                    if ((tmp / (C - 1)) % 2 == 0) {
                        dir = 3;
                        this.c = tmp % (C - 1+ 1;
                    } else {
                        this.c = C - tmp % (C - 1);
                    }
                }
 
                break;
            }
 
        }
 
        @Override
        public int compareTo(Shark o) {
            return Integer.compare(o.size, this.size);
        }
 
    }
 
}
cs

'알고리즘 문제 > BOJ' 카테고리의 다른 글

[백준] 2493. 탑  (0) 2020.10.15
[백준] 17779. 게리맨더링 2  (0) 2020.10.15
[백준] 10830. 행렬 제곱  (0) 2020.10.12
[백준] 16236. 아기 상어  (0) 2020.10.12
[백준] 16235. 나무 재테크  (0) 2020.10.12
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
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
글 보관함