3. BFS & DFS

๐Ÿ’ก ๋™๋นˆ๋‚˜ ๋‹˜์˜ ์ด์ฝ”ํ…Œ 2021 ๊ฐ•์˜ ๋ชฐ์•„๋ณด๊ธฐ ๋ฅผ ๋ณด๋ฉด์„œ ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ด๊ฒƒ์ด ์ทจ์—…์„ ์œ„ํ•œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค with ํŒŒ์ด์ฌ ์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š” ๐Ÿ˜Š ํ•™์Šต ๋„๊ตฌ๋กœ๋Š” ๋ฆฌํ”Œ๋ › ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ณ  ์›๋ณธ ์†Œ์Šค์ฝ”๋“œ๋Š” ๋™๋นˆ๋‹˜์˜ Github ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ  ์Šค์Šค๋กœ ๊ณต๋ถ€ํ•œ ์†Œ์Šค์ฝ”๋“œ๋Š” Github ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

BFS & DFS

  • ๋Œ€ํ‘œ์ ์ธ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ํƒ์ƒ‰์ด๋ž€ ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ๋Š” ๊ณผ์ •!

  • DFS/BFS ๋Š” ์ฝ”ํ…Œ์—์„œ ๋งค์šฐ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ์œ ํ˜•์ž„!

์‹œ์ž‘ํ•˜๊ธฐ ์ „์—

์Šคํƒ

  • ๋จผ์ € ๋“ค์–ด์˜จ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚˜์ค‘์— ๋‚˜๊ฐ€๋Š” ํ˜•์‹์˜ ์ž๋ฃŒ๊ตฌ์กฐ

  • ์„ ์ž…ํ›„์ถœ

  • ์ž…๊ตฌ์™€ ์ถœ๊ตฌ๊ฐ€ ๋™์ผํ•œ ํ˜•ํƒœ๋กœ ์Šคํƒ์„ ์‹œ๊ฐํ™”ํ•จ

    • ์˜ˆ) ๋ฐ•์Šค์Œ“๊ธฐ

  • ๋‹ค์–‘ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ์‚ฌ์šฉ๋จ

  • ์—ฐ์‚ฐ

    • ๋ฆฌ์ŠคํŠธ๋ฅผ ์Šคํƒ ์ž๋ฃŒ๊ตฌ์กฐ ๊ตฌํ˜„์„ ์œ„ํ•ด ์ด์šฉํ•  ์ˆ˜ ์žˆ์Œ

    • ์‚ฝ์ž… : stack.append(n)

    • ์‚ญ์ œ : stack.pop()

    • ์ตœ์ƒ๋‹จ ์›์†Œ๋ถ€ํ„ฐ ์ถœ๋ ฅ : print(stack[::-1])

    • ์ตœํ•˜๋‹จ ์›์†Œ๋ถ€ํ„ฐ ์ถœ๋ ฅ : print(stack)

  • ์ž๋ฐ”

    • stack ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Œ

    • ์‚ฝ์ž… : push(n)

    • ์‚ญ์ œ : pop()

    • ์ตœ์ƒ๋‹จ ์›์†Œ์ถœ๋ ฅ : peek()

ํ

  • ๋จผ์ € ๋“ค์–ด์˜จ ๋ฐ์ดํ„ฐ๊ฐ€ ๋จผ์ € ๋‚˜๊ฐ€๋Š” ํ˜•์‹์˜ ์ž๋ฃŒ๊ตฌ์กฐ

  • ์„ ์ž…์„ ์ถœ

  • ์ž…๊ตฌ์™€ ์ถœ๊ตฌ๊ฐ€ ๋ชจ๋‘ ๋šซ๋ ค์žˆ๋Š” ํ„ฐ๋„๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์‹œ๊ฐํ™”

  • ์˜ˆ์‹œ) ์€ํ–‰ ๋Œ€๊ธฐ์ค„

  • ์—ฐ์‚ฐ

    • ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌํ˜„์€ ํ•  ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ์ฆ๊ฐ€ํ•œ๋‹ค.

    • ๋”ฐ๋ผ์„œ python ์—์„œ๋Š” deque() ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•จ

    • ์‚ฝ์ž… : append(n)

    • ์‚ญ์ œ : popeleft()

    • ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅ : queue

    • ์—ญ์ˆœ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ : queue.reverse()

  • java

    • queue ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ง€์›ํ•จ : linkedList

    • ์‚ฝ์ž… : offer(n)

    • ์‚ญ์ œ : poll()

    • ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅ : for loop + q.poll()

