[백준-1743] 음식물 피하기 / Python
📚 Problem Solving/Baekjoon
1743번: 음식물 피하기
첫째 줄에 통로의 세로 길이 N(1 ≤ N ≤ 100)과 가로 길이 M(1 ≤ M ≤ 100) 그리고 음식물 쓰레기의 개수 K(1 ≤ K ≤ 10,000)이 주어진다. 그리고 다음 K개의 줄에 음식물이 떨어진 좌표 (r, c)가 주어진
www.acmicpc.net

import sys
from collections import deque
input = sys.stdin.readline
n, m, k = map(int, input().split())
graph = [["."] * m for _ in range(n)]
for _ in range(k):
r, c = map(int, input().split())
graph[r - 1][c - 1] = "#"
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
answer = 0
def bfs(x, y):
global answer
queue = deque()
queue.append((x, y))
size = 1
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m and graph[nx][ny] == "#":
graph[nx][ny] = "."
queue.append((nx, ny))
size += 1
answer = max(answer, size)
for i in range(n):
for j in range(m):
if graph[i][j] == "#":
graph[i][j] = "."
bfs(i, j)
print(answer)
해설
BFS 문제!
'📚 Problem Solving > Baekjoon' 카테고리의 다른 글
[백준-18405] 경쟁적 전염 / Python (0) | 2021.07.02 |
---|---|
[백준-6118] 숨바꼭질 / Python (0) | 2021.07.02 |
[백준-1926] 그림 / Python (0) | 2021.07.01 |
[백준-2251] 물통 / Python (0) | 2021.07.01 |
[백준-17413] 단어 뒤집기 2 / Python (1) | 2021.06.28 |