fork download
  1. E=enumerate
  2. def V(m,x,y):
  3. if x>-1<y:
  4. try:return m[x][y]
  5. except:return 0
  6. def f(m):
  7. s=[];f=e=0
  8. while O:=[(x,y)for x,b in E(m)for y,_ in E(b)if m[x][y]and(x,y)not in s]:
  9. q=[O[0]]
  10. while q:
  11. x,y=q.pop(0);M,D=[(0,1),(0,-1),(1,0),(-1,0)],[(-1,-1),(-1,1),(1,-1),(1,1)]
  12. if(x,y)in s:continue
  13. s+=(x,y),
  14. for u,v in M:
  15. p=(x+u,y+v);z=p in s;e-=~-z
  16. if V(m,*p)and~-z:q+=p,
  17. for u,v in D:
  18. p=(x+u,y+v);z=p in s;f+=~-z&all(((x+u+j,y+v+k)in s)^1for j,k in[(0,(v<1)*2-1),((u<1)*2-1,0)])
  19. if V(m,*p)and~-z:q+=p,
  20. return f-e+sum(map(sum,m))
  21.  
  22. import ast
  23. s="""[[1]] -> 1
  24. [[1,0,1]] -> 2
  25. [[1,0],[0,1]] -> 1
  26. [[0,1,0],[0,0,1],[1,1,1]] -> 1
  27. [[0,1,1,0],[1,0,1,1],[1,1,0,1],[0,1,1,0]] -> -1
  28. [[0,0,1,1,0],[0,1,1,1,1],[1,1,0,1,1],[0,1,1,0,0]] -> 0
  29. [[1,1,1,0,1,1,1],[1,0,1,0,1,0,1],[1,1,1,0,1,1,1]] -> 0
  30. [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]] -> 1"""
  31. for i in filter(None,s.split('\n')):a,b=map(ast.literal_eval,i.split(' -> '));assert f(a)==b
Success #stdin #stdout 0.16s 15888KB
stdin
Standard input is empty
stdout
Standard output is empty