์žฌ๊ท€ํ•จ์ˆ˜

  • ์ž๊ธฐ์ž์‹ ์„ ๋‹ค์‹œ ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜

  • recursive function

  • ๋‹จ์ˆœํ•œ ํ˜•ํƒœ์˜ ์žฌ๊ท€ํ•จ์ˆ˜ ์˜ˆ์ œ

    • ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค ๋ฅผ ๋ฌดํ•œํžˆ ์ถœ๋ ฅ

    • ์–ด๋Š์ •๋„ ์ถœ๋ ฅํ•˜๋‹ค๊ฐ€ ์ตœ๋Œ€ ์žฌ๊ท€ ๊นŠ์ด ์ดˆ๊ณผ ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋จ

def recursive_func():
  print("์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!")
  recursive_func()

recursive_func()

# ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!
# ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!
# ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!
# ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!
# ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!
# ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!
# ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!
# ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!
# ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!
# Traceback (most recent call last):
#   File "main.py", line 8, in <module>
#     recursive_func()
#   File "main.py", line 6, in recursive_func
#     recursive_func()
#   File "main.py", line 6, in recursive_func
#     recursive_func()
#   File "main.py", line 6, in recursive_func
#     recursive_func()
#   [Previous line repeated 992 more times]
#   File "main.py", line 5, in recursive_func
#     print("์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!")
# RecursionError: maximum recursion depth exceeded while calling a Python object
# exit status 1
  • ์ผ๋ฐ˜์ ์œผ๋กœ ์žฌ๊ท€ํ•จ์ˆ˜๋Š” ํŠน์ • ์กฐ๊ฑด์—์„œ ์ข…๋ฃŒ๋  ์ˆ˜ ์žˆ๋„๋ก ์ข…๋ฃŒ์กฐ๊ฑด์„ ๋ช…์‹œํ•ด์•ผํ•œ๋‹ค.

def recursive_func(i):
  if i == 100:
    return
  
  print(i, "๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜์—์„œ", i+1, " ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!")
  recursive_func(i+1)

  print(i, "๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค!")


recursive_func(1)


# 95 ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜์—์„œ 96  ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!
# 96 ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜์—์„œ 97  ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!
# 97 ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜์—์„œ 98  ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!
# 98 ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜์—์„œ 99  ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!
# 99 ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜์—์„œ 100  ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค!
# 99 ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค!
# 98 ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค!
# 97 ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค!
# 96 ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค!
# 95 ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค!
# 94 ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค!
# 93 ๋ฒˆ์งธ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค!

ํŒฉํ† ๋ฆฌ์–ผ ๊ตฌํ˜„๋ฌธ์ œ

  • n! = 1 * 2 * 3 ... * n

  • ์ˆ˜ํ•™์ ์œผ๋กœ 0! ๊ณผ 1! ์˜ ๊ฐ’์€ 1์ด๋‹ค.

  • ์ด ๋ฌธ์ œ๋Š” 1) ๋ฐ˜๋ณต์ ์œผ๋ก ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ 2) ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•, 2๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

# BFS & DFS
# ์žฌ๊ท€ํ•จ์ˆ˜
# ํŒฉํ† ๋ฆฌ์–ผ ๊ตฌํ˜„์˜ˆ์ œ 
# n! = 1 * 2 * 3  ... * n

## ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๊ตฌํ˜„
def loop(i):
  result = 1

  for i in range(1, i+1):
    result *= i
  return result

print(loop(5))  # 120


## ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„
def self_func(i):
  if i <= 1:
    return 1

  return i * self_func(i-1)

print(self_func(5))  #120

