[프로그래머스 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.
[백준 16236번] 아기 상어
from collections import deque n = int(input()) area = [] result = 0 shark_size = 2 ate_fish = 0 nx = [-1,1,0,0] ny = [0,0,-1,1] for i in range(n): area.append(list(map(int,input().split()))) for i in range(n): for j in range(n): if area[i][j] == 9: shark_location_x, shark_location_y = i, j def bfs(x,y,sharkSize): visited = [[False]*n for _ in range(n)] distence = [[0]*n for _ in range(n)] visite..
2023. 5. 27.
[HackerRank] Connected Cells in a Grid
def bfs(x,y,area,row,column,visited): if area[x][y] != 1 or visited[x][y]: return 0 count = 1 visited[x][y] = True queue = [(x,y)] nx = [-1,1,0,0,-1,-1,1,1] ny = [0,0,-1,1,-1,1,-1,1] while queue: v = queue.pop(0) for i in range(8): temp_x = v[0] + nx[i] temp_y = v[1] + ny[i] if temp_x >= 0 and temp_x = 0 and temp_y < column: if area[temp_x][temp_y] == 1 and not visited[temp_x][..
2023. 5. 12.
[HackerRank] KnightL on a Chessboard
def findMinimum(a,b,n): area = [[0]*n for _ in range(n)] visited = [[False]*n for _ in range(n)] nx = [a, a, -a, -a, b, b, -b, -b] ny = [b, -b, b, -b, a, -a, a, -a] queue =[[0,0]] visited[0][0] = True while queue: v = queue.pop(0) for i in range(8): temp_x = v[0] + nx[i] temp_y = v[1] + ny[i] if temp_x >= 0 and temp_x =0 and temp_y < n: if not visited[temp_x][temp_y]: if area[tem..
2023. 5. 12.
[백준 2583번] 영역 구하기
from collections import deque m,n,k = map(int,input().split()) area = [[0]*n for _ in range(m)] visited = [[False]*n for _ in range(m)] count_list = [] for i in range(k): x1, y1, x2, y2 = map(int,input().split()) for j in range(y1,y2): for k in range(x1,x2): area[j][k] = 1 def bfs(x,y): if visited[x][y] or area[x][y] == 1: return False count = 1 queue = deque([(x,y)]) visited[x][y] = True while ..
2023. 4. 6.