Post

[BaekJoon] 17071번 - 숨바꼭질 5 [Java][C++]

[BaekJoon] 17071번 - 숨바꼭질 5 [Java][C++]

문제 링크


1. 문제 풀이


수빈이가 동생을 찾는 최단 시간을 구해야 하는데 동생이 매초 움직이는 상황이다. 수빈이의 이동은 $X - 1$, $X + 1$, $2 \times X$ 총 3가지가 있는데, 이때 한 칸 옆으로 이동했다가 다시 원래 자리로 돌아가면 2초를 소모해서 원래 자리로 돌아올 수 있다는 것이 포인트다. 수빈이가 방문한 좌표들에 대해 홀수 초에 방문했는지 짝수 초에 방문했는지 기록하는 2차원 배열을 활용해서 동생을 매초 이동시키며 수빈이가 방문한 좌표에 동생이 위치할 때 동생과 수빈이 모두 홀수 초거나 짝수 초이면 이는 수빈이가 2의 배수 초만큼 좌우 이동으로 다시 해당 위치로 갈 수 있으므로 동생을 찾을 수 있다는 의미가 된다.


2. 코드


1. 풀이 [Java]

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.io.*;
import java.util.*;

public class Main {

    static final int MAX = 500_000;

    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 k = Integer.parseInt(st.nextToken());

        System.out.println(bfs(n, k));
    }

    static int bfs(int n, int k) {
        Queue<Integer> q = new ArrayDeque<>();
        q.offer(n);

        boolean[][] visited = new boolean[1 + MAX][2];
        visited[n][0] = true;

        int time = 0;

        while (!q.isEmpty()) {
            int bro = k + time * (time + 1) / 2;
            if (bro > MAX) return -1;

            if (visited[bro][time % 2]) return time;

            int nextParity = (time + 1) % 2;
            int size = q.size();
            while (size-- > 0) {
                int node = q.poll();

                int next1 = node - 1;
                if (next1 >= 0 && !visited[next1][nextParity]) {
                    q.offer(next1);
                    visited[next1][nextParity] = true;
                }

                int next2 = node + 1;
                if (next2 <= MAX && !visited[next2][nextParity]) {
                    q.offer(next2);
                    visited[next2][nextParity] = true;
                }

                int next3 = node * 2;
                if (next3 <= MAX && !visited[next3][nextParity]) {
                    q.offer(next3);
                    visited[next3][nextParity] = true;
                }
            }

            time++;
        }

        return -1;
    }
}


2. 풀이 [C++]

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;

constexpr int MAX = 500000;
bool visited[1 + MAX][2];

int bfs(int n, int k) {
    queue<int> q;
    q.push(n);

    visited[n][0] = true;

    int time = 0;

    while (!q.empty()) {
        int bro = k + time * (time + 1) / 2;
        if (bro > MAX) return -1;

        if (visited[bro][time % 2]) return time;

        int nextParity = (time + 1) % 2;
        int size = q.size();
        while (size-- > 0) {
            int node = q.front();
            q.pop();

            int next1 = node - 1;
            if (next1 >= 0 && !visited[next1][nextParity]) {
                q.push(next1);
                visited[next1][nextParity] = true;
            }

            int next2 = node + 1;
            if (next2 <= MAX && !visited[next2][nextParity]) {
                q.push(next2);
                visited[next2][nextParity] = true;
            }

            int next3 = node * 2;
            if (next3 <= MAX && !visited[next3][nextParity]) {
                q.push(next3);
                visited[next3][nextParity] = true;
            }
        }

        time++;
    }

    return -1;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, k;
    cin >> n >> k;
    cout << bfs(n, k);
}

This post is licensed under CC BY 4.0 by the author.