2.py (859B) [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 stk = [(i0, j0)] 18 while stk: 19 (i, j) = stk.pop() 20 height = G[i][j] 21 if height == 9: 22 s += 1 23 continue 24 for pos in [(i+1,j),(i-1,j),(i,j-1),(i,j+1)]: 25 next = safeget(G, pos[0], pos[1]) 26 if next == height + 1: 27 stk.append(pos) 28 return s 29 30 31 def sol(G): 32 tot = 0 33 for i in range(len(G)): 34 for j in range(len(G[0])): 35 if G[i][j] == 0: 36 tot += score(G, i, j) 37 return tot 38 39 if __name__ == '__main__': 40 G = parse() 41 print(sol(G))