[Baekjoon] 15898: 피아의 아틀리에 ~신비한 대회의 연금술사~
글 작성자: 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 |
댓글
이 글 공유하기
다른 글
-
[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
댓글을 사용할 수 없습니다.