-
Notifications
You must be signed in to change notification settings - Fork 0
/
boj_1926_bfs.py
55 lines (40 loc) · 1.43 KB
/
boj_1926_bfs.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from pprint import pprint
import sys
from collections import deque
input = sys.stdin.readline
input_x, input_y = input().rstrip().split(" ")
pictures = []
visited = [[False for j in range(int(input_y))] for i in range(int(input_x))]
max_size = 0
pictures_count = 0
dx = [0,0,1,-1]
dy = [1,-1,0,0]
for i in range(int(input_x)):
input_picture = list(map(int, input().split(" ")))
pictures.append(input_picture)
for cur_x in range(int(input_x)):
for cur_y in range(int(input_y)):
if visited[cur_x][cur_y] == True:
continue
if pictures[cur_x][cur_y] == 0:
continue
queue = deque([])
cur_size = 0
visited[cur_x][cur_y] = True
queue.append([cur_x,cur_y])
pictures_count += 1
while len(queue) != 0:
cur_pos = queue.popleft()
cur_size += 1
for i in range(4):
nx = cur_pos[0] + dx[i]
ny = cur_pos[1] + dy[i]
if nx < 0 or ny < 0 or nx >= int(input_x) or ny >= int(input_y):
continue
if visited[nx][ny] == False and pictures[nx][ny] == 1:
queue.append([nx, ny])
visited[nx][ny] = True
if max_size < cur_size:
max_size = cur_size
print(pictures_count)
print(max_size)