1.py (921B) [raw]
1 #!/usr/bin/env python3 2 import sys 3 4 def parse(): 5 G = [] 6 for l in sys.stdin: 7 G.append([int(x) for x in l.strip()]) 8 return G 9 10 def safeget(G, i, j): 11 if i < 0 or j < 0 or i >= len(G) or j >= len(G[0]): 12 return None 13 return G[i][j] 14 15 def score(G, i0, j0): 16 s = 0 17 seen = set() 18 stk = [(i0, j0)] 19 while stk: 20 (i, j) = stk.pop() 21 seen.add((i, j)) 22 height = G[i][j] 23 if height == 9: 24 s += 1 25 continue 26 for pos in [(i+1,j),(i-1,j),(i,j-1),(i,j+1)]: 27 next = safeget(G, pos[0], pos[1]) 28 if next == height + 1 and pos not in seen: 29 stk.append(pos) 30 return s 31 32 33 def sol(G): 34 tot = 0 35 for i in range(len(G)): 36 for j in range(len(G[0])): 37 if G[i][j] == 0: 38 tot += score(G, i, j) 39 return tot 40 41 if __name__ == '__main__': 42 G = parse() 43 print(sol(G))