Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.04.18 - [BOJ] 15649. N과 M(1) #55

Closed
suhyunsim opened this issue Apr 18, 2021 · 0 comments
Closed

21.04.18 - [BOJ] 15649. N과 M(1) #55

suhyunsim opened this issue Apr 18, 2021 · 0 comments
Assignees
Labels

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Apr 18, 2021

문제

핵심 아이디어

  • 재귀: 어떤 위치에 올 수 있는 수를 결정

  • 1부터 N까지 수 중에서 앞에서 사용하지 않은 수

  • 위치 변화 → 함수의 인자로 위치가 필요

  • 사용한 수의 변화

  • checked[i]: i를 사용했으면 true, 아니면 false

  • array[i]: 수열을 저장

  • go(index, n, m): index 번째의 수를 결정

어려운 점, 실수

풀이

package main.java.com.poogle.BOJ.Q15649;

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

public class Main {
    //사용한지 여부를 알기 위해 (checked[i]: 사용했으면 true)
    static boolean[] checked = new boolean[10];
    //고른 수열을 저장
    static int[] array = new int[10];

    static StringBuilder sb = new StringBuilder();

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

        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        go(0, N, M);
        bw.write(String.valueOf(sb));
        bw.flush();
        bw.close();
        br.close();
    }

    //index번 째의 수를 결정
    private static void go(int index, int n, int m) {
        //마지막 도착
        if (index == m) {
            for (int i = 0; i < m; i++) {
                sb.append(array[i]);
                if (i != m - 1)
                    sb.append(" ");
            }
            sb.append("\n");
            return;
        }
        for (int i = 1; i <= n; i++) {
            // 1 ~ N 중에서 사용하지 않은 수 찾기
            if (!checked[i]) {
                //수 i를 사용하기
                array[index] = i;
                //함수 호출 전에 사용했다고 준비
                checked[i] = true;
                go(index + 1, n, m);
                //호출 후에 index에는 다른 i가 들어가야 함 -> 다른 i는 사용하지 않은 상태
                checked[i] = false;
            }
        }
    }

}
@suhyunsim suhyunsim added this to the 4월 3주 차 milestone Apr 18, 2021
@suhyunsim suhyunsim self-assigned this Apr 18, 2021
@suhyunsim suhyunsim added 백트래킹 브루트포스 완전탐색 성공 맞은 문제 실버 BOJ - 실버 재귀 labels Apr 18, 2021
suhyunsim added a commit that referenced this issue Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant