Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dirs = [
- (0, 1), #RIGHT
- (1, 0), #DOWN
- (0, -1), #LEFT
- (-1, 0) #Up
- ]
- dim = 5
- pos = [0, -1] #starting point
- target = dim**2
- matrix = [[0]*dim for _ in range(dim)]
- def withinBorders(x, y):
- px, py, = pos[0]+x, pos[1]+y
- if not (0 <= px < dim): return False
- if not (0 <= py < dim): return False
- if isinstance(matrix[px][py], str): return False
- return True
- while target:
- (x, y), *dirs, = dirs
- for _ in range(dim):
- if not withinBorders(x, y): break
- pos[0] += x
- pos[1] += y
- px, py = pos
- matrix[px][py] = f'0{target}'[-2:]
- target -= 1
- dirs = [*dirs, (x, y)]
- for r in matrix:
- print(*r)
Advertisement
Add Comment
Please, Sign In to add comment