728x90
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
'공부 > C#' 카테고리의 다른 글
[C#] 프로그래머스 스택/큐 - 기능개발 (0) | 2025.03.09 |
---|---|
[C#] 코딩테스트에서 쓰는 함수 / 변수 (0) | 2025.03.07 |
[C#] 프로그래머스 백트래킹 - 소수 찾기 (0) | 2025.03.07 |
[C#] 프로그래머스 BFS 연습 - 단어 변환 (0) | 2025.03.07 |
[C#] 프로그래머스 DFS 연습 - 여행경로 (0) | 2025.03.07 |