์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ ๊ณ„์‚ฐ (์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•) ์˜ˆ์ œ

  • ๋‘ ๊ฐœ์˜ ์ž์—ฐ์ˆ˜์— ๋Œ€ํ•ด์„œ ์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•์ด ์žˆ๋‹ค.

  • ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•

    • ๋‘ ์ž์—ฐ์ˆ˜ A, B ์— ๋Œ€ํ•˜์—ฌ A๋ฅผ B๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ R์ด๋ผ๊ณ  ํ• ๋•Œ

    • A, B์˜ ์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜๋Š” B์™€ R์˜ ์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜์™€ ๊ฐ™๋‹ค.

  • ์˜ˆ์‹œ) GCD(192, 162)

    • A / B

    • 192 / 162

    • 162 / 30

    • 30 / 12

    • 12 / 6

    • 6

# ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•์œผ๋กœ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ ๊ตฌํ•˜๊ธฐ
def gcd(a, b):
  if a % b == 0:
    return b
  else:
    return gcd(b, a % b)

print(gcd(192, 162))
# 6

์žฌ๊ท€ํ•จ์ˆ˜ ์‚ฌ์šฉ์‹œ ์œ ์˜์‚ฌํ•ญ

  • ์ž˜ ํ™œ์šฉํ•˜๋ฉด ๋ณต์žกํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

    • ํ•˜์ง€๋งŒ ๋•Œ๋–„๋กœ ์˜คํžˆ๋ ค ๋‹ค๋ฅธ์‚ฌ๋žŒ์ด ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์šด ์ฝ”๋“œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์‹ ์ค‘ํ•˜๊ฒŒ ์‚ฌ์šฉํ•ด์•ผํ•จ

  • ๋ชจ๋“  ์žฌ๊ท€ํ•จ์ˆ˜๋Š” ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅ -> ๊ทธ ๋ฐ˜๋Œ€๋„ ๊ฐ€๋Šฅ

  • ์žฌ๊ท€ ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜๋ณต๋ฌธ๋ณด๋‹ค ์œ ๋ฆฌํ•œ ๊ฒฝ์šฐ๋„ ์žˆ๊ณ , ๋ถˆ๋ฆฌํ•œ ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค.

  • ์ปดํ“จํ„ฐ๊ฐ€ ํ•จ์ˆ˜๋ฅผ ์—ฐ์†์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๋ฉด ์ปดํ“จํ„ฐ ๋ฉ”๋ชจ๋ฆฌ ๋‚ด๋ถ€์˜ ์Šคํƒ์— ํ”„๋ ˆ์ž„์ด ์Œ“์ธ๋‹ค.

    • ์Šคํƒ ์‚ฌ์šฉํ•ด์•ผํ•  ๋•Œ, ๊ตฌํ˜„์ƒ ์Šคํƒ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋Œ€์‹ ์— ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Œ

  • ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰

  • ๊นŠ์€ ๋ถ€๋ถ„์„ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ์Šคํƒ ์ž๋ฃŒ๊ตฌ์กฐ ํ˜น์€ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ๋‹ค.

  • ๊ตฌ์ฒด์ ์ธ ๋™์ž‘๊ณผ์ •

    1. ํƒ์ƒ‰ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ์Šคํƒ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ

    2. ์Šคํƒ์˜ ์ตœ์ƒ๋‹จ ๋…ธ๋“œ์— ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์ž‡์œผ๋ฉด ๊ทธ ๋…ธ๋“œ๋ฅผ ์Šคํƒ์— ๋„ฃ๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ

    3. ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ์ ‘ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด ์Šคํƒ์—์„œ ์ตœ์ƒ๋‹จ ๋…ธ๋“œ๋ฅผ ๊บผ๋ƒ„

    4. ๋”์ด์ƒ 2๋ฒˆ ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์„๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

# DFS - Deep first search

def dfs(graph, i, visited):
  visited[i] = True
  print(i, end=' ')

  for j in graph[i]:
    if not visited[j]:
      dfs(graph, j, visited)

graph = [
  [],
  [2, 3, 8],
  [1, 7],
  [1, 4, 5],
  [3, 5],
  [3, 4],
  [7],
  [2, 6, 8],
  [1, 7]
]

visited = [False] * 9

