이 영역을 누르면 첫 페이지로 이동
Arc 블로그의 첫 페이지로 이동

Arc

페이지 맨 위로 올라가기

Arc

[Baekjoon] 15898: 피아의 아틀리에 ~신비한 대회의 연금술사~

  • 2023.05.10 15:32
  • Algorithm/PS
글 작성자: SeoArc

문제

최고의 폭탄을 제조하는 문제이다.

 

풀이

내 풀이

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

public class Q15898 {
    private static final String WHITE = "W";
    private static final String YELLOW = "Y";
    private static final String GREEN = "G";
    private static final String RED = "R";
    private static final String BLUE = "B";
    private static Map<String, Integer> qualities;

    static class Ingredient { // 재료
        private int efficacy; // 효능
        private String element; // 원소

        public Ingredient() {
        }

        public Ingredient(int efficacy, String element) {
            this.efficacy = efficacy;
            this.element = element;
        }

        public Ingredient(Ingredient ingredient) {
            this.efficacy = ingredient.efficacy;
            this.element = ingredient.element;
        }

        public void setEfficacy(int efficacy) {
            this.efficacy = efficacy;
        }

        public void setElement(String element) {
            this.element = element;
        }

        public void fusion(Ingredient ingredient) { // 융합!
            fusionEfficacy(ingredient.efficacy);
            fusionElement(ingredient.element);
        }

        private void fusionEfficacy(int efficacy) {
            int resultEfficacy = this.efficacy + efficacy;
            if (resultEfficacy < 0) {
                this.efficacy = 0;
                return;
            }
            this.efficacy = Math.min(resultEfficacy, 9);
        }

        private void fusionElement(String element) {
            if (element.equals(WHITE)) {
                return;
            }
            this.element = element;
        }

        public int getQuality() { // 품질 가져오기
            return qualities.get(this.element) * this.efficacy;
        }
    }

    private static BufferedReader br;
    private static Ingredient[][][] ingredients;
    private static int n;
    private static boolean[] visited;
    private static long maxTotal;
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));

        n = Integer.parseInt(br.readLine());
        ingredients = new Ingredient[n][4][4];
        visited = new boolean[n];
        qualities = new HashMap<>();
        // 각 재료 품질
        qualities.put(WHITE, 0);
        qualities.put(RED, 7);
        qualities.put(BLUE, 5);
        qualities.put(GREEN, 3);
        qualities.put(YELLOW, 2);
        // ----------------------
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 4; j++) {
                for (int k = 0; k < 4; k++) {
                    ingredients[i][j][k] = new Ingredient();
                }
            }
        }
        for (int i = 0; i < n; i++) {
            input(i);
        }

        Ingredient[][] kiln = new Ingredient[5][5];
        for (int i = 0; i < 5; i++) {
            Arrays.fill(kiln[i], new Ingredient(0, WHITE));
        }

        search(0, kiln);
        System.out.println(maxTotal);
    }

    public static void input(int index) throws IOException {
        for (int i = 0; i < 4; i++) {
            String[] row = br.readLine().split(" ");
            for (int j = 0; j < 4; j++) {
                ingredients[index][i][j].setEfficacy(Integer.parseInt(row[j]));
            }
        }

        for (int i = 0; i < 4; i++) {
            String[] row = br.readLine().split(" ");
            for (int j = 0; j < 4; j++) {
                ingredients[index][i][j].setElement(row[j]);
            }
        }
    }

    public static void search(int count, Ingredient[][] kiln) {
        if (count == 3) { // 3개 다 뽑으면
            long total = 0;
            for (Ingredient[] k : kiln) { // 가마에서 품질을 꺼내와 더한다
                for (Ingredient ingredient : k) {
                    total += ingredient.getQuality();
                }
            }
            maxTotal = Math.max(maxTotal, total); // 최고의 품질을 구한다.
            return;
        }

        for (int i = 0; i < n; i++) {
            if (visited[i]) {
                continue;
            }
            visited[i] = true;
            for (int j = 0; j <= 3; j++) { // 회전시키기
                for (int k = 0; k <= 1; k++) { // row = 0, 1
                    for (int l = 0; l <= 1; l++) { // col = 0, 1
                        search(count + 1, fusion(kiln, i, j, k, l)); // 탐색
                    }
                }
            }
            visited[i] = false;
        }
    }

    public static Ingredient[][] fusion(Ingredient[][] kiln, int index, int rotate, int row, int col) {
        Ingredient[][] copyKiln = new Ingredient[5][5]; // 가마를 카피한다.
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                copyKiln[i][j] = new Ingredient(kiln[i][j]);
            }
        }

        // 회전에 따라서 다르게 적용한다.
        if (rotate == 0) {
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    copyKiln[row + i][col + j].fusion(ingredients[index][i][j]); // row와 col에 맞춰 가마에 있는 재료와 융합한다.
                }
            }
        } else if (rotate == 1) {
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    copyKiln[row + j][col + (3 - i)].fusion(ingredients[index][i][j]);
                }
            }

        } else if (rotate == 2) {
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    copyKiln[row + (3 - i)][col + (3 - j)].fusion(ingredients[index][i][j]);
                }
            }
        } else {
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    copyKiln[row + (3 - j)][col + i].fusion(ingredients[index][i][j]);
                }
            }
        }

        return copyKiln;
    }
}

