계단을 오르듯이

18429. 근손실 본문

알고리즘/백준_JAVA

18429. 근손실

happyAyun 2021. 12. 31. 14:04
package argust.fourweek;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class B_18429_근손실 {

	static int count;

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		StringTokenizer st = new StringTokenizer(in.readLine(), " ");
		int N = Integer.parseInt(st.nextToken());
		int K = Integer.parseInt(st.nextToken());
		int[] arr = new int[N];
		boolean[] check = new boolean[N];
		int[] w = new int[N];
		st = new StringTokenizer(in.readLine(), " ");
		for (int i = 0; i < N; i++) {
			arr[i] = Integer.parseInt(st.nextToken());
		}

		perm(0, N, K, arr, check, w);
		System.out.println(sb.append(count));
	}

	private static void perm(int cnt, int N, int K, int[] arr, boolean[] check, int[] w) {
		if (cnt == N) {
			int now = 500;
			for (int i = 0; i < N; i++) {
				now += w[i] - K;
				if (now < 500)
					return;
			}
			count++;
			return;
		}
		for (int i = 0; i < N; i++) {
			if (check[i])
				continue;
			check[i] = true;
			w[cnt] = arr[i]; // 중량넣기
			perm(cnt + 1, N, K, arr, check, w);
			check[i] = false;
		}
	}
}

'알고리즘 > 백준_JAVA' 카테고리의 다른 글

17478. 재귀함수가 뭔가요?  (0) 2021.12.31
17836. 공주님을 구해라  (0) 2021.12.31
20154. 이 구역의 승자는 누구야  (0) 2021.12.31
1912. 연속합  (0) 2021.12.31
20291. 파일 정리  (0) 2021.12.30