print(dfs(graph, 1, visited))
  • ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰

  • ๊ทธ๋ž˜ํ”„์—์„œ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ถ€ํ„ฐ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ํ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•˜๋ฉฐ ๋™์ž‘ ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Œ

    1. ํƒ์ƒ‰ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ํ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.

    2. ํ์—์„œ ๋…ธ๋“œ๋ฅผ ๊บผ๋‚ธ ๋’ค์— ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ธ์ ‘๋…ธ๋“œ ์ค‘์—์„œ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.

    3. ๋” ์ด์ƒ 2๋ฒˆ์˜ ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

  • ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค.

  • ๋™์ž‘์˜ˆ์‹œ

 # BFS - Breadth First Search
from collections import deque

def bfs(graph, i, visited):
  visited[i] = True
  queue = deque([i])

# queue ๊ฐ€ ๋นŒ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
  while queue:
    v = queue.popleft()
    print(v, end=' ')

    for j in graph[v]:
      if not visited[j]:
        queue.append(j)
        visited[j] = True

graph = [
  [],
  [2, 3, 8],
  [1, 7],
  [1, 4, 5],
  [3, 5],
  [3, 4],
  [7],
  [2, 6, 8],
  [1, 7]
]

visited = [False] * 9

print(bfs(graph, 1, visited))
# 1, 2, 3, 8, 7, 4, 5, 6
# 1 2 3 8 7 4 5 6

์Œ๋ฃŒ์ˆ˜ ์–ผ๋ ค๋จน๊ธฐ

๋ฌธ์ œ

  • N * M ํฌ๊ธฐ์˜ ์–ผ์Œํ‹€์ด ์žˆ๋‹ค.

  • ๊ตฌ๋ฉ์ด ๋šซ๋ ค์žˆ๋Š” ๋ถ€๋ถ„์€ 0, ์นธ๋ง‰์ด๊ฐ€ ์กด์žฌํ•˜๋Š” ๋ถ€๋ถ„์€ 1๋กœ ํ‘œ์‹œ๋œ๋‹ค.

  • ๊ตฌ๋ฉ์ด ๋šซ๋ ค์žˆ๋Š” ๋ถ€๋ถ„๋ผ๋ฆฌ ์ƒ, ํ•˜, ์ขŒ, ์šฐ๋กœ ๋ถ™์–ด์žˆ๋Š” ๊ฒฝ์šฐ ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.

  • ์ด๋•Œ ์–ผ์Œํ‹€์˜ ๋ชจ์–‘์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด ์•„์ด์Šคํฌ๋ฆผ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด๋ผ

  • ์˜ˆ) 4 * 5 ์˜ ์–ผ์Œํ‹€ -> ์•„์ด์Šคํฌ๋ฆผ์ด ์ด 3๊ฐœ

  • ์—ฐ๊ฒฐ์š”์†Œ์ฐพ๊ธฐ ๋ฌธ์ œ

  • ๋ฌธ์ œ์กฐ๊ฑด

    • ์„ธ๋กœ๊ธธ์ด, ๊ฐ€๋กœ๊ธธ์ด๊ฐ€ ์ฃผ์–ด์ง

    • ์–ผ์Œํ‹€์˜ ํ˜•ํƒœ๊ฐ€ ์ฃผ์–ด์ง

      • ๊ตฌ๋ฉ ๋ถ€๋ถ„ : 0

      • ๋ง‰ํžŒ ๋ถ€๋ถ„ : 1

  • ์ž…๋ ฅ์˜ˆ์‹œ

    • 4 5

    • 00110

    • 00011

    • 11111

    • 00000

  • ์ถœ๋ ฅ์˜ˆ์‹œ

    • 3

