[BOJ] #1697 ์ˆจ๋ฐ”๊ผญ์งˆ (Python, BFS, ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ ๋ฌธ์ œ ํ•ด๊ฒฐ)

    ๋ฐ˜์‘ํ˜•

    ๐Ÿ’ก ๋ฌธ์ œ

     

    1697๋ฒˆ: ์ˆจ๋ฐ”๊ผญ์งˆ

    ์ˆ˜๋นˆ์ด๋Š” ๋™์ƒ๊ณผ ์ˆจ๋ฐ”๊ผญ์งˆ์„ ํ•˜๊ณ  ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ํ˜„์žฌ ์  N(0 ≤ N ≤ 100,000)์— ์žˆ๊ณ , ๋™์ƒ์€ ์  K(0 ≤ K ≤ 100,000)์— ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ๊ฑท๊ฑฐ๋‚˜ ์ˆœ๊ฐ„์ด๋™์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ, ์ˆ˜๋นˆ์ด์˜ ์œ„์น˜๊ฐ€ X์ผ

    www.acmicpc.net

     

    ๐Ÿ’ก ํ’€์ด

    ์ฒ˜์Œ์—๋Š” ๋‹จ์ˆœํžˆ ๊ฐ„๋‹จํ•œ BFS ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋”๋‹ˆ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” n๊ณผ k๊ฐ€ 100,000๊นŒ์ง€ ๋  ์ˆ˜ ์žˆ๋Š”๋ฐ, while๋ฌธ์„ ๋Œ๋ฉด์„œ queue๊ฐ€ ๊ณ„์† 3๋ฐฐ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    # boj 1697 ์ˆจ๋ฐ”๊ผญ์งˆ
    
    import sys
    from collections import deque
    
    input = sys.stdin.readline
    
    n, k = map(int, input().split())
    answer = 0
    
    queue = deque([])
    queue.append([n, 0])
    while queue:
        i, cnt = queue.popleft()
        if i == k:
            answer = cnt
            break
        queue.append([i - 1, cnt + 1])
        queue.append([i + 1, cnt + 1])
        queue.append([i * 2, cnt + 1])
    
    print(answer)

    ์ด๋ ‡๊ฒŒ ๋‹จ์ˆœํžˆ ํ์— -1ํ•œ ๊ฐ’, +1ํ•œ ๊ฐ’, x2ํ•œ ๊ฐ’์„ ๋ฌด์กฐ๊ฑด ๋„ฃ์–ด์ฃผ๊ฒŒ ๋˜๋ฉด, ์ค‘๋ณตํ•ด์„œ queue์— ๋„ฃ์–ด์ง€๊ฒŒ ๋˜๋Š” ๊ฐ’์ด ๋งค์šฐ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ํ•œ๋ฒˆ ๊ฒ€์‚ฌํ–ˆ๋˜ ๊ฒƒ๋“ค์„ ๊ณ„์† ๊ฒ€์‚ฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ queue์— ๋„ฃ๊ธฐ ์ „์— ์กฐ๊ฑด๋ฌธ์„ ํ†ตํ•ด ๋ฐฉ๋ฌธํ–ˆ์—ˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. 

    ์ฒ˜์Œ์— ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ–ˆ์—ˆ๋Š”๋ฐ, n๊ณผ k์˜ ๋ฒ”์œ„๊ฐ€ 10๋งŒ๊นŒ์ง€์—ฌ์„œ ๋„ˆ๋ฌด ํฌ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ 1byte์งœ๋ฆฌ boolean ๋ณ€์ˆ˜๊ฐ€ 100,000๊ฐœ์ด๋ฉด 100,000 byte = 100 KB ์ •๋„๋ฐ–์— ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ฌธ์ œ์—์„œ ์ค€ 124MB ๋ฉ”๋ชจ๋ฆฌ์—์„œ๋Š” ์ถฉ๋ถ„ํžˆ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

    ๋”ฐ์‚ฌ๋Ÿฌ queue์— ๋„ฃ์œผ๋ ค๋Š” ๊ฐ’์ด 0๊ณผ 100,000 ์‚ฌ์ด์˜ ๊ฐ’์ธ์ง€ ํ™•์ธํ•˜๊ณ , ํ•ด๋‹น ๊ฐ’์„ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋‹ค๋ฉด queue์— ๋„ฃ์–ด์ฃผ๋„๋ก ์กฐ๊ฑด๋ฌธ์„ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

     

    ๐Ÿ’ก ์ •๋‹ต ์ฝ”๋“œ

    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    # boj 1697 ์ˆจ๋ฐ”๊ผญ์งˆ
    
    import sys
    from collections import deque
    
    input = sys.stdin.readline
    
    n, k = map(int, input().split())
    answer = 0
    visited = [False] * 1000001
    
    queue = deque([])
    queue.append([n, 0])
    while queue:
        i, cnt = queue.popleft()
        if i == k:
            answer = cnt
            break
        if 0 <= i - 1 <= 1000000 and not visited[i - 1]:
            visited[i - 1] = True
            queue.append([i - 1, cnt + 1])
        if 0 <= i + 1 <= 1000000 and not visited[i + 1]:
            visited[i + 1] = True
            queue.append([i + 1, cnt + 1])
        if 0 <= i * 2 <= 100000 and not visited[i * 2]:
            visited[i * 2] = True
            queue.append([i * 2, cnt + 1])
    
    print(answer)

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€