Notice
Recent Posts
Recent Comments
Link
S E P H ' S
[Python] 단어 변환 본문
def solution(begin, target, words):
answer = len(words)
depth = -1
if target not in words:
return 0
answer = DFS(begin, target, words, depth, answer)
return answer
def DFS(begin, target, words, depth, answer):
depth += 1
nextWord = []
if begin == target:
if depth <= answer:
answer = depth
return answer
for word in words:
cnt = 0
for idx, char in enumerate(word):
if begin[idx] != char:
cnt += 1
if cnt == 1:
nextWord.append(word)
if begin in words:
words.remove(begin)
for begin in nextWord:
answer = DFS(begin, target, words, depth, answer)
return answer
'Algorithm > Programmers' 카테고리의 다른 글
[Python] 카펫 (0) | 2021.08.08 |
---|---|
[Python] 여행 경로 (0) | 2021.08.07 |
[Python] 네트워크 (0) | 2021.08.07 |
[Python] 땅따먹기 (0) | 2021.07.31 |
[Python] 숫자의 표현 (0) | 2021.07.31 |