Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 이클립스
- 20055
- 14466
- CMD
- Error
- 2167. 2차원 배열의 합
- 별자리 만들기
- 자바
- Java
- jenkins
- 설정
- dockercompose
- 소가길을건너간이유6
- 2108_통계학
- EC2
- SpringBoot
- documentationpluginsbootstrapper
- 21278
- Error fetching remote repo 'origin'
- 알고리즘
- to display the conditions report re-run your application with 'debug' enabled
- 날짜일수
- 18222
- 투에모스문자열
- 호석이두마리치킨
- 프로그래머스
- Eclipse
- docker
- 이산수학
- 백준
Archives
- Today
- Total
계단을 오르듯이
17143. 낚시왕 본문
- 주석으로 설명을 대신한다.
- 역시 .. 상어는 쉽지 않아~
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class B_17143_낚시왕 {
static List<Node> shark;
static int R, C, M, result;
static int[][][] arr; // 상어크기, 인덱스
static boolean[] eat;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(in.readLine(), " ");
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
shark = new ArrayList<>();
eat = new boolean[M]; // 상어 먹힘 체크
arr = new int[R][C][2];
for (int i = 0; i < M; i++) {
st = new StringTokenizer(in.readLine(), " ");
int r = Integer.parseInt(st.nextToken()) - 1; // 인덱스 0부터 시작
int c = Integer.parseInt(st.nextToken()) - 1;
int s = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
int z = Integer.parseInt(st.nextToken());
shark.add(new Node(r, c, s, d, z));
arr[r][c][0] = z;
arr[r][c][1] = i;
}
for (int i = 0; i < C; i++) { // 열만큼 연산 진행 - 낚시왕의 이동거리
search(i); // 낚시
// 상어 이동
arr = new int[R][C][2]; // 상어들의 위치 초기화
for (int j = 0; j < M; j++) {
if (eat[j])
continue;
go(j);
}
}
System.out.println(sb.append(result));
}
private static void search(int c) { // 같은 열에서 땅에 가까운(열의 값이 0에 가까운) 상어 찾기
for (int i = 0; i < R; i++) {
if (arr[i][c][0] != 0) { // 상어가 존재하면
result += arr[i][c][0]; // 결과값에 크기 더하기 - 낚시 성공
eat[arr[i][c][1]] = true; // 잡힘
arr[i][c][0] = 0; // 초기화
arr[i][c][1] = 0;
return;
}
}
}
private static void go(int idx) { // 상어 이동
Node now = shark.get(idx);
int s = now.s;
int r = now.r;
int c = now.c;
if (s == 0) { // 속력이 0이면
if (arr[r][c][0] != 0) { // 상어가 이미 존재하면
if (arr[r][c][0] < shark.get(idx).z) { // 현재의 크기가 크면
eat[arr[r][c][1]] = true; // 이전 상어 먹힘
arr[r][c][0] = shark.get(idx).z;
arr[r][c][1] = idx;
} else if (arr[r][c][0] > shark.get(idx).z) { // 이전 상어가 크면
eat[idx] = true; // 현재 상어 먹힘
}
} else {
arr[r][c][0] = shark.get(idx).z;
arr[r][c][1] = idx;
}
return;
}
if (now.d == 1) { // 위
int oper = -1;
while (s-- > 0) {
r += oper;
if (r < 0) {
r = 1;
oper = (-1) * oper; // 방향전환
} else if (r >= R) {
r = R - 2;
oper = (-1) * oper;
}
}
if (oper == 1) // 방향 전환됨
shark.set(idx, new Node(r, c, now.s, 2, now.z));
else
shark.set(idx, new Node(r, c, now.s, now.d, now.z));
} else if (now.d == 2) { // 아래
int oper = 1;
while (s-- > 0) {
r += oper;
if (r >= R) {
r = R - 2;
oper = (-1) * oper;
} else if (r < 0) {
r = 1;
oper = (-1) * oper;
}
}
if (oper == -1) // 방향 전환됨
shark.set(idx, new Node(r, c, now.s, 1, now.z));
else
shark.set(idx, new Node(r, c, now.s, now.d, now.z));
} else if (now.d == 3) { // 오
int oper = 1;
while (s-- > 0) {
c += oper;
if (c >= C) {
c = C - 2;
oper = (-1) * oper;
} else if (c < 0) {
c = 1;
oper = (-1) * oper;
}
}
if (oper == -1) // 방향 전환됨
shark.set(idx, new Node(r, c, now.s, 4, now.z));
else
shark.set(idx, new Node(r, c, now.s, now.d, now.z));
} else { // 왼
int oper = -1;
while (s-- > 0) {
c += oper;
if (c < 0) {
c = 1;
oper = (-1) * oper;
} else if (c >= C) {
c = C - 2;
oper = (-1) * oper;
}
}
if (oper == 1) // 방향 전환됨
shark.set(idx, new Node(r, c, now.s, 3, now.z));
else
shark.set(idx, new Node(r, c, now.s, now.d, now.z));
}
if (arr[r][c][0] != 0) { // 상어가 이미 존재하면
if (arr[r][c][0] < shark.get(idx).z) { // 현재의 크기가 크면
eat[arr[r][c][1]] = true; // 이전 상어 먹힘
arr[r][c][0] = shark.get(idx).z;
arr[r][c][1] = idx;
} else if (arr[r][c][0] > shark.get(idx).z) { // 이전 상어가 크면
eat[idx] = true; // 현재 상어 먹힘
}
} else {
arr[r][c][0] = shark.get(idx).z;
arr[r][c][1] = idx;
}
}
static class Node {
int r, c, s, d, z;
public Node(int r, int c, int s, int d, int z) {
super();
this.r = r;
this.c = c;
this.s = s;
this.d = d;
this.z = z;
}
}
}
'알고리즘 > 백준_JAVA' 카테고리의 다른 글
2573. 빙산 (0) | 2022.01.09 |
---|---|
17135. 캐슬디펜스 (0) | 2022.01.06 |
17144. 미세먼지 안녕! (0) | 2022.01.06 |
17179. 케이크 자르기 (0) | 2022.01.06 |
17406. 배열 돌리기 4 (0) | 2022.01.06 |