쉬운 완전 탐색 문제

import java.util.Scanner;

public class Solution {
	public static int max = 0;

	public static void main(String[] args) throws Exception {
		Scanner in = new Scanner(System.in);
		int testcase = in.nextInt();
		for (int t = 1; t <= testcase; t++) {
			max = 0;
			int n = in.nextInt();
			int limitCalories = in.nextInt();
			int ingredients[][] = new int[n][2];

			for (int i = 0; i < n; i++) {
				ingredients[i][0] = in.nextInt();// 맛
				ingredients[i][1] = in.nextInt();// 칼로리
			}

			for (int i = 0; i < n; i++) {
				findMaxTaste(i, ingredients[i][0], ingredients[i][1], ingredients, limitCalories);
			}

			System.out.println("#" + t + " " + max);
		}
	}

	public static void findMaxTaste(int index, int taste, int calories, int ingredients[][], int limit) {
		if (calories > limit || index == ingredients.length) {
			return;
		}

		max = Math.max(max, taste);

		for (int i = index + 1; i < ingredients.length; i++) {
			if (calories + ingredients[i][1] <= limit) {
				findMaxTaste(i, taste + ingredients[i][0], calories + ingredients[i][1], ingredients, limit);
			}
		}
	}
}

'알고리즘 공부' 카테고리의 다른 글

SWEA 3462: 선표의 축구 경기 예측  (0) 2021.01.10
SWEA 1949: 등산로 조성  (0) 2021.01.07
SWEA 1267: 작업순서  (0) 2021.01.03
SWEA 5653: 줄기세포 배양  (0) 2021.01.03
SWEA 5521: 상원이의 생일파티  (0) 2020.12.27

+ Recent posts