๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

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

๐ŸŽฎinspirer9 2023. 2. 25. 00:30
728x90
๋ฐ˜์‘ํ˜•
  • ์ฒ˜์Œ์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งŒ ๋ณด๊ณ  ๋‚˜์ด๋ธŒํ•˜๊ฒŒ ์ƒ๊ฐํ–ˆ๋‹ค.
  • ์–ด์ฐจํ”ผ ์ตœ์†Œ ๋‹จ๊ณ„๋Š” (๊ธ€์ž์ˆ˜ + 1) ์•„๋‹ˆ๋ƒ... len(begin)+1 ๋ฆฌํ„ดํ•˜๋ฉด ๋˜์ง€?

  • ๊ทธ ๊ฒฐ๊ณผ๋Š” ๋‹น์—ฐํžˆ ์‹คํŒจ์ง€ ใ…‹ใ…‹ใ…‹
  • ์—„์ฒญ ๊ผฌ์•„๋†“์€ ๊ฒƒ์ด ๋ถ„๋ช…ํ–ˆ๋‹ค.
  • hit -> hot -> hom -> mom -> mow -> cow -> cog ์ด๋Ÿฐ ์‹...

  • ๊ทธ๋ฆฌ๊ณ  ์ฒ˜์Œ์— ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด๋„ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค.
  • hit -> hot, hin, cit ์ด๋ ‡๊ฒŒ ๋‹ค ๋ฐ”๊ฟ€ ์ˆ˜๋„ ์žˆ์„๊ฑฐ๊ณ ...
  • ๊ฒฐ๊ตญ ์ถœ๋ฐœ์ง€๋Š” ์—ฌ๋Ÿฌ ๊ฐœ๊ณ  ๋„์ฐฉ์ง€๋Š” ํ•˜๋‚˜์ธ ์ƒ˜.
  • ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์ค‘์— ๊ฐ€์žฅ ์งง์€ ๊ฒฝ๋กœ๋ฅผ ๊ณ ๋ฅด๋Š” ๋ฌธ์ œ.

๋ฌธ์ œ ์„ค๋ช…

๋‘ ๊ฐœ์˜ ๋‹จ์–ด 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 ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

begin target words return
"hit" "cog" ["hot", "dot", "dog", "lot", "log", "cog"] 4
"hit" "cog" ["hot", "dot", "dog", "lot", "log"] 0

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์˜ˆ์ œ #1
๋ฌธ์ œ์— ๋‚˜์˜จ ์˜ˆ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ #2
target์ธ "cog"๋Š” words ์•ˆ์— ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

ํ’€์ด

  • ๋ฌธ์ œ๊ฐ€ DFS๋‚˜ BFS ์“ฐ๋Š” ๋ฌธ์ œ๋ผ๊ณ  ํ–ˆ์œผ๋‹ˆ... ๋‹ค์‹œ ์ œ๋Œ€๋กœ ์ƒ๊ฐํ•ด๋ณด๊ธฐ...
  • ๊ท€์ฐฎ์œผ๋‹ˆ ๊ทธ๋ƒฅ ๋”•์…”๋„ˆ๋ฆฌ์— ๋„ฃ๊ณ  for ๋ฌธ ๋Œ๋ ค... ์™œ ์‹คํŒจ๋‚˜์ง€?
