알고리즘/DFS | BFS
파이썬 | 백준 | 1743 | 음식물 피하기
cha-n
2020. 10. 4. 20:08
solution
# 1743, 음식물 피하기
import sys
sys.setrecursionlimit(10000)
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
def dfs(x, y):
visited[x][y] = 1
global cnt
cnt += 1
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 1 <= nx < n + 1 and 1 <= ny < m + 1:
if visited[nx][ny] == 0 and aisle[nx][ny] == 1:
dfs(nx, ny)
n, m, k = map(int, sys.stdin.readline().split())
aisle = [[0] * (m + 1) for _ in range(n + 1)]
visited = [[0] * (m + 1) for _ in range(n + 1)]
for _ in range(k):
r, c = map(int, sys.stdin.readline().split())
aisle[r][c] = 1
max_ = 0
for i in range(1, n + 1):
for j in range(1, m + 1):
if aisle[i][j] == 1 and visited[i][j] == 0:
cnt = 0
dfs(i, j)
max_ = max(max_, cnt)
print(max_)
문제 출처 www.acmicpc.net/problem/1743