View

๋ฐ˜์‘ํ˜•

10974๋ฒˆ: ๋ชจ๋“  ์ˆœ์—ด

๋ฌธ์ œ

N์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ์ˆœ์—ด์„ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N(1 โ‰ค N โ‰ค 8)์ด ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ N!๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ๋ชจ๋“  ์ˆœ์—ด์„ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1

3

์˜ˆ์ œ ์ถœ๋ ฅ 1

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

๊ทธ๋ƒฅ ์ˆœ์—ด


๐Ÿคฆโ€โ™€๏ธ My Solution

package ์ˆœ์—ด์กฐํ•ฉ;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BJ_10974 {
    static int N;
    static boolean check[];
    static int select[];

    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        check = new boolean[N];
        select = new int[N];
        permu(0);
    }

    static public void permu(int cnt) {
        // ๊ธฐ์ €์กฐ๊ฑด
        if (cnt == N) {
            for (int i = 0; i < N; i++) {
                System.out.print(select[i] + " ");
            }
            System.out.println();
            return;
        }

        for (int i = 0; i < N; i++) {
            if (check[i])
                continue;
            check[i] = true;
            select[cnt] = i + 1;
            permu(cnt + 1);
            check[i] = false;
        }
    }

}

๋ฐ˜์‘ํ˜•
Share Link
reply
๋ฐ˜์‘ํ˜•
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30