티스토리 뷰

728x90

https://www.acmicpc.net/problem/1743

 

1743번: 음식물 피하기

첫째 줄에 통로의 세로 길이 N(1 ≤ N ≤ 100)과 가로 길이 M(1 ≤ M ≤ 100) 그리고 음식물 쓰레기의 개수 K(1 ≤ K ≤ N×M)이 주어진다.  그리고 다음 K개의 줄에 음식물이 떨어진 좌표 (r, c)가 주어진다

www.acmicpc.net

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from collections import deque
 
 
def bfs(x, y):
    global cnt
    dq = deque()
    dq.append((x, y))
    visit[x][y] = True
    while dq:
        x, y = dq.popleft()
        cnt += 1
        for dx, dy in direction:
            nx, ny = x + dx, y + dy
            if 0 <= nx <= N and 0 <= ny <= M and lst[nx][ny] == 1 and not visit[nx][ny]:
                dq.append([nx, ny])
                visit[nx][ny] = True
 
 
N, M, K = map(int, input().split())
lst = [[0 for _ in range(M + 1)] for _ in range(N + 1)]
visit = [[False for _ in range(M + 1)] for _ in range(N + 1)]
direction = [(0, 1), (1, 0), (0, -1), (-1, 0)]
res = 0
for i in range(K):
    a, b = map(int, input().split())
    lst[a][b] = 1
 
for i in range(1, N + 1):
    for j in range(1, M + 1):
        cnt = 0
        if lst[i][j] == 1 and not visit[i][j]:
            bfs(i, j)
        res = max(cnt, res)
print(res)
cs

 

댓글