본문 바로가기

BFS55

[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.
[백준 14502번] 연구소 import copy def dfs(graph, x, y): if graph[x][y] != 2: return for i in range(4): temp_x = x + nx[i] temp_y = y + ny[i] if temp_x >=0 and temp_x =0 and temp_y < m: if graph[temp_x][temp_y] == 0: graph[temp_x][temp_y] = 2 dfs(graph,temp_x,temp_y) def count_safe_area(graph): count = 0 for i in range(n): for j in range(m): if graph[i][j] == 0: count += 1 return count n, m = map(int,in.. 2023. 5. 10.
[백준 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.
[백준 7569번] 토마토(시즌 2) import sys input = sys.stdin.readline from collections import deque # 가로(m), 세로(n), 높이(h) m, n, h = map(int,input().split()) # 3차원 리스트 선언 및 초기화 area = [[[0]*m for _ in range(n)] for _ in range(h)] # 3차원 리스트 입력받기 for i in range(h): for j in range(n): area[i][j] = list(map(int,input().split())) # 값이 1인 인덱스(높이,세로,가로)배열 one_list = [] # 값이 0인 인덱스(높이,세로,가로)배열 zero_list = [] # 3차원 리스트에서 값이 1인 인덱스와 값이 0.. 2023. 2. 16.
[백준 2468번] 안전 영역 from collections import deque # 2차원 배열의 행과 열 개수 n = int(input()) # 지역 높이 정보(2차원 배열) area = [] visited = [[False]*n for _ in range(n)] count_list = [] solution = 0 for _ in range(n): area.append(list(map(int,input().split()))) max_value = max(map(max,area)) def bfs(x,y,value): if visited[x][y] or (area[x][y]-value) = 0 and temp_x = 0 and temp_y < n: if (area[temp_x][temp_y] - valu.. 2023. 2. 15.