fork download
  1. n = int(input())
  2.  
  3. dx, dy = 1, 0
  4. x, y = 0, 0
  5. arr = [[None] * n for _ in range(n)]
  6. for i in range(1, n**2+1):
  7. arr[x][y] = i
  8. nx, ny = x+dx, y+dy
  9. if 0 <= nx < n and 0 <= ny < n and not arr[nx][ny]:
  10. x, y = nx, ny
  11. else:
  12. dx, dy = -dy, dx
  13. x = x + dx
  14. y = y + dy
  15.  
  16. for y in range(n):
  17. for x in range(n):
  18. print(arr[x][y], end = ' ')
  19. print()
Runtime error #stdin #stdout #stderr 0.02s 28384KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 1, in <module>
EOFError: EOF when reading a line