Spiral Matrix


a = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
b = []
nrow = len(a)
ncol = len(a[0])
x,y,di = 0, 0, 0
seen = [[False]*ncol for i in range(nrow)]r = [0,1,0,1]
c = [1,0,1,0]for i in range(nrow*ncol):
b.append(a[x][y])
seen[x][y] = True
x = x+r[di]
y = y+c[di]
if 0 <= x < nrow and 0<=y<ncol and not seen[x][y]:
pass
else:
x = xr[di]
y = yc[di]
di = (di+1)%4
x, y = x + r[di],y + c[di]
return b