๐Ÿงฉ Problem Solving/[๋ฐฑ์ค€]

[๋ฐฑ์ค€] 16928 ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ ๊ฒŒ์ž„ (python ํŒŒ์ด์ฌ)

์ œ๋ด‰์•„ 2022. 7. 3. 01:54

https://www.acmicpc.net/problem/16928

 

16928๋ฒˆ: ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ ๊ฒŒ์ž„

์ฒซ์งธ ์ค„์— ๊ฒŒ์ž„ํŒ์— ์žˆ๋Š” ์‚ฌ๋‹ค๋ฆฌ์˜ ์ˆ˜ N(1 ≤ N ≤ 15)๊ณผ ๋ฑ€์˜ ์ˆ˜ M(1 ≤ M ≤ 15)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์‚ฌ๋‹ค๋ฆฌ์˜ ์ •๋ณด๋ฅผ ์˜๋ฏธํ•˜๋Š” x, y (x < y)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. x๋ฒˆ ์นธ์— ๋„์ฐฉํ•˜๋ฉด, y๋ฒˆ ์นธ์œผ

www.acmicpc.net


์•„์ด๋””์–ด

1. bfs

  • ์ฃผ์‚ฌ์œ„๋ฅผ ์„ ํƒ(1 ~ 6)ํ•  ์ˆ˜ ์žˆ๋Š” ์ ๊ณผ ์ตœ๋‹จ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•ดํ•œ๋‹ค๋Š” ์ ์— bfs๋ฅผ ์„ ํƒ
  • ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ๋งŒ ์ž˜ ์ฒดํฌํ•ด์ฃผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ.

์ฝ”๋“œ ์„ค๋ช…

    deq = deque()
    deq.append([1, 0])
    visited[1] = True

    while deq:
        po, count = deq.popleft()

        if po == 100:
            print(count)
            break

        for i in range(1,7):
            newpo = po + i
            if newpo > 100:
                continue

            for k in range(len(sadari)):
                if newpo == sadari[k][0] and not visited[newpo]:
                    visited[newpo] = True
                    deq.append([sadari[k][1], count + 1])

            for j in range(len(baaam)):
                if newpo == baaam[j][0] and not visited[newpo]:
                    visited[newpo] = True
                    deq.append([baaam[j][1], count + 1])

            if not visited[newpo]:
                visited[newpo] = True
                deq.append([newpo, count + 1])

bfs๋กœ ๋ฏธ๋กœ ์ฐพ๊ธฐ ํ•  ๋•Œ์ฒ˜๋Ÿผ ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰ ๋Œ€์‹  ์ฃผ์‚ฌ์œ„ 1 ~ 6์„ ํ™•์ธํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๋จผ์ € 100์„ ๋„˜์–ด๊ฐ€๋ฉด ์•ˆ ๋˜๋ฏ€๋กœ 100์„ ๋„˜๊ธฐ๋ฉด continue๋กœ ๋„˜๊ฒจ์ค€๋‹ค. ์ดํ›„ ๊ทธ ์นธ์— ๋ฑ€์ด๋‚˜ ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์ค€๋‹ค.

์—†๋‹ค๋ฉด ๊ทธ๋ƒฅ ์ด๋™ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

ํ์—๋Š”์ด๋™ํ•œ ์œ„์น˜์™€ ํšŸ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

์ตœ์ข…์ ์œผ๋กœ ์œ„์น˜๊ฐ€ 100์ด ๋œ๋‹ค๋ฉด ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ด์ค€๋‹ค.


์ „์ฒด ์ฝ”๋“œ

import sys
from collections import deque

def bfs():
    deq = deque()
    deq.append([1, 0])
    visited[1] = True

    while deq:
        po, count = deq.popleft()

        if po == 100:
            print(count)
            break

        for i in range(1,7):
            newpo = po + i
            if newpo > 100:
                continue
            
            for k in range(len(sadari)):
                if newpo == sadari[k][0] and not visited[newpo]:
                    visited[newpo] = True
                    deq.append([sadari[k][1], count + 1])

            for j in range(len(baaam)):
                if newpo == baaam[j][0] and not visited[newpo]:
                    visited[newpo] = True
                    deq.append([baaam[j][1], count + 1])

            if not visited[newpo]:
                visited[newpo] = True
                deq.append([newpo, count + 1])

if __name__ == '__main__':
    N, M = map(int, input().split())
    visited = [False for _ in range(101)]
    sadari = []
    baaam = []

    for i in range(N):
        sadari.append([int(x) for x in sys.stdin.readline().rstrip().split()])
    for i in range(M):
        baaam.append([int(x) for x in sys.stdin.readline().rstrip().split()])

    bfs()