We will find a way, we always have.

-interstellar

Problem Solving/๋ฐฑ์ค€

[๋ฐฑ์ค€] 5014๋ฒˆ: ์Šคํƒ€ํŠธ๋งํฌ - ํŒŒ์ด์ฌ

Redddy 2022. 6. 1. 23:12

๐Ÿงฉ๋ฌธ์ œ ํ•ด์„

F = ์Šคํƒ€ํŠธ๋งํฌ ๊ฑด๋ฌผ ์ธต์˜ ์ด ๊ณ„์ˆ˜
S = ํ˜„์žฌ ๊ฐ•ํ˜ธ์˜ ์œ„์น˜
G = ์Šคํƒ€ํŠธ๋งํฌ์˜ ์œ„์น˜
U = ์œ„๋กœ ์˜ฌ๋ผ๊ฐ€๋Š” ์—˜๋ ˆ๋ฒ ์ดํ„ฐ ์ธต์˜ ๊ฐ„๊ฒฉ
D = ์•„๋ž˜๋กœ ๋‚ด๋ ค๊ฐ€๋Š” ์—˜๋ ˆ๋ฒ ์ดํ„ฐ ์ธต์˜ ๊ฐ„๊ฒฉ

์œ„ ์‚ฌํ•ญ๋“ค์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ํ˜„์žฌ ๊ฐ•ํ˜ธ์˜ ์œ„์น˜์—์„œ ์Šคํƒ€ํŠธ๋งํฌ์˜ ์œ„์น˜๋กœ ์—˜๋ ˆ๋ฒ ์ดํ„ฐ๋กœ ๊ฐ€๋ ค ํ•  ๋•Œ, ๋ˆ„๋ฅธ ๋ฒ„ํŠผ์˜ ์ตœ์†Œ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋งŒ์•ฝ ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋‹ค๋ฉด, ๊ณ„๋‹จ ์ด์šฉํ•˜๋ผ๋Š” ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•œ๋‹ค.

๋ชจ๋“  ์ธต์„ ๋Œ์•„๋‹ค๋‹ ํ•„์š”๊ฐ€ ์—†๊ณ  ๋ชฉํ‘œ์ธต์— ๋„๋‹ฌํ•˜๋ฉด ์—˜๋ ˆ๋ฒ ์ดํ„ฐ๋ฅผ ๋ฉˆ์ถ”๋ฉด ๋˜๋‹ˆ๊นŒ BFS๊ฐ€ ํšจ์œจ์ ์ด๋‹ค. 

 

๐Ÿ“• ํ’€์ด

BFS๋กœ ํ’€์—ˆ๋‹ค.
์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ์ธต์€ [ํ˜„์žฌ ์œ„์น˜ + U, ํ˜„์žฌ ์œ„์น˜ -D] ๋ฅผ ํ†ตํ•ด ์œ ์ถ”ํ•˜์˜€๊ณ , ์ด ๋ฐ์ดํ„ฐ๊ฐ€ F ์•ˆ์— ์žˆ๊ณ , ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋‹ค๋ฉด, ๊ทธ ์ธต์œผ๋กœ ๋ฐฉ๋ฌธํ•˜์˜€๋‹ค.

 

๐Ÿ’ป ์ฝ”๋“œ

from collections import deque
f,s,g,u,d = map(int, input().split())

# f= ์ด ์ธต์˜ ๊ณ„์ˆ˜
# s=  ๊ฐ•ํ˜ธ์˜ ์œ„์น˜์ธต
# g= ์Šคํƒ€ํŠธ๋งํฌ์˜ ์œ„์น˜
# u= ์œ„๋กœ ๊ฐˆ์ˆ˜ ์žˆ๋Š” ์ธต
# d= ์•„๋ž˜๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ธต
# ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ๋ˆ„๋ฅธ ๋ฒ„ํŠผ ์ตœ์†Œ๊ฐ’
# ๋ถˆ๊ฐ€ํ•˜๋‹ค๋ฉด use the stairs

visited = [False for i in range(f+1)] # ๋ฐฉ๋ฌธ์ฒดํฌ
def bfs():
    queue = deque()
    queue.append((s, 0)) # ์‹œ์ž‘์œ„์น˜์™€ ์นด์šดํŠธ ์ฒดํฌ
    while queue: # ํ๊ฐ€ ๋นŒ๋•Œ๊นŒ์ง€
        now, cnt = queue.popleft()
        candi = [now+u, now-d] # ์ด๋™๊ฐ€๋Šฅ ์œ„์น˜
        for i in candi:
            if 0 < i <= f: # f ์•ˆ์— ์žˆ๋‹ค๋ฉด
                if not visited[i]: # ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด
                    visited[i] = True # ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
                    queue.append((i, cnt+1)) # ํ์— ์‚ฝ์ž…
                    if i == g: # ๋งŒ์•ฝ ํ˜„์žฌ ์ด๋™ ์œ„์น˜๊ฐ€ ์Šคํƒ€ํŠธ๋งํฌ์˜ ์œ„์น˜๋ผ๋ฉด
                        return cnt+1 # ์ด๋™ํšŸ์ˆ˜ ์ถœ๋ ฅ
    return "use the stairs" # ํ๊ฐ€ ๋นŒ๋•Œ๊นŒ์ง€ ์ฐพ์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด, ๊ณ„๋‹จ ์ด์šฉํ•˜์‹œ์˜ค.
if s == g: # ์‹œ์ž‘๋ถ€ํ„ฐ ๊ฐ•ํ˜ธ์˜ ์œ„์น˜๊ฐ€ ์Šคํƒ€ํŠธ๋งํฌ์˜ ์œ„์น˜๋ผ๋ฉด ์ด๋™์•ˆํ•จ
    print(0)
else: # ์ด๋™์‹œ์ž‘ ํ•จ์ˆ˜ ํ˜ธ์ถœ
    visited[s] = True # ํ˜„์žฌ ์œ„์น˜ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
    print(bfs())

 

๐Ÿ”— ๋ฌธ์ œ๋งํฌ : ์Šคํƒ€ํŠธ๋งํฌ

 

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

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

www.acmicpc.net

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