티스토리 뷰

문제풀이/백준

17143. 낚시왕

BiteSnail 2024. 1. 30. 14:47

문제 개요

칼럼 개수만큼의 상어를 잡을 때 최종적으로 잡은 상어들의 크기의 합을 구하는 문제입니다.

문제 접근

두 가지 클래스를 만들어서 해결하였습니다. 상어의 위치와 방향을 나타내는 Pos 클래스를 키로 상어의 크기와 스피드를 나타내는 Shark클래스를 값으로 이용해 Map을 만들고 순서대로 진행하였습니다.

코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

class Pos{
    private static int R;
    private static int C;
    private static final int[][] dirs = {
        {-1, 0},
        {1, 0},
        {0, 1},
        {0, -1}
    };

    private int row;
    private int col;
    private int dir;
    public int getRow() {
        return row;
    }
    public void setRow(int row) {
        this.row = row - 1;
    }
    public int getCol() {
        return col + 1;
    }
    public void setCol(int col) {
        this.col = col - 1;
    }

    public Pos(int row, int col, int dir) {
        this.row = row - 1;
        this.col = col - 1;
        this.dir = dir - 1;
    }
    public static int getR() {
        return R + 1;
    }
    public static void setR(int r) {
        R = r - 1;
    }
    public static int getC() {
        return C + 1;
    }
    public static void setC(int c) {
        C = c - 1;
    }

    public static int[][] getDirs() {
        return dirs;
    }

    public int otherSide(){
        if(dir == 0){
            return 1;
        }
        if(dir == 1){
            return 0;
        }
        if(dir == 2){
            return 3;
        }
        return 2;
    }

    public int movement(int cur, int size){
        if(cur <= 0){
            cur = Math.abs(cur);
            if(dir == 0 || dir == 3){
                dir = otherSide();
            }
        }
        cur %= (size * 2);
        if(cur > size){
            cur -= size;
            dir = otherSide();
            return size - cur;
        }
        return cur;
    }

    public Pos move(int speed){
        int originDir = dir;
        Pos next;
        if(originDir  < 2){
            next = new Pos(movement(row+dirs[dir][0]*speed, R)+1, col+1, dir+1);
        }
        else{
            next = new Pos(row+1, movement(col+dirs[dir][1]*speed, C)+1, dir+1);
        }
        dir = originDir;
        return next;
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == this){
            return true;
        }
        if(obj.getClass() != this.getClass() || obj == null){
            return false;
        }
        Pos pos = (Pos)obj;
        return pos.row == row && pos.col == col;
    }

    @Override
    public int hashCode() {
        return Objects.hash(row, col);
    }
    public int getDir() {
        return dir+1;
    }
    public void setDir(int dir) {
        this.dir = dir-1;
    }
}

class Shark{
    private int speed;
    private int size;

    public Shark(int speed, int size) {
        this.speed = speed;
        this.size = size;
    }
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }

}

public class Main {
    static Map<Pos, Shark> sharks;

    static void input() throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] first = bf.readLine().split(" ");
        Pos.setR(Integer.parseInt(first[0]));
        Pos.setC(Integer.parseInt(first[1]));
        int M = Integer.parseInt(first[2]);
        sharks = new HashMap<>();
        for(int i=0;i<M;i++){
            int[] infos = Arrays.stream(bf.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
            sharks.put(new Pos(infos[0], infos[1], infos[3]), new Shark(infos[2], infos[4]));
        }
    }

    static void process(){
        int result = 0;
        for(int i=1;i<=Pos.getC();i++){
            Map<Pos, Shark> nextSharks = new HashMap<>();
            int curCol = i;
            Optional<Pos> fished = sharks.keySet().stream().filter(pos -> pos.getCol() == curCol).collect(Collectors.minBy(Comparator.comparingInt(Pos::getRow)));
            if(!fished.isEmpty()){
                result += sharks.get(fished.get()).getSize();
                sharks.remove(fished.get());
            }
            sharks.entrySet().stream().forEach(entry -> {
                Pos curPos = entry.getKey();
                Shark curShark = entry.getValue();
                Pos moved = curPos.move(curShark.getSpeed());
                if(nextSharks.containsKey(moved)){
                    if(nextSharks.get(moved).getSize() > sharks.get(curPos).getSize()){
                        return;
                    }
                    nextSharks.remove(moved);
                }
                nextSharks.put(moved, curShark);
            });
            sharks = nextSharks;
        }

        System.out.println(result);
    }

    public static void main(String[] args) throws IOException {
        input();
        process();
    }
}

문제

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

'문제풀이 > 백준' 카테고리의 다른 글

17142. 연구소 3  (0) 2024.02.01
17140. 이차원 배열과 연산  (0) 2024.01.31
17144. 미세먼지 안녕!  (1) 2024.01.26
16236. 아기 상어  (1) 2024.01.23
15685. 드래곤 커브  (0) 2024.01.19
공지사항
최근에 올라온 글
최근에 달린 댓글
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
글 보관함