반응형
나름 평범한? dfs bfs 문제인거같은데
고민이 된게
방문한 단어를 기록하는 visited를 함수를 돌면서 들고다녀야 하나
그냥 함수 밖에 두고 사용해도 되나였다
함수 밖에두고 실행했더니 무한 재귀에 걸려 에러가 났다..
그래서 결론은 visited도 들고 다녀야한다!!
def solution(begin, target, words):
answer = 0
answers = []
def search(count, keyword, visited):
if keyword == target:
answers.append(count)
return
for word in words:
if word not in visited:
diff_count = 0
for i in range(len(begin)):
if keyword[i] != word[i]:
diff_count+=1
if diff_count == 1:
new_visited = visited +[word]
search(count+1, word, new_visited)
search(0,begin,[])
if answers != []:
answer = min(answers)
return answer
계속 화이팅!!
반응형
'개발💻 > 알고리즘' 카테고리의 다른 글
[프로그래머스] 이중우선순위큐 Python (0) | 2024.05.22 |
---|---|
[프로그래머스] 정수 삼각형 Python (0) | 2024.05.21 |
[프로그래머스] 베스트앨범 Python (21) | 2024.03.21 |
[프로그래머스] 네트워크 Python (21) | 2024.03.19 |
[프로그래머스] 카펫 Python (22) | 2024.03.18 |