1. ๋ฌธ์ : https://school.programmers.co.kr/learn/courses/30/lessons/172927
2. ํ์ด
5 ์ง๋ ๋๋ง๋ค dfs์ ๋ฃ๋ tool๊ฐ ๋ฐ๊ฟ์ค๋๊ฑฐ ์๋ฆ๋ค์ธ ์ ๋๋ค...
3. ์ฝ๋
class Solution {
static int min = Integer.MAX_VALUE;
public int solution(int[] picks, String[] minerals) {
int answer = 0;
int dia = picks[0];
int iron = picks[1];
int stone = picks[2];
if (dia > 0)
dfs(0, "d", 0, minerals, dia-1, iron, stone);
if (iron > 0)
dfs(0, "i", 0, minerals, dia, iron-1, stone);
if (stone > 0)
dfs(0, "s", 0, minerals, dia, iron, stone-1);
return min;
}
public void dfs(int fatigue, String tool, int index, String[] minerals, int dia, int iron, int stone) {
if ( index >= minerals.length ) {
min = Math.min(min, fatigue);
return;
}
if (minerals[index].equals("diamond")) {
if ("d".equals(tool)) {
fatigue += 1;
} else if ("i".equals(tool)) {
fatigue += 5;
} else {
fatigue += 25;
}
} else if (minerals[index].equals("iron")) {
if ("d".equals(tool)) {
fatigue += 1;
} else if ("i".equals(tool)) {
fatigue += 1;
} else {
fatigue += 5;
}
} else if (minerals[index].equals("stone")) {
if ("d".equals(tool)) {
fatigue += 1;
} else if ("i".equals(tool)) {
fatigue += 1;
} else {
fatigue += 1;
}
}
if ((index + 1) % 5 == 0) {
if ((dia == 0 && iron == 0 && stone == 0) ) {
min = Math.min(min, fatigue);
return;
}
if (dia > 0) {
dfs (fatigue, "d", index+1, minerals, dia-1, iron, stone);
}
if (iron > 0) {
dfs(fatigue, "i", index+1, minerals, dia, iron-1, stone);
}
if (stone > 0) {
dfs(fatigue, "s", index+1, minerals, dia, iron, stone-1);
}
} else {
dfs(fatigue, tool, index+1, minerals, dia, iron, stone);
}
}
}
'๐ป Coding Problems Solving > DFS | BFS | Backtracking' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ 9466] ํ ํ๋ก์ ํธ (0) | 2023.08.01 |
---|---|
[BOJ 2668] ์ซ์๊ณ ๋ฅด๊ธฐ (0) | 2023.06.27 |
[BOJ 17086] ์๊ธฐ์์ด2 (0) | 2023.05.16 |
[BOJ 17086] ์ด๋ชจํฐ์ฝ (0) | 2023.05.16 |
[BOJ 12851] ์จ๋ฐ๊ผญ์ง2 (java) (0) | 2023.04.18 |
์ต๊ทผ๋๊ธ