[백준 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.
[프로그래머스 Lv.2] 게임 맵 최단거리
from collections import deque def solution(maps): n,m = len(maps), len(maps[0]) # 행 개수, 열 개수 dx = [-1,1,0,0] dy = [0,0,-1,1] visited = [[False]*m for _ in range(n)] queue = deque([(0,0)]) visited[0][0] = True while queue: x,y = queue.popleft() for i in range(4): nx = x + dx[i] ny = y + dy[i] if nx >= 0 and nx = 0 and ny < m and not visited[nx][ny] and maps[nx][ny] != 0: queue.append(..
2023. 8. 2.