๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] Contact SWEA #1238

by seolhee2750 2022. 7. 12.
๋ฌธ์ œ

https://swexpertacademy.com/main/code/problem/problemDetail.do

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
from collections import deque

for t in range(10):
    l, s = map(int, input().split())
    lst = list(map(int, input().split()))
    maximum = max(lst)
    memory = [set() for _ in range(maximum)]

    for i in range(0, l, 2):
        memory[lst[i]-1].add(lst[i+1])
    for i in range(len(memory)):
        memory[i] = list(memory[i])

    visited = [0 for _ in range(maximum)]
    queue = deque()

    if len(memory[s-1]) == 0:
        print(s)
    else:
        for i in memory[s-1]:
            queue.append((i, 1))
        while queue:
            (n, cnt) = queue.popleft()

            if visited[n-1] > 0:
                continue

            visited[n-1] = cnt

            if len(memory[n-1]) == 0:
                continue

            for i in memory[n-1]:
                queue.append((i, cnt+1))

        maxResult = max(visited)
        answer = 0
        for i in range(maximum):
            if visited[i] == maxResult:
                answer = max(answer, i+1)
        print("#" + str(t+1) + " " + str(answer))

๐Ÿ‘‰ BFS ๋ฌธ์ œ

  • lst ๋ฆฌ์ŠคํŠธ์— ์ฃผ์–ด์ง€๋Š” ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›๊ณ , ๊ทธ ์ค‘ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•ด์„œ ๊ทธ ๋ฒ”์œ„๋งŒํผ memory ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ–ˆ๋‹ค.
  • memory์—๋Š” ํ•œ ์‚ฌ๋žŒ์ด ์–ด๋–ค ์ˆซ์ž์˜ ์‚ฌ๋žŒ์—๊ฒŒ ์—ฐ๋ฝ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง„๋‹ค.
    ex) 1๋ฒˆ ์‚ฌ๋žŒ์ด 7, 8, 17๋ฒˆ์—๊ฒŒ ์—ฐ๋ฝ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•˜๋ฉด memory[0] = {7, 8, 17}
    ๊ฐ™์€ ์—ฐ๋ฝ๋ง์ด ์—ฌ๋Ÿฌ ๋ฒˆ ์ž…๋ ฅ๋  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ–ˆ์œผ๋ฏ€๋กœ, ์ค‘๋ณต ์ œ๊ฑฐ๋ฅผ ์œ„ํ•ด set ํ˜•ํƒœ์˜ ์›์†Œ๋กœ ๊ตฌ์„ฑํ–ˆ๋‹ค.
    ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  ์ •๋ณด ์ž…๋ ฅ์ด ๋๋‚˜๋ฉด ๋‹ค์‹œ ๊ทธ set๋“ค์„ ๋ฆฌ์ŠคํŠธ๋กœ ํ˜•๋ณ€ํ™˜ํ•ด์ฃผ์—ˆ๋‹ค.
  • visited ๋ฆฌ์ŠคํŠธ๋Š” bool์ด ์•„๋‹Œ intํ˜•์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋ฐฉ๋ฌธํ•œ ๊ณณ์„ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ๋‹ค.
  • queue์—๋Š” ์‹œ์ž‘์ ์—์„œ ์ด์–ด์ง€๋Š” ์ ๊ณผ, ๋ช‡ ๋ฒˆ์งธ๋กœ ๋ฐฉ๋ฌธ์ธ์ง€ ํ‘œํ˜„ํ•˜๋Š” ๊ฐ’์„ ํŠœํ”Œ ์Œ์œผ๋กœ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.
    ๊ทธ๋ฆฌ๊ณ  while ๋ฐ˜๋ณต์„ ํ•˜๋ฉฐ queue์˜ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ ์›์†Œ ๊ฐ’์„ n, cnt์— ํ• ๋‹นํ•ด์„œ ์‚ฌ์šฉํ–ˆ๋‹ค.
  • visited[n-1]์ด 0๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์ด๋ฏธ ์—ฐ๋ฝ์„ ๋ฐ›์€ ๊ณณ์ด๋ฏ€๋กœ ๋‹ค์‹œ ์—ฐ๋ฝํ•  ์ˆ˜ ์—†๊ณ , continue ํ–ˆ๋‹ค.
    ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” visited[n-1]์— cnt๋ฅผ ์ €์žฅํ•ด์„œ ๋ช‡ ๋ฒˆ์งธ๋กœ ๋ฐฉ๋ฌธํ•˜๋Š” ์ ์ธ์ง€๋ฅผ ํ‘œ์‹œํ–ˆ๋‹ค.
  • memory[n-1]์— ์ €์žฅ๋œ ๊ฐ’์ด ์—†๋‹ค๋ฉด ํ•ด๋‹น ์ ์€ ์—ฐ๋ฝํ•  ์ˆ˜ ์žˆ๋Š” ๊ณณ์ด ์—†๋Š” ๊ฒƒ์ด๋ฏ€๋กœ continue ํ–ˆ๋‹ค.
    ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” memory[n-1]์— ์ €์žฅ๋œ ์ ๋“ค์„ ๋ชจ๋‘ queue์— ์ถ”๊ฐ€ํ–ˆ๋‹ค.
  • queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ์œ„์˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๊ณ , ๋ฐ˜๋ณต์ด ๋๋‚˜๋ฉด
    visited์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ฐ€์ง„ ์›์†Œ ์ค‘ ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ์ฐพ์•„์„œ ๊ฒฐ๊ณผ๋กœ ์ถœ๋ ฅํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์‹œ์ž‘์ ์„ ๊ธฐ์ค€์œผ๋กœ ๊ณ„์† ํƒ์ƒ‰ํ•  ๊ณณ๋“ค์„ ์ ์ ˆํžˆ ์ฐพ์•„์ฃผ๋ฉด ๋˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€๋‹ค.

๋Œ“๊ธ€