[level 3] Title: 단어 변환, Time: 0.44 ms, Memory: 10.2 MB -BaekjoonHub

This commit is contained in:
SSUM
2025-03-07 21:27:10 +09:00
parent 790795e937
commit a923a2aacd
2 changed files with 95 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
# [level 3] 단어 변환 - 43163
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/43163)
### 성능 요약
메모리: 10.2 MB, 시간: 0.44 ms
### 구분
코딩테스트 연습 > 깊이너비우선탐색DFSBFS
### 채점결과
정확성: 100.0<br/>합계: 100.0 / 100.0
### 제출 일자
2025년 03월 07일 21:27:08
### 문제 설명
<p>두 개의 단어 begin, target과 단어의 집합 words가 있습니다. 아래와 같은 규칙을 이용하여 begin에서 target으로 변환하는 가장 짧은 변환 과정을 찾으려고 합니다.</p>
<div class="highlight"><pre class="codehilite"><code>1. 한 번에 한 개의 알파벳만 바꿀 수 있습니다.
2. words에 있는 단어로만 변환할 수 있습니다.
</code></pre></div>
<p>예를 들어 begin이 "hit", target가 "cog", words가 ["hot","dot","dog","lot","log","cog"]라면 "hit" -&gt; "hot" -&gt; "dot" -&gt; "dog" -&gt; "cog"와 같이 4단계를 거쳐 변환할 수 있습니다.</p>
<p>두 개의 단어 begin, target과 단어의 집합 words가 매개변수로 주어질 때, 최소 몇 단계의 과정을 거쳐 begin을 target으로 변환할 수 있는지 return 하도록 solution 함수를 작성해주세요.</p>
<h5>제한사항</h5>
<ul>
<li>각 단어는 알파벳 소문자로만 이루어져 있습니다.</li>
<li>각 단어의 길이는 3 이상 10 이하이며 모든 단어의 길이는 같습니다.</li>
<li>words에는 3개 이상 50개 이하의 단어가 있으며 중복되는 단어는 없습니다.</li>
<li>begin과 target은 같지 않습니다.</li>
<li>변환할 수 없는 경우에는 0를 return 합니다.</li>
</ul>
<h5>입출력 예</h5>
<table class="table">
<thead><tr>
<th>begin</th>
<th>target</th>
<th>words</th>
<th>return</th>
</tr>
</thead>
<tbody><tr>
<td>"hit"</td>
<td>"cog"</td>
<td>["hot", "dot", "dog", "lot", "log", "cog"]</td>
<td>4</td>
</tr>
<tr>
<td>"hit"</td>
<td>"cog"</td>
<td>["hot", "dot", "dog", "lot", "log"]</td>
<td>0</td>
</tr>
</tbody>
</table>
<h5>입출력 예 설명</h5>
<p>예제 #1<br>
문제에 나온 예와 같습니다.</p>
<p>예제 #2<br>
target인 "cog"는 words 안에 없기 때문에 변환할 수 없습니다.</p>
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges

View File

@@ -0,0 +1,22 @@
from collections import deque
def solution(begin, target, words):
len_words = len(words)
len_word = len(words[0])
queue = deque([(begin, 0)])
visited = set()
while queue:
pop_word, dist = queue.popleft()
if pop_word == target:
return dist
for word in words:
num = 0
for i in range(len_word):
if(pop_word[i] != word[i]):
num += 1
if num == 1 and word not in visited:
queue.append((word, dist+1))
visited.add(word)
answer = 0
return answer