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
| from collections import deque
import sys
input = sys.stdin.readline
W, H = map(lambda x: int(x)+2, input().split())
maps = [[0] * W for _ in range(H)]
for i in range(1,H-1):
maps[i] = [0]+list(map(int, input().split()))+[0]
visited = [[False] * W for _ in range(H)]
def bfs(start):
queue = deque([start])
dy = [0,1,0,-1,1,-1]
cnt = 0
while queue:
y,x = queue.popleft()
dx = [-1,0,1,0,-1,-1] if y%2==0 else [-1,0,1,0,1,1]
for i in range(6):
ny, nx = y+dy[i], x+dx[i]
if 0<=ny<H and 0<=nx<W:
if maps[ny][nx] == 0 and not visited[ny][nx]:
queue.append((ny,nx))
visited[ny][nx] = True
elif maps[ny][nx] == 1:
cnt += 1
return cnt
visited[0][0] = True
print(bfs((0,0)))
|