๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Problem Solving/BAEKJOON

[baekjoon ]python #1436 ์˜ํ™”๊ฐ๋… ์ˆŒ

by DevIseo 2022. 3. 11.
N = int(input())
end_num= 666 # ๊ฐ€์žฅ ์ž‘์€ ์ข…๋ง ์ˆซ์ž
cnt=0 # ์ข…๋ง ์ˆซ์ž์˜ ์ˆ˜

while True:
    if '666' in str(end_num): # ์ข…๋ง ์ˆซ์ž๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด
        cnt+=1 #์ˆ˜๋ฅผ ์นด์šดํŠธ
    if cnt == N: # ์šฐ๋ฆฌ๊ฐ€ ์ฐพ๋Š” N๋ฒˆ์งธ์˜ ์ข…๋ง ์ˆซ์ž == ์ข…๋ง์ˆซ์ž์˜ ์ˆ˜
        print(end_num)
        break #while๋ฌธ ๋ฉˆ์ถ”๊ธฐ
    end_num+=1 # ์ข…๋ง ์ˆซ์ž๋ฅผ ์ฐพ๊ธฐ์œ„ํ•จ

๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜

Brute(๋ฌด์‹ํ•œ) + Force(ํž˜) ๋ธŒ๋ฃจํŠธ ํฌ์Šค๋Š” ์™„์ „ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ,
๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•˜๊ณ  ์กฐ๊ฑด์— ์ถฉ์กฑ๋˜๋Š” ๊ฒฐ๊ณผ๋งŒ์„ ๊ฐ€์ ธ์˜ด!
๋ฌด์‹ํ•˜๊ฒŒ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 100% ํ™•๋ฅ ๋กœ ์ •๋‹ต์„ ์ถœ๋ ฅ.
์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•ด๊ฐ€ ์กด์žฌํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜๋Š” ๋ฒ”์œ„๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์—,
์„ค๊ณ„์‹œ 'ํ•ด๊ฐ€ ํ•˜๋‚˜ ์ด์ƒ ์กด์žฌํ•œ๋‹ค'๋Š” ๊ฐ€์ •์„ ์„ธ์šฐ๊ณ  ๋ชจ๋“  ๋ฒ”์œ„๋ฅผ ํƒ์ƒ‰! but, ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆผ!

๋ธŒ๋ฃจํŠธ ํฌ์Šค์˜ ์ข…๋ฅ˜

- ์„ ํ˜•๊ตฌ์กฐ

์ˆœ์ฐจํƒ์ƒ‰

-๋น„์„ ํ˜•๊ตฌ์กฐ

BFS(๋„“์ด ์šฐ์„  ํƒ์ƒ‰),DFS(๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰)

 

 

์ˆœ์ฐจํƒ์ƒ‰

1) ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ๊ตฌ์กฐํ™”
2) ๊ตฌ์กฐํ™”๋œ ๊ณต๊ฐ„์„ ์ ์ ˆํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๋ฅผ ์ฐพ์„ ๋•Œ ๊นŒ์ง€ ํƒ์ƒ‰

BFS,DFS ๋‘ ๊ฐ€์ง€ ๋ชจ๋‘ ๊ทธ๋ž˜ํ”„๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•!

๊ทธ๋ž˜ํ”„๋ž€?

node(์ •์ )๊ณผ ๊ทธ ์ •์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” edge(๊ฐ„์„ )์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ์ผ์ข….
๊ทธ๋ž˜ํ”„๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ํ•˜๋‚˜์˜ ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ์ฐจ๋ก€๋Œ€๋กœ ๋ชจ๋“  ์ •์ ๋“ค์„ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๋Š” ๊ฒƒ

BFS(๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰, Breadth-Fist Search)


์ถœ์ฒ˜ https://developer-mac.tistory.com/64

๋ฃจํŠธ ๋…ธ๋“œ(ํ˜น์€ ๋‹ค๋ฅธ ์ž„์˜์˜ ๋…ธ๋“œ)์—์„œ ์‹œ์ž‘ํ•ด์„œ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ๋จผ์ € ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ,
์‹œ์ž‘ ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ€๊นŒ์šด ์ •์ ์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ  ๋ฉ€๋ฆฌ ๋–จ์–ด์ ธ ์žˆ๋Š” ์ •์ ์„ ๋‚˜์ค‘์— ๋ฐฉ๋ฌธํ•˜๋Š” ์ˆœํšŒ ๋ฐฉ๋ฒ•

