gimmickbutreal

[프로그래머스/자바] 피로도 - DFS 본문

Algorithm/Java

[프로그래머스/자바] 피로도 - DFS

isshosng 2023. 4. 30. 19:16

https://school.programmers.co.kr/learn/courses/30/lessons/87946

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

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
class Solution {
    
    public int answer = 0;
    public boolean[] visited;
    
    public int solution(int k, int[][] dungeons) {
        visited = new boolean[dungeons.length]; // 던전의 개수만큼만 방문함
        
        dfs(0,k,dungeons); 
        
        return answer;
    }
    
    public void dfs(int depth, int k, int[][] dungeons){
             
        for(int i = 0; i < dungeons.length; i++){ // 던전의 크기만큼만 보기
            if(!visited[i] && k >= dungeons[i][0]){  
                visited[i] = true;
                dfs(depth+1,k-dungeons[i][1],dungeons);
                visited[i] = false;
            }
        }
        answer = Math.max(answer,depth);
    }
    
}
cs