본문 바로가기
Algorithms/BOJ

[BOJ] 17143 낚시왕(Java)

by 그냥팬더 2021. 5. 1.
반응형

낚시왕


문제에 다양한 종류의 조건이 나온다.

처음 조건은
- 1. 2차원 Map에서 상어는 한 칸에 최대 한마리있을 수 있다.
- 2. 상어는 크기와 속도를 가지고 있다.

그리고 전체 시뮬레이션은
낚시왕은 1번열의 한칸 왼쪽에 존재하고, 가장 오른쪽 열을 벗어나면 종료된다.
-1. 낚시왕이 오른칸으로 한칸 이동한다.
-2. 낚시왕이 있는 열에 있는 상어 중에서 땅과 제일 가까운 상어를 잡는다. 상어를 잡으면 격자판에서 잡은 상어가 사라진다.
-3. 상어가 이동한다.(상어는 초당 속도칸만큼 이동하고, 격자에 닿는 순간 방향을 바꾼다)

즉, 상어는 좌표(r,c)와 크기(size), 속도(speed)를 가지고 있음을 알수 있고 해당하는 클래스를 작성해준다.
또한 상어가 이동을 하기때문에 해당하는 메소드도 작성한다

class Shark {
    int row;
    int column;
    int speed;
    int direction;
    int size;

    public Shark( int size, int row, int column, int speed, int direction ) {
        this.row = row;
        this.column = column;
        this.speed = speed;
        this.direction = direction;
        this.size = size;
    }

    public void move( int R, int C ) {
        int[] dc = {0, 0, 1, -1};
        int[] dr = {-1, 1, 0, 0};

        int end = C;
        if (this.direction < 3) {
            end = R;
        }

        int changed = 2 * (end - 1);
        int speed = this.speed % changed;

        for (int i = 0; i < speed; i++) {
            if (this.direction < 3) {
                // 위, 아래
                if ((this.row == end && this.direction == 2) || (this.row == 1 && this.direction == 1)) {
                    this.changeDirection();
                }
                this.row += dr[this.direction - 1];
            } else {
                if ((this.column == end && this.direction == 3) || (this.column == 1 && this.direction == 4)) {
                    this.changeDirection();
                }
                this.column += dc[this.direction - 1];
            }
        }
    }

    public void changeDirection() {
        int next = this.direction + 1;
        if (next % 2 == 0) {
            this.direction = next;
        } else {
            this.direction = next - 2;
        }
    }
}

위의 코드에서 ChangeDirection을 하는 이유는 전체 시뮬레이션 코드에 따라서 조정되도록 해두었다.

그리고 각각의 Map에 저장해두는 값을 더 알기 편하게 Shark형태의 2차원 배열로 선언해 문제를 풀었다.

<전체소스>

/*
 *  @Author: Pandahun
 *  @Content: 낚시왕
 */
import java.io.*;
import java.util.*;

public class problem17143 {

    private static int R, C, N;
    private static Shark[][] map;
    private static List sharkList;
    private static int answer;

    public static void main( String[] args ) throws IOException, NumberFormatException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");


        R = Integer.parseInt(st.nextToken());
        C = Integer.parseInt(st.nextToken());
        N = Integer.parseInt(st.nextToken());

        answer = 0;
        map = new Shark[R + 1][C + 1];
        sharkList = new ArrayList<>();

        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine(), " ");
            int r = Integer.parseInt(st.nextToken());
            int c = Integer.parseInt(st.nextToken());
            int speed = Integer.parseInt(st.nextToken());
            int d = Integer.parseInt(st.nextToken());
            int size = Integer.parseInt(st.nextToken());
            Shark shark = new Shark(size, r, c, speed, d);
            sharkList.add(shark);
            map[r][c] = shark;
        }

/*
        for(int i = 1 ; i<=R;i++){
            for(int k = 1 ; k<=C;k++)
                System.out.print(map[i][k] +" ");
            System.out.println();
        }*/

        for (int idx = 1; idx <= C; idx++) {
            kill(idx);
            survive();
        }
        System.out.println(answer);

    }

    static void kill( int idx ) {
        for (int r = 1; r <= R; r++) {
            if (map[r][idx] != null) {
                Shark killedShark = map[r][idx];
                answer += map[r][idx].size;
                sharkList.remove(killedShark);
                map[r][idx] = null;
                break;
            }
        }
    }

    static void survive() {
        map = new Shark[R + 1][C + 1];
        for (Shark shark : sharkList) {
            shark.move(R, C);
        }

        for (int i = sharkList.size() - 1; i >= 0; i--) {

            Shark sharks = sharkList.get(i);
            if (map[sharks.row][sharks.column] == null) {
                map[sharks.row][sharks.column] = sharks;
            } else {
                if (map[sharks.row][sharks.column].size > sharks.size) {
                    sharkList.remove(sharks);
                } else {
                    sharkList.remove(map[sharks.row][sharks.column]);
                    map[sharks.row][sharks.column] = sharks;
                }
            }
        }

    }
}

class Shark {
    int row;
    int column;
    int speed;
    int direction;
    int size;

    public Shark( int size, int row, int column, int speed, int direction ) {
        this.row = row;
        this.column = column;
        this.speed = speed;
        this.direction = direction;
        this.size = size;
    }

    public void move( int R, int C ) {
        int[] dc = {0, 0, 1, -1};
        int[] dr = {-1, 1, 0, 0};

        int end = C;
        if (this.direction < 3) {
            end = R;
        }

        int changed = 2 * (end - 1);
        int speed = this.speed % changed;

        for (int i = 0; i < speed; i++) {
            if (this.direction < 3) {
                // 위, 아래
                if ((this.row == end && this.direction == 2) || (this.row == 1 && this.direction == 1)) {
                    this.changeDirection();
                }
                this.row += dr[this.direction - 1];
            } else {
                if ((this.column == end && this.direction == 3) || (this.column == 1 && this.direction == 4)) {
                    this.changeDirection();
                }
                this.column += dc[this.direction - 1];
            }
        }
    }

    public void changeDirection() {
        int next = this.direction + 1;
        if (next % 2 == 0) {
            this.direction = next;
        } else {
            this.direction = next - 2;
        }
    }
}

[Github]

 

반응형

'Algorithms > BOJ' 카테고리의 다른 글

[BOJ] 17822 원판 돌리기(Java)  (0) 2021.05.01
[BOJ] 7576 토마토(Java)  (0) 2021.05.01

댓글