ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

5014๋ฒˆ: ์Šคํƒ€ํŠธ๋งํฌ

์ฒซ์งธ ์ค„์— F, S, G, U, D๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ S, G ≤ F ≤ 1000000, 0 ≤ U, D ≤ 1000000) ๊ฑด๋ฌผ์€ 1์ธต๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ณ , ๊ฐ€์žฅ ๋†’์€ ์ธต์€ F์ธต์ด๋‹ค.

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๋ฌธ์ œ์— F, S, G, U, D `(๊ฑด๋ฌผ์˜ ๋†’์ด, ์‹œ์ž‘์ธต, ๊ฐ€๊ณ ์ž ํ•˜๋Š” ์ธต, ํ•œ ๋ฒˆ์— ์˜ฌ๋ผ๊ฐ€๋Š” ์ธต, ํ•œ ๋ฒˆ์— ๋‚ด๋ ค๊ฐ€๋Š” ์ธต)`๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์‹œ์ž‘ ์ธต์—์„œ ์—˜๋ฆฌ๋ฒ ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด ๊ฐ€๊ณ ์ž ํ•˜๋Š” ์ธต์— ๋„๋‹ฌํ•˜๋Š” ์ตœ์†Œ ๊ฒฝ์šฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋งŒ์•ฝ ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋‹ค๋ฉด ๊ณ„๋‹จ์„ ์ด์šฉํ•˜๋ผ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

  • BFS๋ฅผ ํ†ตํ•ด ์‹œ์ž‘์ธต ๋ถ€ํ„ฐ ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒฝ์šฐ, ๋‚ด๋ ค๊ฐ€๋Š” ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
    • ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒฝ์šฐ F๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์•„์•ผ ํ•˜๋ฉฐ, ๋‚ด๋ ค๊ฐ€๋Š” ๊ฒฝ์šฐ๋Š” 0 ๋ณด๋‹ค ์ปค์•ผ ํ•œ๋‹ค.
  • ์žฌ๋ฐฉ๋ฌธ์„ ๋ง‰๊ธฐ ์œ„ํ•ด ํ˜„์žฌ ๋ฐฉ๋ฌธํ•œ ์ธต์„ `visited` ๋ฆฌ์ŠคํŠธ๋ฅผ ํ†ตํ•ด ์ฒดํฌํ•œ๋‹ค.
  • ์ตœ์†Œ๊ฐ’์„ ์นด์šดํŠธํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์€ 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.
    • q์— ๋ฐฉ๋ฌธํ•  ๋‹ค๋ฅธ ์ธต๊ณผ ์นด์šดํŠธ๋ฅผ ํ•จ๊ป˜ ์ถ”๊ฐ€ํ•˜๊ธฐ.
      • `q.append((next_floor, cnt + 1))`
    • visited๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐฉ๋ฌธ ํšŸ์ˆ˜๋ฅผ ๋ˆ„์ ํ•˜๊ธฐ
      • `visited[next_floor] = visited[cur_floor] + 1`
    • ํ˜„์žฌ ์ถ”๊ฐ€๋œ ํ๋ฅผ ํ•˜๋‚˜์˜ ์นด์šดํŠธ๋กœ ์ทจ๊ธ‰ํ•˜๊ธฐ

 

์ฝ”๋“œ

from sys import stdin
from collections import deque


def bfs(start):
    q = deque([start])

    while q:
        cur_floor, cnt = q.popleft()

        if cur_floor == g:
            return cnt

        for next_floor in cur_floor + u, cur_floor - d:
            if 0 < next_floor <= f and not visited[next_floor]:
                q.append((next_floor, cnt + 1))
                visited[next_floor] = True
    return "use the stairs"


if __name__ == '__main__':
    f, s, g, u, d = map(int, stdin.readline().split())
    visited = [False] * (f + 1)
    print(bfs([s, 0]))
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€