https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWT-lPB6dHUDFAVT
์คํจ ์ฝ๋ - ๋ชจ๋ tc ํต๊ณผx
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Solution {
static int N, L;
static int[] T, K;
static boolean[] visited;
static int[] result; //์กฐํฉ ๊ฒฐ๊ณผ(์ธ๋ฑ์ค ์ ์ฅ)
static int max = 0;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int TC = Integer.parseInt(br.readLine());
for(int tc = 1; tc <= TC; tc++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(st.nextToken());
L = Integer.parseInt(st.nextToken()); //์ ํ ์นผ๋ก๋ฆฌ
T = new int[N]; //๋ง ์ ์
K = new int[N]; //์นผ๋ก๋ฆฌ
for(int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine(), " ");
T[i] = Integer.parseInt(st.nextToken());
K[i] = Integer.parseInt(st.nextToken());
}
visited = new boolean[N];
for(int i = 1; i <= N; i++) {
result = new int[i];
combination(0, 0, i); //n๊ฐ ์กฐํฉ
}
//์ถ๋ ฅ
System.out.println("#" + tc + " " + max);
}
}
public static void combination(int start, int depth, int n) {
if(depth == n) {
int scoreSum = 0;
int calSum = 0;
for(int i = 0; i < result.length; i++) {
scoreSum += T[result[i]];
calSum += K[result[i]];
}
if(calSum <= L) {
max = Math.max(max, scoreSum);
}
return;
}
for(int i = start; i < N; i++) {
if(visited[i]) continue;
visited[i] = true;
result[depth] = i; //์ธ๋ฑ์ค๋ฅผ ์ ์ฅ
combination(i + 1, depth + 1, n);
visited[i] = false;
}
}
}
์๊ณ ๋ณด๋ ํ ์ผ ์์์ max = 0์ผ๋ก ์ด๊ธฐํ๋ฅผ ์ํด์คฌ๋ค.....
์ ๋ ๊ฒ ์กฐํฉ์ผ๋ก ํ ์๋ ์์ง๋ง, ๋ถ๋ถ ์งํฉ์ผ๋ก๋ ํ ์ ์๋ค.
๋ค๋ฅธ ํ์ด
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Solution {
static int N, L;
static int[] T;
static int[] K;
static int max;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int TC = Integer.parseInt(br.readLine());
for(int tc = 1; tc <= TC; tc++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(st.nextToken());
L = Integer.parseInt(st.nextToken()); //์ ํ ์นผ๋ก๋ฆฌ
T = new int[N]; //๋ง ์ ์
K = new int[N]; //์นผ๋ก๋ฆฌ
for(int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine(), " ");
T[i] = Integer.parseInt(st.nextToken());
K[i] = Integer.parseInt(st.nextToken());
}
max = 0;
dfs(0, 0, 0);
//์ถ๋ ฅ
System.out.println("#" + tc + " " + max);
}
}
public static void dfs(int depth, int score, int cal) {
if (cal > L) return;
if(depth == N) {
max = Math.max(max, score);
return;
}
dfs(depth + 1, score + T[depth], cal + K[depth]); //์ฌ๋ฃ ํฌํจ
dfs(depth + 1, score, cal); //์ฌ๋ฃ ํฌํจx
}
}
'Algorithm > SWEA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
SWEA - ์๋ก์ด ๋ถ๋ฉด์ฆ ์น๋ฃ๋ฒ (0) | 2023.02.05 |
---|---|
SWEA - 1249๋ฒ ๋ณด๊ธ๋ก (0) | 2022.09.30 |
SWEA 6808๋ฒ - ๊ท์์ด์ ์ธ์์ด์ ์นด๋๊ฒ์ (0) | 2022.08.09 |
SWEA 9229๋ฒ - ํ๋น์ด์ Spot Mart (0) | 2022.08.08 |
SWEA 1228๋ฒ - ์ํธ๋ฌธ1 (0) | 2022.08.08 |