알고리즘 풀이/프로그래머스

[프로그래머스][JAVA] 피로도 (DFS)

배게 2022. 4. 2. 00:03
728x90
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
class Solution {
    private static int answer;
    private static boolean[] visited;
    
    public int solution(int k, int[][] dungeons) {
        answer = 0;
        visited = new boolean[dungeons.length];
        
        DFS(dungeons, k, 0);
 
        return answer;
    }
    
    private void DFS(int[][] dungeons, int k, int depth){
        // 돌 수 있는 던젼이 하나도 없는 경우
        answer = Math.max(answer, depth);
        
        for(int i=0; i<dungeons.length; i++){
            if(!visited[i] && k>=dungeons[i][0]){
                visited[i] = true;
                k-=dungeons[i][1];
                
                DFS(dungeons, k, depth+1);
                // System.out.println(depth);
                
                visited[i] = false;
                k+=dungeons[i][1];
            }            
        }     
    }
}
cs