์ฃผ๋กœ ๋‘ ๋…ธ๋“œ ์‚ฌ์ด์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ณ  ์‹ถ์„ ๋•Œ ์ด ๋ฐฉ๋ฒ•์„ ์„ ํƒ
ex) ์ง€๊ตฌ ์ƒ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ์นœ๊ตฌ ๊ด€๊ณ„๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ํ‘œํ˜„ํ•œ ํ›„ Sam๊ณผ Eddie์‚ฌ์ด์— ์กด์žฌํ•˜๋Š” ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๊ฒฝ์šฐ

  • ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์˜ ๊ฒฝ์šฐ - ๋ชจ๋“  ์นœ๊ตฌ ๊ด€๊ณ„๋ฅผ ๋‹ค ์‚ดํŽด๋ด์•ผ ํ• ์ง€๋„ ๋ชจ๋ฆ„
  • ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์˜ ๊ฒฝ์šฐ - Sam๊ณผ ๊ฐ€๊นŒ์šด ๊ด€๊ณ„๋ถ€ํ„ฐ ํƒ์ƒ‰

DFS(๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰, Depth-First Search)

์ถœ์ฒ˜ https://developer-mac.tistory.com/64

๋ฃจํŠธ ๋…ธ๋“œ(ํ˜น์€ ๋‹ค๋ฅธ ์ž„์˜์˜ ๋…ธ๋“œ)์—์„œ ์‹œ์ž‘ํ•ด์„œ ๋‹ค์Œ ๋ถ„๊ธฐ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๋ถ„๊ธฐ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ์‹

์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฏธ๋กœ์ฐพ๊ธฐ๋ฅผ ํ•  ๋•Œ ์ตœ๋Œ€ํ•œ ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ์„ ๋•Œ๊นŒ์ง€ ์ญ‰ ๊ฐ€๋‹ค๊ฐ€ ๋” ์ด์ƒ ๊ฐˆ ์ˆ˜ ์—†๊ฒŒ ๋˜๋ฉด ๋‹ค์‹œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ฐˆ๋ฆผ๊ธธ๋กœ ๋Œ์•„์™€์„œ ๊ทธ ๊ฐˆ๋ฆผ๊ธธ๋ถ€ํ„ฐ ๋‹ค์‹œ ๋‹ค๋ฅธ ๋ฐฉํ–ฅ์œผ๋กœ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ ๋ฐฉ์‹์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Œ

  1. ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ์— ์ด ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•จ
  2. ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS)์ด ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS)๋ณด๋‹ค ์ข€ ๋” ๊ฐ„๋‹จํ•จ
  3. ๊ฒ€์ƒ‰ ์†๋„ ์ž์ฒด๋Š” ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS)์— ๋น„ํ•ด์„œ ๋Š๋ฆผ

   DFS(๊นŠ์ด์šฐ์„ ํƒ์ƒ‰)                                                BFS(๋„ˆ๋น„์šฐ์„ ํƒ์ƒ‰)

ํ˜„์žฌ ์ •์ ์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ ๋“ค๊นŒ์ง€ ๋“ค์–ด๊ฐ€๋ฉด์„œ ํƒ์ƒ‰ ํ˜„์žฌ ์ •์ ์— ์—ฐ๊ฒฐ๋œ ๊ฐ€๊นŒ์šด ์ ๋“ค๋ถ€ํ„ฐ ํƒ์ƒ‰
์Šคํƒ ๋˜๋Š” ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ ํ๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌํ˜„

[https://codesyun.tistory.com/79](https://codesyun.tistory.com/79) [https://velog.io/@lucky-korma/DFS-BFS%EC%9D%98-%EC%84%A4%EB%AA%85-%EC%B0%A8%EC%9D%B4%EC%A0%90](https://velog.io/@lucky-korma/DFS-BFS%EC%9D%98-%EC%84%A4%EB%AA%85-%EC%B0%A8%EC%9D%B4%EC%A0%90) [https://developer-mac.tistory.com/64](https://developer-mac.tistory.com/64)

๋Œ“๊ธ€