classSolution(object):deforangesRotting(self, grid):"""
:type grid: List[List[int]]
:rtype: int
"""
R, C =len(grid),len(grid[0])# queue - all starting cells with rotting oranges
queue = collections.deque()for r, row inenumerate(grid):for c, val inenumerate(row):if val ==2:
queue.append((r, c,0))defneiors(r, c):for nr, nc in((r-1,c),(r,c-1),(r+1,c),(r,c+1)):if0<= nr < R and0<= nc < C:yield nr, nc
d =0while queue:
r, c, d = queue.popleft()for nr, nc in neiors(r, c):if A[nr][nc]==1:
A[nr][nc]=2
queue.append((nr, nc, d+1))ifany(1in row for row in grid):return-1return d