ํ•ด๊ฒฐ

  • ํ•ด๊ฒฐ์•„์ด๋””์–ด

    • 0 ๋ถ€๋ถ„๋ผ๋ฆฌ ์—ฐ๊ฒฐ๋œ ๋ฉ์–ด๋ฆฌ๊ฐ€ ๊ณง ์–ผ์Œ์ด ๋˜๋ฏ€๋กœ, ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฉ์–ด๋ฆฌ๋กœ ๋ณด๊ณ  visited count ๋ฅผ ์ฒดํฌํ•˜๋ฉด ๋œ๋‹ค.

    • DFS ๋กœ ํ’€์–ด๋ณด๊ธฐ TIP

      1. ํŠน์ •ํ•œ ์ง€์ ์˜ ์ฃผ๋ณ€ ์ƒ, ํ•˜, ์ขŒ, ์šฐ๋ฅผ ์‚ดํŽด๋ณธ ๋’ค์— ์ฃผ๋ณ€ ์ง€์  ์ค‘์—์„œ ๊ฐ’์ด '0' ์ด๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ง€์ ์ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์ง€์ ์„ ๋ฐฉ๋ฌธํ•œ๋‹ค.

      2. ๋ฐฉ๋ฌธํ•œ ์ง€์ ์—์„œ ๋‹ค์‹œ ์ƒ, ํ•˜, ์ขŒ, ์šฐ๋ฅผ ์‚ดํ”ผ๋ฉด์„œ ๋ฐฉ๋ฌธ์„ ์ง„ํ–‰ํ•œ๋‹ค.

        1. ์ด๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉด ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์ง€์ ์„ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ์Œ

      3. ๋ชจ๋“  ๋…ธ๋“œ์— ๋Œ€ํ•ด์„œ 1-2๋ฒˆ์˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ง€์ ์˜ ์ˆ˜๋ฅผ ์นด์šดํŠธ ํ•œ๋‹ค.

# DFS & BFS ๋ฌธ์ œ
# ์Œ๋ฃŒ์ˆ˜ ์–ผ๋ ค๋จน๊ธฐ

# n, m ์ž…๋ ฅ๋ฐ›๊ธฐ
n, m = map(int, input().split())

# ํ˜„์žฌ ์–ผ์Œํ‹€ ์ƒํƒœ ์ž…๋ ฅ๋ฐ›๊ธฐ
ice_frames = []

for i in range(n):
  ice_frames.append(list(map(int, input())))

print(ice_frames)

# ์ด ๋ฉ์–ด๋ฆฌ ๊ฐœ์ˆ˜ : result
result = 0

# dfs func ์ •์˜
# - ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ๋ฐฉ๋ฌธ๋˜๋ฉด return True
# - ํ˜„์žฌ์œ„์น˜๊ฐ€ 0๋ฉด 1๋กœ ์ฑ„์šฐ๊ธฐ
# - ํ•ด๋‹น ์œ„์น˜๋ถ€ํ„ฐ ์ƒํ•˜์ขŒ์šฐ ๋ชจ๋‘ ๋ฐฉ๋ฌธ ์žฌ๊ท€ํ˜ธ์ถœ -> ๋ฐฉ๋ฌธ ๊ฐ€๋Šฅํ•˜๋ฉด 1๋กœ ์ฑ„์šฐ๋Š” ๊ณผ์ • ์žฌ๊ท€์ ์œผ๋กœ ๋ฐ˜๋ณตํ• ๊ฒƒ 

def dfs(x, y):
  if x <= -1 or x >= n or y <= -1 or y >= m:
    return False

  if ice_frames[x][y] == 0:
    ice_frames[x][y] = 1

    dfs(x-1, y)
    dfs(x+1, y)
    dfs(x, y-1)
    dfs(x, y+1)

    return True
  return False

# n, m for loop -> dfs ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๊ธฐ 
# - visited == True -> result +=1
# print(result)
for i in range(n):
  for j in range(m):
    if dfs(i, j) == True:
      result += 1

print(result)

๋ฏธ๋กœํƒˆ์ถœ๋ฌธ์ œ

