Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- n=int(input('Введите n:\n'))
- matrix=[[0 for x in range(n+2)] for x in range(n+2)]
- for x in range(n+2):
- matrix[0][x]=-1
- matrix[n+1][x]=-1
- matrix[x][0]=-1
- matrix[x][n+1]=-1
- x=1
- y=1
- direction='right'
- num=1
- matrix[1][1]=1
- while num<n**2:
- if direction=='right':
- if matrix[x][y+1]==0:
- num+=1
- matrix[x][y+1]=num
- y=y+1
- continue
- else:
- direction='down'
- continue
- elif direction=='left':
- if matrix[x][y-1]==0:
- num+=1
- matrix[x][y-1]=num
- y=y-1
- continue
- else:
- direction='up'
- continue
- elif direction=='down':
- if matrix[x+1][y]==0:
- num+=1
- matrix[x+1][y]=num
- x=x+1
- continue
- else:
- direction='left'
- continue
- elif direction=='up':
- if matrix[x-1][y]==0:
- num+=1
- matrix[x-1][y]=num
- x=x-1
- continue
- else:
- direction='right'
- continue
- new_str=''
- for x in matrix[1:n+1]:
- for y in x[1:n+1]:
- new_str+=(' '+str(y))
- print(new_str)
- new_str=''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement