#создаем матирицу ( список списков)
k = int(input())
mas= [[0]*k]*k
count = 0 # счетчик
while True:
while ????:
# тут блок движения в право
while ????:
# тут блок движения в вниз по правой строне
while ????:
# тут блок движения в верх
while ????:
# тут блок движения в право ?????????? что бы заполнить центр
from itertools import count, cycle
def distances(n):
yield n-1
for i in range(n-1, 0, -1):
yield i
yield i
yield i
def directions(di=0, dj=1):
while True:
yield di, dj
di, dj = dj, -di
def matrix(n=3, ival=count(1), fdir=directions, fdist=distances, i=0, j=0):
m = [[0]*n for i in range(n)]
for (di, dj), distance in zip(fdir(), fdist(n)):
for step in range(distance):
m[i][j] = next(ival)
i += di
j += dj
return m
for row in matrix(9):
print(*map('{:02d}'.format, row))
from itertools import count, cycle, chain, tee
def distances(n):
return chain([n-1], *zip(*tee(range(n-1, 0, -1), 2)), [1])
def directions():
return cycle([(0, 1), (1, 0), (0, -1), (-1, 0)])
01 02 03 04 05 06 07 08 09
32 33 34 35 36 37 38 39 10
31 56 57 58 59 60 61 40 11
30 55 72 73 74 75 62 41 12
29 54 71 80 81 76 63 42 13
28 53 70 79 78 77 64 43 14
27 52 69 68 67 66 65 44 15
26 51 50 49 48 47 46 45 16
25 24 23 22 21 20 19 18 17
'''
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 ]