![[백준-1926] 그림](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FA7Xb2%2FbtsIDnFKmtS%2FKhfDKiNQHN9RWhk0eKTLO1%2Fimg.jpg)
[백준-1926] 그림코테2024. 7. 18. 14:30
인트로
백준은 js로 입력 받는 게 불편하여 파이썬을 사용하여 풀었습니다.
알고리즘 공부를 안한지 꽤 되어 다시 천천히 시작해보려고 합니다.
문제
실버1 : https://www.acmicpc.net/problem/1926
나의 풀이
from collections import deque
graph = []
visited = set([])
cnt = 0
max = 0
def dfs(start_x, start_y, graph):
global cnt
global visited
dx = [1,0,-1,0]
dy = [0,1,0,-1]
size = 1
cnt += 1
visited.add((start_x, start_y))
queue = deque([(start_x, start_y)])
while queue:
x, y = queue.popleft()
for i in range(4):
nx = dx[i] + x
ny = dy[i] + y
if(0 <= nx < x_size and 0 <= ny < y_size):
if (nx,ny) not in visited and graph[nx][ny] == 1:
visited.add((nx,ny))
queue.append((nx,ny))
size += 1
return size
ex_input = list(map(int,input().split(' ')))
x_size, y_size = ex_input
for i in range(x_size):
graph.append(list(map(int,input().split(' '))))
for i in range(x_size):
for j in range(y_size):
if graph[i][j] == 1 and (i,j) not in visited:
size = dfs(i,j,graph=graph)
if max < size: max = size
print(cnt)
print(max)
@두루마기 :: 내가해냄
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!