[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/python] ๊นŠ์ด/๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(DFS/BFS) : ๋‹จ์–ด ๋ณ€ํ™˜

2021. 4. 11. 20:51

๋ฌธ์ œ ์„ค๋ช…

๋‘ ๊ฐœ์˜ ๋‹จ์–ด begin, target๊ณผ ๋‹จ์–ด์˜ ์ง‘ํ•ฉ words๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ๊ทœ์น™์„ ์ด์šฉํ•˜์—ฌ begin์—์„œ target์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฐ€์žฅ ์งง์€ ๋ณ€ํ™˜ ๊ณผ์ •์„ ์ฐพ์œผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

  1. ํ•œ ๋ฒˆ์— ํ•œ ๊ฐœ์˜ ์•ŒํŒŒ๋ฒณ๋งŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. words์— ์žˆ๋Š” ๋‹จ์–ด๋กœ๋งŒ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด begin์ด "hit", target๊ฐ€ "cog", words๊ฐ€ ["hot","dot","dog","lot","log","cog"]๋ผ๋ฉด "hit" -> "hot" -> "dot" -> "dog" -> "cog"์™€ ๊ฐ™์ด 4๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‘ ๊ฐœ์˜ ๋‹จ์–ด begin, target๊ณผ ๋‹จ์–ด์˜ ์ง‘ํ•ฉ words๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ตœ์†Œ ๋ช‡ ๋‹จ๊ณ„์˜ ๊ณผ์ •์„ ๊ฑฐ์ณ begin์„ target์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ์‚ฌํ•ญ

  • ๊ฐ ๋‹จ์–ด๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ฐ ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 3 ์ด์ƒ 10 ์ดํ•˜์ด๋ฉฐ ๋ชจ๋“  ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • words์—๋Š” 3๊ฐœ ์ด์ƒ 50๊ฐœ ์ดํ•˜์˜ ๋‹จ์–ด๊ฐ€ ์žˆ์œผ๋ฉฐ ์ค‘๋ณต๋˜๋Š” ๋‹จ์–ด๋Š” ์—†์Šต๋‹ˆ๋‹ค.
  • begin๊ณผ target์€ ๊ฐ™์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” 0๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค.

 

์ •๋‹ต

def solution(begin, target, words):
    answer = 0
    queue = [begin]

    while True:
        temp_queue = []
        for word_1 in queue:
            
            if word_1 == target:
                    return answer

            for i in range(len(words)-1, -1, -1):
                word_2 = words[i]
                # word_1๊ณผ word_2์˜ ์ฒ ์ž ์ค‘ ํ•œ๊ธ€์ž๋งŒ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด
                if sum([x!=y for x, y in zip(word_1, word_2)]) == 1:
                    temp_queue.append(words.pop(i))

        if not temp_queue:
            return 0
        queue = temp_queue
        answer += 1

		if word_1 == target:
                    return answer

queue์— ๋“ค์–ด์žˆ๋Š” word_1๊ณผ target์ด ๊ฐ™์œผ๋ฉด ํƒ์ƒ‰์ด ๋๋‚˜๋ฏ€๋กœ

๋ช‡ ๋‹จ๊ณ„ ๊ณผ์ •์„ ๊ฑฐ์ณค๋Š”์ง€ ์ €์žฅํ•œ answer๋ฅผ returnํ•œ๋‹ค.

 

for i in range(len(words)-1, -1, -1):
                word_2 = words[i]
                # word_1๊ณผ word_2์˜ ์ฒ ์ž ์ค‘ ํ•œ๊ธ€์ž๋งŒ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด
                if sum([x!=y for x, y in zip(word_1, word_2)]) == 1:
                    temp_queue.append(words.pop(i))

๋‹จ์–ด์˜ ์ง‘ํ•ฉ words์˜ ๋งˆ์ง€๋ง‰ ๋‹จ์–ด๋ถ€ํ„ฐ ์‚ดํŽด๋ณด๋ฉด์„œ word_2์— ์ €์žฅํ•œ๋‹ค.

๊ฐ™์€ ์œ„์น˜์˜ word_2์™€ queue์—์„œ ๊บผ๋‚ธ word_1์˜ ๋‹จ์–ด ์ฒ ์ž๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด 1์„ ๋ฐ˜ํ™˜ํ•˜๋„๋กํ•˜์—ฌ

๋‹จ์–ด์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์‚ดํŽด๋ณธ ํ›„ ๋‹ค ๋”ํ–ˆ์„ ๋•Œ 1์ด๋ผ๋ฉด ์ฒ ์ž๊ฐ€ ํ•˜๋‚˜๋งŒ ๋‹ค๋ฆ„์„ ์˜๋ฏธํ•œ๋‹ค.

์ด๋•Œ temp_queue์— ํ•ด๋‹น ๋‹จ์–ด๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š”๋ฐ

words.pop์„ ์ด์šฉํ•˜์—ฌ ๋‹ค์Œ๋ฒˆ์— ๊ทธ ๋‹จ์–ด๋ฅผ ๋‹ค์‹œ ์‚ดํŽด๋ณด์ง€ ์•Š๋„๋ก word์—์„œ ์—†์• ์ค€๋‹ค.

 

	if not temp_queue:
            return 0
        queue = temp_queue
        answer += 1

๋งŒ์•ฝ temp_queue๊ฐ€ ๋น„์—ˆ๋‹ค๋ฉด ์ฒ ์ž ํ•˜๋‚˜๋งŒ ๋‹ค๋ฅธ ๋‹จ์–ด๊ฐ€ word์— ์—†๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ

๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋œป์ด๊ธฐ ๋•Œ๋ฌธ์— 0์„ return ํ•œ๋‹ค.

๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด queue๋ฅผ temp_queue๋กœ ๋ฐ”๊พธ๊ณ  ํ•œ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณค์œผ๋‹ˆ answer์— 1์„ ๋”ํ•ด์ค€๋‹ค.

 

์ด์ œ ๋‹ค์‹œ queue์— ์žˆ๋Š” ๋‹จ์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์œ„ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

728x90

BELATED ARTICLES

more