공부/C#
[C#] 프로그래머스 백트래킹 - 피로도
굴러다니다니
2025. 3. 7. 22:37
https://school.programmers.co.kr/learn/courses/30/lessons/87946
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
1. DFS 위주로 생각
2. 방문 확인을 위한 visited, 현재 피로도 값 저장
3. public int answer로 표시한 정답값을 자주 업데이트 해주기
using System;
public class Solution {
public int answer = -1;
public void DFS(bool[] visited, int[,] dungeons, int currentP, int max){
if (max > answer) answer = max;
for(int i = 0; i < dungeons.GetLength(0); i++){
if (dungeons[i, 0] <= currentP && !visited[i]){ //피로도보다 진입이 작고, 방문 안했었으면 진입 가능
visited[i] = true;
DFS(visited, dungeons, currentP - dungeons[i, 1], max+1);
visited[i] = false;
}
}
}
public int solution(int k, int[,] dungeons) {
bool[] visited = new bool[dungeons.GetLength(0)];
DFS(visited, dungeons, k, 0);
return answer;
}
}
728x90
반응형