단순히 구현 문제라 딱히 설명할게 없다.

 

내부에 효능과 원소를 가진 재료 클래스를 두어 해결하였다.

 

회고

구현 굳굳

저작자표시 (새창열림)

'Algorithm > PS' 카테고리의 다른 글

[Baekjoon] 16947: 서울 지하철 2호선  (0) 2023.06.14
[Baekjoon] 22856: 트리 순회  (0) 2023.06.14
[Baekjoon] 21611: 마법사 상어와 블리자드  (0) 2023.05.10
[Baekjoon] 2176: 합리적인 이동경로  (0) 2023.05.10
[Baekjoon] 1943: 동전 분배  (0) 2023.05.10

댓글

이 글 공유하기

  • 구독하기

    구독하기

  • 카카오톡

    카카오톡

  • 라인

    라인

  • 트위터

    트위터

  • Facebook

    Facebook

  • 카카오스토리

    카카오스토리

  • 밴드

    밴드

  • 네이버 블로그

    네이버 블로그

  • Pocket

    Pocket

  • Evernote

    Evernote

다른 글

  • [Baekjoon] 16947: 서울 지하철 2호선

    [Baekjoon] 16947: 서울 지하철 2호선

    2023.06.14
  • [Baekjoon] 22856: 트리 순회

    [Baekjoon] 22856: 트리 순회

    2023.06.14
  • [Baekjoon] 21611: 마법사 상어와 블리자드

    [Baekjoon] 21611: 마법사 상어와 블리자드

    2023.05.10
  • [Baekjoon] 2176: 합리적인 이동경로

    [Baekjoon] 2176: 합리적인 이동경로

    2023.05.10
다른 글 더 둘러보기

정보

Arc 블로그의 첫 페이지로 이동

Arc

  • Arc의 첫 페이지로 이동

검색

메뉴

  • 홈
  • 태그
  • 방명록

카테고리

  • 분류 전체보기 (106)
    • Language (28)
      • C++ (0)
      • C# (0)
      • Java (28)
    • Algorithm (47)
      • Algorithm (15)
      • Data Structure (6)
      • PS (26)
    • Computer Science (22)
      • Design Pattern (1)
      • Network (14)
      • OS (7)
    • Game (0)
      • Unity (0)
    • Backend (3)
      • Spring (1)
      • JPA (2)
    • DB (0)
      • SQL (0)
    • DevOps (2)
      • AWS (0)
      • Docker (2)
      • Jenkins (0)
      • Nginx (0)
    • Software Engineering (4)
      • OOP (4)
    • AI (0)
      • Machine Learning (0)
    • Others (0)

최근 글

인기 글

댓글

공지사항

아카이브

태그

  • network
  • 네트워크
  • graph
  • 그래프
  • java
  • 알고리즘
  • 자바
  • algorithm

나의 외부 링크

정보

SeoArc의 Arc

Arc

SeoArc

블로그 구독하기

  • 구독하기
  • RSS 피드

방문자

  • 전체 방문자
  • 오늘
  • 어제

티스토리

  • 티스토리 홈
  • 이 블로그 관리하기
  • 글쓰기
Powered by Tistory / Kakao. © SeoArc. Designed by Fraccino.

티스토리툴바