Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- getline()함수
- const l-value참조자
- string유형
- C언어 덱
- 네트워크 결합
- 프로그래머스 푸드 파이트 대회
- LAN의 분류
- 원형 연결 구조 연결된 큐
- 문자형 배열
- auto 키워드
- C언어 계산기 프로그램
- 백준 파이썬
- 유형 변환
- c언어 괄호검사
- r-value참조자
- 알고리즘 조건
- 입출력 관리자
- l-value참조자
- 문제해결 단계
- 값/참조/주소에 의한 전달
- C언어 스택 연산
- 논리 연산
- 괄호 검사 프로그램
- IPv4 주소체계
- const화
- 프로그래머스 배열만들기4
- 주기억장치
- 운영체제 기능
- 회전 및 자리 이동 연산
- 범위 기반 for문
Archives
- Today
- Total
chyam
[프로그래머스 Lv3, python] - 단어 변환 본문

https://school.programmers.co.kr/learn/courses/30/lessons/43163
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
# 내 풀이(dfs)
min_res = float('inf')
def dfs(stand,target,cnt,visited,words):
global min_res
if stand == target:
min_res = min(min_res,cnt)
if cnt > len(words): # 깊이 벗어남
return
for i in range(len(words)):
diff = 0
if not visited[i]: # 방문 X
for t in range(len(stand)):
if stand[t] != words[i][t]:
diff += 1
if diff == 1:# 1개만 다름
visited[i] = True
dfs(words[i],target,cnt+1,visited,words)
visited[i] = False
def solution(begin, target, words):
answer = 0
visited = [False]*len(words)
if target not in words: # 단어안에 없으면
return 0
dfs(begin,target,0,visited,words)
if min_res == float('inf'):
return 0
return min_res
# bfs 사용
from collections import deque
def solution(begin, target, words):
if target not in words:
return 0
visited = [False] * len(words)
queue = deque([(begin, 0)]) # 기준, 횟수
while queue:
current, cnt = queue.popleft()
if current == target: # 같으면 리턴! (최소임)
return cnt
for i in range(len(words)):
if not visited[i]: # 방문 X
diff = 0
for j in range(len(current)):
if current[j] != words[i][j]: # 다른가?
diff += 1
if diff == 1: # 다른게 한개임 => 방문
visited[i] = True
queue.append((words[i], cnt + 1)) # 기준을 갱신, 횟수+1
return 0'프로그래머스 > LV3' 카테고리의 다른 글
| [프로그래머스 Lv3, python] - 베스트앨범 (1) | 2026.05.04 |
|---|---|
| [프로그래머스 Lv3, python] - 기지국 설치 (2) | 2026.05.03 |
| [프로그래머스 Lv3,python] - 네트워크 (2) | 2026.04.27 |
| [프로그래머스 Lv3,python] - 단속카메라 (0) | 2025.07.15 |
| [프로그래머스 Lv3, python] - 정수 삼각형 (1) | 2025.07.11 |
