[BaekJoon] 17143 낚시왕 (Java)

오태호·2023년 5월 17일
0

백준 알고리즘

목록 보기
227/395
post-thumbnail

1.  문제 링크

https://www.acmicpc.net/problem/17143

2.  문제




3.  소스코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    static final int[][] DIRECTION = {{}, {-1, 0}, {1, 0}, {0, 1}, {0, -1}};
    static final int[] REVERSE_DIR = {0, 2, 1, 4, 3};
    static int R, C, M;
    static Shark[][] sharks;

    static void input() {
        Reader scanner = new Reader();

        R = scanner.nextInt();
        C = scanner.nextInt();
        M = scanner.nextInt();
        sharks = new Shark[R][C];

        for(int idx = 0; idx < M; idx++) {
            int x = scanner.nextInt() - 1, y = scanner.nextInt() - 1;
            int speed = scanner.nextInt(), direction = scanner.nextInt(), size = scanner.nextInt();

            if(sharks[x][y] != null && sharks[x][y].size > size) continue;
            sharks[x][y] = new Shark(x, y, speed, direction, size);
        }
    }

    static void solution() {
        int totalSize = 0;
        for (int col = 0; col < C; col++) {
            totalSize += catchShark(col);
            moveShark();
        }

        System.out.println(totalSize);
    }

    static void moveShark() {
        Shark[][] newSharks = new Shark[R][C];

        for(int row = 0; row < R; row++) {
            for(int col = 0; col < C; col++) {
                if(sharks[row][col] == null) continue;

                Shark shark = sharks[row][col];
                for(int moveNum = 0; moveNum < shark.speed; moveNum++) {
                    int cx = shark.x + DIRECTION[shark.direction][0], cy = shark.y + DIRECTION[shark.direction][1];
                    if(!isInMap(cx, cy)) {
                        shark.direction = REVERSE_DIR[shark.direction];
                        cx = shark.x + DIRECTION[shark.direction][0]; cy = shark.y + DIRECTION[shark.direction][1];
                    }

                    shark.x = cx; shark.y = cy;
                }

                if(newSharks[shark.x][shark.y] != null && newSharks[shark.x][shark.y].size > shark.size) continue;
                newSharks[shark.x][shark.y] = shark;
            }
        }

        sharks = newSharks;
    }

    static boolean isInMap(int x, int y) {
        if(x >= 0 && x < R && y >= 0 && y < C) return true;
        return false;
    }

    static int catchShark(int col) {
        int size = 0;
        for(int row = 0; row < R; row++) {
            if(sharks[row][col] != null) {
                size = sharks[row][col].size;
                sharks[row][col] = null;
                break;
            }
        }

        return size;
    }

    static class Shark {
        int x, y, speed, direction, size;

        public Shark(int x, int y, int speed, int direction, int size) {
            this.x = x;
            this.y = y;
            this.speed = speed;
            this.direction = direction;
            this.size = size;
        }
    }

    public static void main(String[] args) {
        input();
        solution();
    }

    static class Reader {
        BufferedReader br;
        StringTokenizer st;

        public Reader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while(st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
    }
}
profile
자바, 웹 개발을 열심히 공부하고 있습니다!

0개의 댓글