ひさしぶりの写経しました.
E - イルミネーション (Illumination)
建物の中にあるイルミネーションしないマスをどうやって数えるかが分からなかった.
from sys import stdin input = stdin.readline w, h = map(int, input().strip().split()) g = [list(map(int, input().strip().split())) for _ in range(h)] for i in range(h): g[i] = ['x'] + g[i] + ['x'] g.insert(0, ['x'] * (w + 2)) g.append(['x'] * (w + 2)) w, h = len(g[0]), len(g) directs = [[(0, 1), (0, -1), (-1, 0), (1, 0), (1, -1), (-1, -1)], [(0, 1), (0, -1), (-1, 1), (1, 1), (1, 0), (-1, 0)]] visited = [[0] * w for _ in range(h)] stack = [(0, 0), (1, 0)] while stack: i, j = stack.pop() visited[i][j] = 1 direct = directs[i % 2] for dh, dw in direct: fh, fw = i + dh, j + dw if not ((0 <= fh < h) and (0 <= fw < w)): continue if visited[fh][fw]: continue if g[fh][fw] == 1: continue g[fh][fw] = 'x' stack.append((fh, fw)) ans = 0 for i in range(h): for j in range(w): if g[i][j] == 'x': continue direct = directs[i % 2] if g[i][j] == 1: cnt = 0 for dh, dw in direct: fh, fw = i + dh, j + dw if not ((0 <= fh < h) and (0 <= fw < w)): continue if g[fh][fw] == 1: cnt += 1 ans += 6 - cnt if g[i][j] == 0: cnt = 0 for dh, dw in direct: fh, fw = i + dh, j + dw if not ((0 <= fh < h) and (0 <= fw < w)): continue if g[fh][fw] == 1: cnt += 1 ans -= cnt print(ans)