1
1
import sys
2
2
from collections import deque
3
+
3
4
input = sys .stdin .readline
4
5
5
- odd = [(- 1 , 1 ), (0 , 1 ), (1 , 1 ), (1 , 0 ), (0 , - 1 ), (- 1 , 0 )] # 행, 렬 기준으로함 정각부터 정시계방향으로 순환
6
+ odd = [(- 1 , 1 ), (0 , 1 ), (1 , 1 ), (1 , 0 ), (0 , - 1 ), (- 1 , 0 )] # 행, 렬 기준으로함 정각부터 정시계방향으로 순환
6
7
even = [(- 1 , 0 ), (0 , 1 ), (1 , 0 ), (1 , - 1 ), (0 , - 1 ), (- 1 , - 1 )]
7
8
9
+
8
10
def bfs (x , y ):
9
11
q = deque ([(x , y )])
10
12
count = 0
11
13
while q :
12
14
x , y = q .popleft ()
13
15
visited [x ][y ] = 1
14
- dir = even if x % 2 == 0 else odd
16
+ dir = even if x % 2 == 0 else odd
15
17
for i in range (6 ):
16
18
dx , dy = dir [i ][0 ], dir [i ][1 ]
17
19
nx , ny = x + dx , y + dy
18
20
19
- if 0 <= nx < h + 2 and 0 <= ny < w + 2 :
21
+ if 0 <= nx < h + 2 and 0 <= ny < w + 2 :
20
22
if arr [nx ][ny ] == 1 :
21
23
count += 1
22
24
elif arr [nx ][ny ] == 0 and not visited [nx ][ny ]:
@@ -25,13 +27,12 @@ def bfs(x, y):
25
27
return count
26
28
27
29
28
-
29
- w , h = map (int , input ().split ()) # 열, 행 순서로 입력
30
- arr = [[0 ] * (w + 2 )]
31
- visited = [[0 ] * (w + 2 ) for _ in range (h + 2 )]
30
+ w , h = map (int , input ().split ()) # 열, 행 순서로 입력
31
+ arr = [[0 ] * (w + 2 )]
32
+ visited = [[0 ] * (w + 2 ) for _ in range (h + 2 )]
32
33
for i in range (h ):
33
- arr .append ([0 ]+ list (map (int , input ().split ())) + [0 ])
34
- arr .append ([0 for _ in range (w + 2 )])
34
+ arr .append ([0 ] + list (map (int , input ().split ())) + [0 ])
35
+ arr .append ([0 for _ in range (w + 2 )])
35
36
36
37
# for i in range(h+2):
37
38
# print(arr[i])
0 commit comments