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)
  12. if(x,y)not in s:
  13. s+=(x,y),
  14. for p in[(x,y+1),(x,y-1),(x+1,y),(x-1,y)]:z=p in s;e-=~-z;q+=(V(m,*p)!=0and~-z)*[p]
  15. for u,v in[(1,1),(1,-1),(-1,1),(-1,-1)]:p=(x-u,y-v);z=p in s;f+=~-z&~-((x-u,y)in s)*~-((x,y-v)in s);q+=(V(m,*p)!=0and~-z)*[p]
  16. return f-e+sum(map(sum,m))
  17.  
  18. import ast
  19. s="""[[1]] -> 1
  20. [[1,0,1]] -> 2
  21. [[1,0],[0,1]] -> 1
  22. [[0,1,0],[0,0,1],[1,1,1]] -> 1
  23. [[0,1,1,0],[1,0,1,1],[1,1,0,1],[0,1,1,0]] -> -1
  24. [[0,0,1,1,0],[0,1,1,1,1],[1,1,0,1,1],[0,1,1,0,0]] -> 0
  25. [[1,1,1,0,1,1,1],[1,0,1,0,1,0,1],[1,1,1,0,1,1,1]] -> 0
  26. [[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"""
  27. for i in filter(None,s.split('\n')):a,b=map(ast.literal_eval,i.split(' -> '));assert f(a)==b
Success #stdin #stdout 0.18s 15932KB
stdin
Standard input is empty
stdout
Standard output is empty