본문 바로가기

BFS55

[프로그래머스 Lv.3] 단어 변환 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr from collections import deque def solution(begin, target, words): if target not in words: # words 안에 없다면 0 반환 return 0 def canExchange(arr_a,arr_b): # 바꿀 수 있는 알파벳인지 확인 diff = 0 for i in range(len(arr_a)): if arr_a[i] != arr_b[i]: diff += 1 if diff == 1: return True else: return False de.. 2023. 9. 29.
[프로그래머스 Lv.2] 타겟 넘버 재풀이 [프로그래머스 Lv.2] 타겟 넘버 # BFS from collections import deque def solution(numbers, target): answer = 0 queue = deque() queue.append((numbers[0],0)) queue.append((-numbers[0],0)) while queue: v, index = queue.popleft() index += 1 if index < len(numbers): queue.append((v+numbers[ind woohyun-king.tistory.com def solution(numbers, target): answer = 0 def dfs(depth,s): nonlocal answer if depth == len(numb.. 2023. 9. 28.
[SWEA 5102번] 노드의 거리 from collections import deque t = int(input()) answer = [] for _ in range(t): v, e = map(int,input().split()) graph = [[] for _ in range(v+1)] visited = [0]*(v+1) for i in range(e): start, end = map(int,input().split()) graph[start].append(end) graph[end].append(start) s, g = map(int,input().split()) def bfs(s,g): visited[s] = 0 queue = deque([s]) while queue: v = queue.popleft() for node in gra.. 2023. 9. 21.
[SWEA 5105번] 미로의 거리 from collections import deque t= int(input()) dx = [-1,1,0,0] dy = [0,0,-1,1] answer = [] for _ in range(t): n = int(input()) maze = [list(map(int,list(input()))) for _ in range(n)] visited = [[0]*n for _ in range(n)] for i in range(n): for j in range(n): if maze[i][j] == 2: start_x, start_y = i, j def bfs(x,y): visited[x][y] = -1 queue = deque([(x,y)]) while queue: cur_x, cur_y = queue.popleft(.. 2023. 9. 21.
[백준 9205번] 맥주 마시면서 걸어가기 9205번: 맥주 마시면서 걸어가기 송도에 사는 상근이와 친구들은 송도에서 열리는 펜타포트 락 페스티벌에 가려고 한다. 올해는 맥주를 마시면서 걸어가기로 했다. 출발은 상근이네 집에서 하고, 맥주 한 박스를 들고 출발한다. www.acmicpc.net from collections import deque def calculate_distance(x1,y1,x2,y2): return abs(x1-x2) + abs(y1-y2) def bfs(x,y): global end_x, end_y queue = deque([(x,y)]) visited = set() while queue: x, y = queue.popleft() if calculate_distance(x,y,end_x,end_y) 2023. 8. 24.
[백준 1525번] 퍼즐 from copy import deepcopy from collections import deque import sys sys.setrecursionlimit(10**7) graph = [list(map(int,input().split())) for _ in range(3)] final_graph = [[1,2,3],[4,5,6],[7,8,0]] dx = [-1,1,0,0] dy = [0,0,-1,1] visited = {} temp_graph = deepcopy(graph) for i in range(3): for j in range(3): if graph[i][j] == 0: first_x, first_y = i, j def changeToString(arr): # 퍼즐의 상태를 문자열로 바꿔주는 함.. 2023. 8. 12.