알고리즘 문제

[BackTracking] boj15665 java "N과 M(11)"

민돌v 2021. 7. 21. 17:56

https://www.acmicpc.net/problem/15665

 

15665번: N과 M (11)

한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해

www.acmicpc.net

package BackTracking;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

public class boj15665_N과M11 {
	static boolean visit[];
	static int list[];
	static StringBuilder sb = new StringBuilder();

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String temp[] = br.readLine().split(" ");

		int n = Integer.parseInt(temp[0]);
		int m = Integer.parseInt(temp[1]);
		list = new int[n];

		temp = br.readLine().split(" ");

		for (int i = 0; i < n; i++)
			list[i] = Integer.parseInt(temp[i]);

		Arrays.sort(list);
		visit = new boolean[n];

		Permutation(n, m, new int[m], 0, 0);

		System.out.println(sb.toString());
	}

	public static void Permutation(int n, int m, int result[], int count, int start) {
		HashSet<Integer> hashset = new HashSet<Integer>();

		if (count == m) {
			for (int a : result)
				sb.append(a + " ");
			sb.append("\n");
			return;
		}

		for (int i = 0; i < n; i++) {

			if (hashset.isEmpty()) {

				result[count] = list[i];
				hashset.add(list[i]);
				Permutation(n, m, result, count + 1, i);
			}

			else {
				if (!hashset.contains(list[i])) {
					result[count] = list[i];
					hashset.add(list[i]);
					Permutation(n, m, result, count + 1, i);
				}
			}

		}
	}
}