Skip to content

Conversation

@sunha20
Copy link
Contributor

@sunha20 sunha20 commented Jan 19, 2026

🚀 이슈 번호

Resolve: {#2314}

🧩 문제 해결

스스로 해결:

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지 DP, 외판원순회
  • 🔹 어떤 방식으로 접근했는지
    완전탐색을 하기에는 시간초과 문제가 있음
    사람당 한번씩만 그림을 살 수 있고 왔던 경로와 현재 위치에 따라 다음 그림 구매의 가격이 정해지기 때문에, 외판원 순회처럼 풀 수 있겠다고 생각했음.
    dp[route|mask][n] > cost[prev].charAt(n) - '0' 이부분을 동일 할때도 탐색을 진행하면 시간 초과가 남.

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O(2^N * N^2)
  • 이유:
    경로 개수가 2^N개, 경로별로 마지막 위치가 N개로 총 2^N * N 개의 상태가 존재
    각 상태 별로 N개의 다음 이동경로를 탐색함.

💻 구현 코드

import java.io.*;

public class Main {
    static int N, maxCnt=0;
    static String[] cost;
    static int[][] dp;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        // 입력받기
        N = Integer.parseInt(br.readLine());
        cost = new String[N];
        for (int n = 0; n<N ; n++) {
            cost[n] = br.readLine();
        }

        // DP만들기. N개의 bit로 표현하는 수가... 2^16 - 1 = 니까 int안에 표현 가능.
        // 근데 128MB면 ... 4MB = 100만개 -> 3200만개까지
        // 2^16 * 10 면 충분히 가능하네..

        // DP 배열을 [2^N-1, N] 크기로 만들면됨. -> 무조건 1에서 시작이니까.
        // 그러면 초기 위치는 [0,0]이 되겠지. (예술가는 0부터 N-1로 만들기)
        dp = new int[1 << (N+1)][N];
        // 초기화
        for (int r = 0; r < 1 << (N+1); r++) {
            for (int c = 0; c < N; c++) {
                dp[r][c] = Integer.MAX_VALUE;
            }
        }

        // DP[경로][현재 위치]
        // = 그림을 구매할 수 있는 최소 가격.
        // = min(for -> (DP[현재 위치 뺀 경로][n] + n에서 현재 위치로 파는 가격))


        // 순서는 해당 경로에 대한 모든 n을 구하고, +1된 경로로.

        dp[1][0] = 0; // 이미 1번이 가지고 있음.
        a(1,0);

        System.out.println(maxCnt);
    }

    static void a(int route, int prev) {
        int currentPeopleCount = Integer.bitCount(route);
        maxCnt = Math.max(maxCnt, currentPeopleCount);
        for (int n = 0; n < N; n++) {
            int mask = 1<<n;
            if ((route & mask) != 0)  // 이미 거쳐온 경로
                continue;

            // 갈 수 있는 경로면,
            // 아까 전 위치에서 지금 위치로 파는 가격을 현재 가격이랑 비교해봐야겠지.
            if (dp[route][prev] <= cost[prev].charAt(n) - '0' && dp[route|mask][n] > cost[prev].charAt(n) - '0'){
                dp[route|mask][n] = cost[prev].charAt(n) - '0';
                a(route|mask, n);
            }
        }
    }
}

@sunha20 sunha20 linked an issue Jan 19, 2026 that may be closed by this pull request
@sunha20 sunha20 self-assigned this Jan 19, 2026
@sunha20 sunha20 merged commit 10f03bf into main Jan 30, 2026
1 check passed
@sunha20 sunha20 deleted the sunha/2314/1 branch January 30, 2026 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

260119 : 코딩테스트

2 participants