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 |
Tags
- Deque
- set
- bfs
- 약수구하기
- 재귀
- 오픽독학
- 슬라이싱
- 복사
- 최대재귀높이
- 구현
- Inner Join
- IH
- 파일명 변경
- 딕셔너리
- 스택
- 이진탐색
- heapq
- %H
- not null
- issubset
- 백준
- 완전이진트리
- dfs
- 우선순위큐
- 브루트포스
- 제곱근
- 파이썬
- 삼중for문 탈출
- date_format
- 오픽
Archives
- Today
- Total
나예
[프로그래머스] 단어변환(bfs, 파이썬) 본문
1. 문제
https://school.programmers.co.kr/learn/courses/30/lessons/43163
2. 풀이
시작코드를 큐에 넣음 이때 깊이를 표시해주게 튜플형태로 넣어줬다
q가 비거나 타겟을 q에서 뽑을때까지 아래과정 반복
q에서 하나 뽑고 방문하지 않고 뽑은 것과 하나 차이로 다른것을 골라서 큐에 넣고 depth1 증가시킴
내일 다시 풀어봐야겠디.. 하면서도 헷갈린다..
3. 코드
from collections import deque
def solution(begin, target, words):
answer = 0
visited= [ 0 for i in range(len(words))]
q =deque()
q.append((begin,0))
while q:
x = q.popleft()
print(x)
if x[0] == target:
print(x,'뽑았다 멈추자')
answer = x[1]
break
for i in range(len(words)):
word=words[i]
if visited[i] !=0:
continue
else:
#전체 단어중 몇단어 맞나 세볼용도
count=0
for j in range(len(word)):
if x[0][j] != word[j]:
count +=1
if count ==1:
q.append((word,x[1]+1))
visited[i]=1
return answer
728x90