https://www.acmicpc.net/problem/2798
package backjun.Dbruteforce;
import java.util.Scanner;
public class 블랙잭 {
static int result;
static boolean[] visited;
static int[] card_arr;
static void bt(int m, int s, int deph){
if (deph == 3){
if(s<= m)
result = Math.max(result, s);
return;
}
for(int i=0;i<visited.length;i++){
if(!visited[i]){
visited[i] = true;
bt(m, s+card_arr[i], deph+1);
visited[i] = false;
}
}
}
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
int n = sc.nextInt(), m= sc.nextInt();
card_arr = new int[n];
visited = new boolean[n];
for(int i=0; i<n; i++)
card_arr[i] = sc.nextInt();
sc.close();
bt(m, 0, 0);
System.out.println(result);
}
}
dfs 방식으로 해결
visited를 통해 방문 여부를 확인하며 총 3개의 카드를 탐색 했을 때 주어진 M보다 작은 경우에만 최대 값을 비교하여 저장한다.
자바로 알고리즘 공부 시작한 뒤로 처음으로 접한 dfs 알고리즘
Static의 개념을 알게해준 문제
Static ->
Static으로 생성된 변수나 함수는 Heap영역이 아닌 Static 영역에 생성된다. Static 영역에 생성된 멤버들은 어느 영역에서나 참조가 가능하다.
하지만 Static 메모리 영역은 Garbage Collector가 관리하지 않는 영역으로 프로그램이 종료 될 때까지 메모리를 계속 할당 받는다. 따라서 너무 남발하지 않아야한다.