[Gold II] 낚시왕 - 17143
성능 요약
메모리: 58532 KB, 시간: 792 ms
분류
구현(implementation), 시뮬레이션(simulation)
문제 설명
낚시왕이 상어 낚시를 하는 곳은 크기가 R×C인 격자판으로 나타낼 수 있다. 격자판의 각 칸은 (r, c)로 나타낼 수 있다. r은 행, c는 열이고, (R, C)는 아래 그림에서 가장 오른쪽 아래에 있는 칸이다. 칸에는 상어가 최대 한 마리 들어있을 수 있다. 상어는 크기와 속도를 가지고 있다.
낚시왕은 처음에 1번 열의 한 칸 왼쪽에 있다. 다음은 1초 동안 일어나는 일이며, 아래 적힌 순서대로 일어난다. 낚시왕은 가장 오른쪽 열의 오른쪽 칸에 이동하면 이동을 멈춘다.
- 낚시왕이 오른쪽으로 한 칸 이동한다.
- 낚시왕이 있는 열에 있는 상어 중에서 땅과 제일 가까운 상어를 잡는다. 상어를 잡으면 격자판에서 잡은 상어가 사라진다.
- 상어가 이동한다.
상어는 입력으로 주어진 속도로 이동하고, 속도의 단위는 칸/초이다. 상어가 이동하려고 하는 칸이 격자판의 경계를 넘는 경우에는 방향을 반대로 바꿔서 속력을 유지한채로 이동한다.
왼쪽 그림의 상태에서 1초가 지나면 오른쪽 상태가 된다. 상어가 보고 있는 방향이 속도의 방향, 왼쪽 아래에 적힌 정수는 속력이다. 왼쪽 위에 상어를 구분하기 위해 문자를 적었다.
상어가 이동을 마친 후에 한 칸에 상어가 두 마리 이상 있을 수 있다. 이때는 크기가 가장 큰 상어가 나머지 상어를 모두 잡아먹는다.
낚시왕이 상어 낚시를 하는 격자판의 상태가 주어졌을 때, 낚시왕이 잡은 상어 크기의 합을 구해보자.
입력
첫째 줄에 격자판의 크기 R, C와 상어의 수 M이 주어진다. (2 ≤ R, C ≤ 100, 0 ≤ M ≤ R×C)
둘째 줄부터 M개의 줄에 상어의 정보가 주어진다. 상어의 정보는 다섯 정수 r, c, s, d, z (1 ≤ r ≤ R, 1 ≤ c ≤ C, 0 ≤ s ≤ 1000, 1 ≤ d ≤ 4, 1 ≤ z ≤ 10000) 로 이루어져 있다. (r, c)는 상어의 위치, s는 속력, d는 이동 방향, z는 크기이다. d가 1인 경우는 위, 2인 경우는 아래, 3인 경우는 오른쪽, 4인 경우는 왼쪽을 의미한다.
두 상어가 같은 크기를 갖는 경우는 없고, 하나의 칸에 둘 이상의 상어가 있는 경우는 없다.
출력
낚시왕이 잡은 상어 크기의 합을 출력한다.
풀이
상어가 이동하는 과정을 표현하는 게 좀 복잡했다.
R이 6이라고 생각할 때, 아래로 10만큼 이동하는 상어의 경로는
1 2 3 4 5 6 5 4 3 2 1이다. 그래서 (R-1)*2만큼의 cycle을 가진다고 생각했다.
또한 방향을 바꿀 때 상어의 방향도 바꿔줘야 해서 처리가 까다로웠다.
처음에는 상어의 크기를 key로 하고, 상어 객체를 value로 하는 map으로 구현하려고 했는데, 상어가 더 큰 상어에 먹혀서 사라질 경우 제거하는 과정이 복잡해서 list로 바꿨다.
어떤 상어가 이동한 지점에 다른 상어가 있을 경우 그 상어가 이동한 것인지 아닌지에 따라 그 상어를 먹을지 말지가 달라진다.
그래서 상어의 크기로 리스트를 정렬하고 큰 상어부터 이동을 시작하였다. 그럼 이동한 위치에서 만나는 상어의 크기가 자신보다 크다면, 그 상어는 이미 이동을 마친 상어이기 때문에 큰 상어에게 먹히게 될 것이다.
package BOJ.Gold.g2;
import java.util.*;
public class BOJ_17143_낚시왕 {
static class Shark {
int r;
int c;
int s;
int d;
int 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;
}
@Override
public String toString() {
return "Shark{" +
"r=" + r +
", c=" + c +
", s=" + s +
", d=" + d +
", z=" + z +
'}';
}
}
static int R, C, M;
static int[][] map;
static List<Shark> sharks;
static int ans;
static int[] dr = {0, -1, 1, 0, 0};
static int[] dc = {0, 0, 0, 1, -1};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
R = sc.nextInt();
C = sc.nextInt();
M = sc.nextInt();
map = new int[R + 1][C + 1];
sharks = new ArrayList<>();
for (int i = 0; i < M; i++) {
int r = sc.nextInt();
int c = sc.nextInt();
int s = sc.nextInt();
int d = sc.nextInt();
int z = sc.nextInt();
Shark shark = new Shark(r, c, s, d, z);
sharks.add(shark);
map[r][c] = z; // 상어 크기를 저장해두기
}
// 상어 크기순으로 정렬
Collections.sort(sharks, new Comparator<Shark>() {
@Override
public int compare(Shark o1, Shark o2) {
return o1.z - o2.z;
}
});
for (int i = 1; i <= C; i++) {
// 1. 낚시왕이 상어를 잡음
catchShark(i);
moveShark();
}
System.out.println(ans);
}
static void catchShark(int c) {
for (int r = 1; r <= R; r++) {
if (map[r][c] != 0) {
ans += map[r][c];
for (int i = 0; i < sharks.size(); i++) {
if (map[r][c] == sharks.get(i).z) {
sharks.remove(i);
break;
}
}
// 상어를 먹음
map[r][c] = 0;
return;
}
}
}
static void moveShark() {
for (int z = sharks.size() - 1; z >= 0; z--) {
Shark shark = sharks.get(z); // 상어가 큰 순서대로 입력이 들어옴
// 제거를 그냥 하면 for문에서 혼선이 새익ㅁ..
if (shark.z==-1) continue; // 제거 표시
int nr = 0;
int nc = 0;
if (shark.d == 1 || shark.d == 2) {
nr = shark.r + dr[shark.d] * (shark.s % (2 * (R - 1)));
while (nr > R || nr < 1) {
if (nr > R) {
nr = R - (nr - R);
shark.d = 1;
}
if (nr < 1) {
nr = 1 + (1 - nr);
// 방향 바꾸기
shark.d = 2;
}
}
nc = shark.c;
} else {
nc = shark.c + dc[shark.d] * (shark.s % (2 * (C - 1)));
while (nc > C || nc < 1) {
if (nc > C) {
nc = C - (nc - C);
shark.d = 4;
}
if (nc < 1) {
nc = 1 + (1 - nc);
shark.d = 3;
}
}
nr = shark.r;
}
if (map[shark.r][shark.c] == shark.z) map[shark.r][shark.c] = 0;
if (map[nr][nc] > shark.z) {
// 큰 거 부터 검사하니까 작은 애들이 큰 걸 만났을 때는 이미 이동 완료임, 여기로 이동할 수 없음
shark.z=-1;
} else {
map[nr][nc] = shark.z;
shark.r = nr;
shark.c = nc;
}
}
}
}
'문제 풀이 > BOJ' 카테고리의 다른 글
[백준] 1806 - 부분합 (자바 Java) (0) | 2022.10.30 |
---|---|
[백준] 1987 - 알파벳 (자바 Java) (0) | 2022.10.28 |
[백준] 12015 - 가장 긴 증가하는 부분 수열 2 (자바 Java) (0) | 2022.10.25 |
[백준] 1520 - 내리막 길 (자바 Java) (0) | 2022.10.24 |
[백준] 11066 - 파일 합치기 (자바 Java) (0) | 2022.10.23 |