Skip to content

Conversation

@ikjo93
Copy link
Collaborator

@ikjo93 ikjo93 commented Dec 4, 2022

접근방법

  • 뭔가 DP 냄새도 살짝 났지만 처음엔 완탐으로 접근했다가 시간 초과나왔습니다 ㅠ
    • ~거리하면 보통 DP 문제인 경우가 많더라구요.. ㅋㅋ
    • 하지만, N이 최대 1000이라 완탐으로 해볼만하다고 생각했었습니다...ㅋㅋ
  • 그래도 일단 완탐으로 접근하고나니 어느 연산에서 반복이 일어나는지를 파악할 수 있어 점화식을 세우기 용이했습니다 👍
  • 완탐 풀이도 공유해봅니다!!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

class Main {

    static int n, answer = Integer.MAX_VALUE;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        String s = br.readLine(); // 0 : B, 1 : O, 2 : J

        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[]{0, 0, 0}); // 0 : 현재 위치, 현재 상태, 현재 에너지

        while (!queue.isEmpty()) {
            int[] pos = queue.poll();

            for (int i = pos[0] + 1; i < n; i++) {
                int energy = pos[2] + (i - pos[0]) * (i - pos[0]);
                if (energy > answer) continue;
                if (pos[1] == 0 && s.charAt(i) == 'O') {
                    move(queue, i, 1, energy);
                } else if (pos[1] == 1 && s.charAt(i) == 'J') {
                    move(queue, i, 2, energy);
                } else if (pos[1] == 2 && s.charAt(i) == 'B') {
                    move(queue, i, 0, energy);
                }
            }
        }

        System.out.println(answer == Integer.MAX_VALUE ? -1 : answer);
    }

    private static void move(Queue<int[]> queue, int pos, int status, int energy) {
        if (pos == n - 1) {
            answer = Math.min(answer, energy);
        } else {
            queue.add(new int[]{pos, status, energy});
        }
    }
}

내 풀이의 시간복잡도

O(N^2)

참고자료

고민사항, 질문

리뷰받고 싶은 부분

@ikjo93 ikjo93 self-assigned this Dec 4, 2022
@ikjo93 ikjo93 linked an issue Dec 4, 2022 that may be closed by this pull request
@ikjo93 ikjo93 changed the title [백준] 20221204 "백준 - BOJ 거리" 풀이 제출 [익조] 20221204 "백준 - BOJ 거리" 풀이 제출 Dec 4, 2022
@ikjo93 ikjo93 merged commit 86a2525 into main Dec 22, 2022
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.

[2022.12.04] 백준 - BOJ 거리

2 participants