변수의 기록

(코테) 백준_1094_모든수열 백트레킹 본문

카테고리 없음

(코테) 백준_1094_모든수열 백트레킹

불광동 물주먹 2025. 5. 14. 20:45

 

 

 

 

 

 

 

 

public class Backjun_10974 {
	static int n; 
	static int[] list;
	static int[] result;
	static StringBuilder sb = new StringBuilder();

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		n = Integer.parseInt(br.readLine());
		list = new int[n + 1]; 
		result = new int[n];
		for (int i = 1; i <= n; i++) list[i] = 1;

		dfs(0);
		System.out.print(sb);  // 여기서 단 한 번 출력
	}

	public static void dfs(int index) {
		if (index == n) {
			for (int i = 0; i < n; i++) {
				sb.append(result[i]).append(" ");
			}
			sb.append("\n");
			return;
		}
		for (int j = 1; j <= n; j++) {
			if (list[j] > 0) {
				list[j]--;
				result[index] = j;
				dfs(index + 1);
				list[j]++;
			}
		}
	}
}