'''
Created on 3 окт. 2016 г.
@author: leah
'''
from collections import deque
k = 10
mas= [[None]*k for i in range(k)]
point = (0,0)
route = deque([lambda x,y: (x+1,y), lambda x,y: (x,y+1), lambda x,y: (x-1,y), lambda x,y: (x,y-1)])
count = 0
while(True):
x,y = point # current point
if mas[x][y] == None:
mas[x][y] = count
count = count +1
else:
break # done
_x,_y = route[0](*point) # next point
if (_x >= k) or (_y >= k) or (_x < 0) or (_y < 0): # check arrays overflow
route.rotate(-1) #change direction
elif mas[_x][_y] != None:
route.rotate(-1) #change direction
point = route[0](*point)
for i in mas:
print(" ".join(str(i)))
[ 0 , 3 5 , 3 4 , 3 3 , 3 2 , 3 1 , 3 0 , 2 9 , 2 8 , 2 7 ]
[ 1 , 3 6 , 6 3 , 6 2 , 6 1 , 6 0 , 5 9 , 5 8 , 5 7 , 2 6 ]
[ 2 , 3 7 , 6 4 , 8 3 , 8 2 , 8 1 , 8 0 , 7 9 , 5 6 , 2 5 ]
[ 3 , 3 8 , 6 5 , 8 4 , 9 5 , 9 4 , 9 3 , 7 8 , 5 5 , 2 4 ]
[ 4 , 3 9 , 6 6 , 8 5 , 9 6 , 9 9 , 9 2 , 7 7 , 5 4 , 2 3 ]
[ 5 , 4 0 , 6 7 , 8 6 , 9 7 , 9 8 , 9 1 , 7 6 , 5 3 , 2 2 ]
[ 6 , 4 1 , 6 8 , 8 7 , 8 8 , 8 9 , 9 0 , 7 5 , 5 2 , 2 1 ]
[ 7 , 4 2 , 6 9 , 7 0 , 7 1 , 7 2 , 7 3 , 7 4 , 5 1 , 2 0 ]
[ 8 , 4 3 , 4 4 , 4 5 , 4 6 , 4 7 , 4 8 , 4 9 , 5 0 , 1 9 ]
[ 9 , 1 0 , 1 1 , 1 2 , 1 3 , 1 4 , 1 5 , 1 6 , 1 7 , 1 8 ]