Post

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

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

문제 링크


1. 문제 풀이


수빈이가 동생을 찾을 수 있는 최단 시간을 찾는 문제로 BFS를 활용하면 간단하게 해결할 수 있다.


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
import java.io.*;
import java.util.*;

public class Main {

    static final int MAX = 100_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];
        visited[n] = true;

        int dist = 0;

        while (!q.isEmpty()) {
            int size = q.size();

            while (size-- > 0) {
                int node = q.poll();
                if (node == k) return dist;

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

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

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

            dist++;
        }

        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
#include <bits/stdc++.h>
using namespace std;

constexpr int MAX = 100000;
bool visited[1 + MAX];

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

    visited[n] = true;

    int dist = 0;

    while (!q.empty()) {
        int sz = q.size();

        while (sz--) {
            int node = q.front();
            q.pop();

            if (node == k) return dist;

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

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

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

        dist++;
    }

    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.