본문 바로가기

Algorithm 💡/BFS42

[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.
[백준 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.
[백준 16234번] 인구 이동 from collections import deque import math N, L ,R = map(int,input().split()) data = [list(map(int,input().split())) for _ in range(N)] visited = [[False]*N for _ in range(N)] dx = [-1,1,0,0] dy = [0,0,-1,1] count = 0 def bfs(x,y): # 국경선을 열고 인구를 이동시키는 BFS 함수 if visited[x][y]: # 이미 방문한 곳이면 Pass return False visited[x][y] = True group = [(x,y)] # 연합을 이룬 좌표 목록 total = data[x][y] # 연합의 총합 queue = deq.. 2023. 8. 7.
[백준 2206번] 벽 부수고 이동하기 from collections import deque n, m = map(int,input().split()) maze = [list(map(int,list(input()))) for _ in range(n)] visited = [[[0]*m for _ in range(n)] for _ in range(2)] dx = [-1,1,0,0] dy = [0,0,-1,1] def bfs(): visited[0][0][0] = 1 queue = deque([(0,0,0)]) while queue: w,x,y = queue.popleft() if x == n-1 and y == m-1: return visited[w][x][y] for i in range(4): nx = x + dx[i] ny = y + dy[i].. 2023. 8. 4.
[백준 14923번] 미로 탈출 from collections import deque dx = [-1,1,0,0] dy = [0,0,-1,1] n, m = map(int,input().split()) hx, hy = map(int,input().split()) ex, ey = map(int,input().split()) maze = [list(map(int,input().split())) for _ in range(n)] visited = [[[0]*m for _ in range(n)] for _ in range(2)] def bfs(): visited[0][hx-1][hy-1] = 0 queue = deque([(0,hx-1,hy-1)]) while queue: v = queue.popleft() w, x, y = v[0], v[1].. 2023. 8. 4.