작성
·
49
·
수정됨
0
안녕하세요. 선생님 배열 2개 말고 2차원 배열로 풀었는데 괜찮나요??
그리고 강의에서 문제들 입력받으실 때 왜 static으로 받지 않고 메인 메서드에서 받아서, DFS의 파라미터로 계속 주고받으시는지 궁금합니다.
import java.util.Scanner;
public class INF0803 {
static int[][] problems;
static int problemsCount;
static int limitTime;
static int maxScore = Integer.MIN_VALUE;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
problemsCount = sc.nextInt();
limitTime = sc.nextInt();
problems = new int[problemsCount][2];
for (int i = 0; i < problemsCount; i++) {
for (int j = 0; j < 2; j++) {
problems[i][j] = sc.nextInt();
}
}
DFS(0, 0, 0);
System.out.println(maxScore);
}
static void DFS(int index, int currentScore, int currentTime) {
if (currentTime > limitTime) {
return;
}
if (index == problemsCount) {
maxScore = Math.max(maxScore, currentScore);
return;
}
DFS(index + 1, currentScore + problems[index][0], currentTime + problems[index][1]);
DFS(index + 1, currentScore, currentTime);
}
}
답변 1
0
아하 감사합니다!