def solution(begin, target, words):
    answer = 0

    if target not in words:
        return answer
    
    words_bridge = {}
    words_bridge[begin] = 0
    for word in words: words_bridge[word] = 999
    
    for depth in range(len(words)):
        print(words_bridge)
        for key, value in words_bridge.items():
            if value == depth:
                for word in words:
                    if len(set(list(word)) & set(list(key))) >= 2:
                        if words_bridge[word] > depth:
                            words_bridge[word] = depth + 1
                            if word == target:
                                return words_bridge[target]

    print(words_bridge)
    return words_bridge[target]
    
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	"hit", "cog", ["hot", "dot", "dog", "lot", "log", "cog"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	4
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	
{'hit': 0, 'hot': 999, 'dot': 999, 'dog': 999, 'lot': 999, 'log': 999, 'cog': 999}
{'hit': 0, 'hot': 1, 'dot': 999, 'dog': 999, 'lot': 999, 'log': 999, 'cog': 999}
{'hit': 0, 'hot': 1, 'dot': 2, 'dog': 999, 'lot': 2, 'log': 999, 'cog': 999}
{'hit': 0, 'hot': 1, 'dot': 2, 'dog': 3, 'lot': 2, 'log': 3, 'cog': 999}

ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	"hit", "cog", ["hot", "dot", "dog", "lot", "log"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	0
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	์‹คํŒจ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.26ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	์‹คํŒจ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
  • ํ•œ์ฐธ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ
    • ์•„... ํ•„์š”ํ•œ ๊ธ€์ž๊ฐ€ ์žˆ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ... ๊ธ€์ž ์ˆœ์„œ๋„ ๋™์ผํ•ด์•ผ ํ•˜๋Š”๊ตฌ๋‚˜!
      • ์‰ฝ๊ฒŒ ๊ฐ€๋ ค๊ณ  ํ–ˆ๋˜ ์ด ์ฝ”๋“œ๊ฐ€ ๋ฐœ๋ชฉ์„ ์žก์•˜๋„ค. ๊ดœํžˆ ์‹œ๊ฐ„ ๋‚ญ๋น„ ํ–ˆ๋‹ค. 
      • if len(set(list(word)) & set(list(key))) >= 2:
def solution(begin, target, words):
    answer = 0

    if target not in words:
        return answer
    
    words_bridge = {}
    for word in words: words_bridge[word] = 999
    words_bridge[begin] = 0
    
    i_will_leave_here = False
    for depth in range(len(words)):
        if i_will_leave_here == True:
            break
        for key, value in words_bridge.items():
            if value == depth:
                for word in words:
                    diffrent_character = 0
                    for i in range(len(word)):
                        if list(word)[i] != list(key)[i]:
                            diffrent_character += 1
                    if diffrent_character == 1:
                        if word == target:
                            i_will_leave_here = True
                        if words_bridge[word] > depth + 1:
                            words_bridge[word] = depth + 1

    #print(words_bridge)
    return words_bridge[target]
  • ๊ทธ๊ฑฐ ๋ง๊ณ ๋„ ํ‹€๋ฆฐ ๋ถ€๋ถ„ ์žˆ์—ˆ๋Š”๋ฐ ใ…Žใ…Žใ…Ž
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.16ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (1.64ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.00ms, 10MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด
from collections import deque

def get_adjacent(current, words):
    for word in words:
        if len(current) != len(word):
            continue
        count = 0
        for c, w in zip(current, word):
            if c != w:
                count += 1
        if count == 1:
            yield word

def solution(begin, target, words):
    dist = {begin: 0}
    queue = deque([begin])

    while queue:
        current = queue.popleft()

        for next_word in get_adjacent(current, words):
            if next_word not in dist:
                dist[next_word] = dist[current] + 1
                queue.append(next_word)

    return dist.get(target, 0)

ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.81ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
  • ์™€์šฐ? ์ด๊ฑด ๋ญ์ง€?
def solution(begin, target, words):
    answer = 0
    Q = [begin]

    while True:
        temp_Q = []
        for word_1 in Q:
            if word_1 == target:
                    return answer
            for i in range(len(words)-1, -1, -1):
                word_2 = words[i]
                if sum([x!=y for x, y in zip(word_1, word_2)]) == 1:
                    temp_Q.append(words.pop(i))

        if not temp_Q:
            return 0
        Q = temp_Q
        answer += 1
        
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.53ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
  • ์˜ˆ์˜๊ฒŒ ๋‹ค๋“ฌ ๋‹ค๋“ฌ ใ…Žใ…Žใ…Ž
def solution(begin, target, words):
    answer = 0

    if target not in words:
        return answer
    
    words_bridge = {}
    for word in words: words_bridge[word] = 999
    words_bridge[begin] = 0
    
    for depth in range(len(words)):
        for key, value in words_bridge.items():
            if value == depth:
                for word in words:
                    if sum(1 for a,b in zip(word,key) if a != b) == 1:
                        if words_bridge[word] > depth + 1:
                            words_bridge[word] = depth + 1
                        if word == target: break

    return words_bridge[target]
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.53ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.3MB)
728x90
๋ฐ˜์‘ํ˜•