Contents

๋ฐฑ์ค€ 15651๋ฒˆ, N๊ณผ M (3) (with.Java)

   Mar 3, 2025     2 min read

๋ฐฑ์ค€ 15651๋ฒˆ, N๊ณผ M (3) (with.Java) ์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณธ ๊ธ€์ž…๋‹ˆ๋‹ค.

์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉฐ, ํ’€์—ˆ๋˜ ๋ฌธ์ œ์— ๋Œ€ํ•œ ํšŒ๊ณ ์™€ ๋‹ค๋ฅธ ํ’€์ด ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๋ฉฐ, ์•Œ์•„๊ฐ€๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ์— ๋Œ€ํ•ด ๋จผ์ € ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ

์ž์—ฐ์ˆ˜ N๊ณผ M์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์•„๋ž˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ธธ์ด๊ฐ€ M์ธ ์ˆ˜์—ด์„ ๋ชจ๋‘ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

  • 1๋ถ€ํ„ฐ N๊นŒ์ง€ ์ž์—ฐ์ˆ˜ ์ค‘์—์„œ M๊ฐœ๋ฅผ ๊ณ ๋ฅธ ์ˆ˜์—ด.
  • ๊ฐ™์€ ์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ๊ณจ๋ผ๋„ ๋œ๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , ์ •์ˆ˜ n์ด ์ฃผ์–ด์ง„๋‹ค.

n์€ ์–‘์ˆ˜์ด๋ฉฐ 11๋ณด๋‹ค ์ž‘๋‹ค.

์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค, n์„ 1, 2, 3์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๋ฌธ์ œ ํ’€์ด

import java.io.*;
import java.util.StringTokenizer;

class Main{
    public static int N, M;
    public static int[] arr;
    public static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        br.close();


        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        arr = new int[M];

        dfs(0);

        bw.flush();
        bw.close();
    }

    public static void dfs(int depth) throws Exception{
        if(depth == M){
            for (int i = 0; i < M; i++){
                bw.write(arr[i] + " ");
            }
            bw.write("\n");
            return;
        }

        for(int i = 1; i <= N; i++){
            arr[depth] = i;
            dfs(depth + 1);
        }
    }
}

ํ’€์ด ์„ค๋ช…

์ด ์ฝ”๋“œ๋Š” ๋ฐฑํŠธ๋ž˜ํ‚น์„ ํ™œ์šฉํ•˜์—ฌ 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ์ˆซ์ž๋กœ ๊ตฌ์„ฑ๋œ M๊ฐœ์˜ ์ˆœ์—ด์„ ์ƒ์„ฑํ•˜๊ณ  ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ž…๋‹ˆ๋‹ค.

๋จผ์ €, BufferedReader๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅ์„ ๋ฐ›๊ณ , StringTokenizer๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋œ ๋‘ ๊ฐœ์˜ ์ •์ˆ˜ N๊ณผ M์„ ํŒŒ์‹ฑํ•ฉ๋‹ˆ๋‹ค.

N์€ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์ˆซ์ž์˜ ๋ฒ”์œ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, M์€ ์ถœ๋ ฅํ•  ์ˆœ์—ด์˜ ๊ธธ์ด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•œ ํ›„ BufferedReader๋ฅผ ๋‹ซ์Šต๋‹ˆ๋‹ค.

๋ฐฐ์—ด arr์€ ํฌ๊ธฐ๊ฐ€ M์ธ ์ •์ˆ˜ ๋ฐฐ์—ด๋กœ, ํ˜„์žฌ ๊ตฌ์„ฑ ์ค‘์ธ ์ˆœ์—ด์„ ์ €์žฅํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

์ดํ›„ dfs(0)์„ ํ˜ธ์ถœํ•˜์—ฌ ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS)์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

dfs ๋ฉ”์„œ๋“œ๋Š” ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ํ˜„์žฌ depth ๊ฐ’์„ ์ธ์ž๋กœ ๋ฐ›์œผ๋ฉฐ, depth๊ฐ€ M๊ณผ ๊ฐ™์•„์ง€๋ฉด arr์— ์ €์žฅ๋œ ๊ฐ’์„ BufferedWriter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์ด๋•Œ, ๊ฐ ์ˆซ์ž๋Š” ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜๋ฉฐ, ์ˆœ์—ด์ด ์™„์„ฑ๋  ๋•Œ๋งˆ๋‹ค ๊ฐœํ–‰ ๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์„ ํƒํ•˜๋ฉฐ arr[depth]์— ์ €์žฅํ•œ ๋’ค, dfs(depth + 1)์„ ํ˜ธ์ถœํ•˜์—ฌ ๋‹ค์Œ ๊นŠ์ด๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋ชจ๋“  DFS ํ˜ธ์ถœ์ด ์ข…๋ฃŒ๋œ ํ›„ BufferedWriter์˜ ๋ฒ„ํผ๋ฅผ ๋น„์šฐ๊ณ  ๋‹ซ์•„ ์ถœ๋ ฅ ์ž‘์—…์„ ๋งˆ๋ฌด๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

์ด๋ฅผ ํ†ตํ•ด ์ค‘๋ณต๋œ ์ˆซ์ž๋ฅผ ํ—ˆ์šฉํ•˜๋Š” ๊ธธ์ด M์˜ ๋ชจ๋“  ์ˆœ์—ด์„ ํšจ์œจ์ ์œผ๋กœ ์ƒ์„ฑํ•˜๊ณ  ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.