๋ฌธ์ œ

  • N * M ํฌ๊ธฐ์˜ ์ง์‚ฌ๊ฐํ˜• ํ˜•ํƒœ์˜ ๋ฏธ๋กœ๊ฐ€ ์žˆ๋‹ค.

  • ์—ฌ๋Ÿฌ๋งˆ๋ฆฌ์˜ ๊ดด๋ฌผ์ด ์ด์–ด์„œ ์ด๋ฅผ ํ”ผํ•ด์„œ ํƒˆ์ถœํ•ด์•ผํ•œ๋‹ค.

  • ์‚ฌ์šฉ์ž์˜ ์œ„์น˜๋Š” ํ˜„์žฌ (1, 1)์— ์žˆ๊ณ  ๋ฏธ๋กœ์˜ ํƒˆ์ถœ๊ตฌ๋Š” (N, M)์— ์žˆ๋‹ค.

  • ํ•œ๋ฒˆ์— ํ•œ์นธ์”ฉ๋งŒ ์ด๋™์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

  • ๊ดด๋ฌผ์žˆ์œผ๋ฉด 0, ๊ดด๋ฌผ ์—†์œผ๋ฉด 1๋กœ ํ‘œ์‹œ๋˜์–ด์žˆ๋‹ค.

  • ๋ฏธ๋กœ๋Š” ๋ฐ˜๋“œ์‹œ ํƒˆ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ๋˜์–ด์žˆ๋‹ค.

  • ํƒˆ์ถœ์„ ์œ„ํ•ด ์›€์ง์—ฌ์•ผํ•˜๋Š” ์ตœ์†Œ ์นธ์˜ ๊ฐœ์ˆ˜๋Š” ๋ฌด์—‡์ผ๊นŒ?

  • 4<= N, M<=200

  • 0๊ณผ 1๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฏธ๋กœ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง

  • ์‹œ์ž‘๊ณผ ๋งˆ์ง€๋ง‰์€ ํ•ญ์ƒ 1

  • ์ž…๋ ฅ์˜ˆ์‹œ

5 6
101010
111111
000001
111111
111111
  • ์ถœ๋ ฅ์˜ˆ์‹œ

    • 10

ํ•ด๊ฒฐ

  • BFS ๋กœ ํ•ด๊ฒฐํ•จ

  • ์ฒ˜์Œ 1,1 ์—์„œ ์‹œ์ž‘

  • ๋งค๋ฒˆ ์ƒˆ๋กœ์šด ๋…ธ๋“œ์— ๋„๋‹ฌํ•  ๋•Œ ํ•ด๋‹น ๋…ธ๋“œ์˜ ๊ฐ’์„ +1 ๋กœ ๋ฐ”๊พธ์–ด์ค€๋‹ค.

  • ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์˜ ๊ฐ’์ด 1์ผ๋•Œ๋งŒ move count ๋กœ +1 ํ•œ๋‹ค.

  • ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์— ๋„๋‹ฌํ•  ๋•Œ๋Š” ์›€์ง์ธ ํšŸ์ˆ˜๋ฅผ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

# ๋ฏธ๋กœ์ฐพ๊ธฐ ๋ฌธ์ œ 
# ์ž…๋ ฅ๊ฐ’: ๋ฏธ๋กœ์˜ ํฌ๊ธฐ (n, m), ๋ฏธ๋กœ์ •๋ณด
# ๋ฐ˜ํ™˜๊ฐ’ : ์ตœ๋‹จ๊ฑฐ๋ฆฌ
# ์•„์ด๋””์–ด
  # BFS ๋ฅผ ์ด์šฉ
  # ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์˜ ๊ฐ’์„ +1 ๋ณ€๊ฒฝ
  # ๋…ธ๋“œ๊ฐ’์ด 1์ธ ๊ฒฝ์šฐ์—๋งŒ move cnt ๋กœ ์ฒดํฌํ•œ๋‹ค.

from collections import deque

n, m = map(int, input().split())
maze = []

for i in range(n):
  maze.append(list(map(int, input())))

dx = [+1, -1, 0, 0]
dy = [0, 0, -1, +1]

def bfs(x, y):
  queue = deque()
  queue.append((x, y))

  while queue:
    x, y = queue.popleft()

    for i in range(4):
      nx = x + dx[i]
      ny = y + dy[i]

      if nx < 0 or nx >= n or ny < 0 or ny >= m:
        continue
      
      if maze[nx][ny] == 0:
        continue
      
      if maze[nx][ny] == 1:
        maze[nx][ny] = maze[x][y] + 1
        queue.append((nx, ny))

  return maze[n-1][m-1]


print(bfs(0, 0))

Last updated