Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rows, cols = map(int, input().split())
- mtrx = [[0 for _ in range(cols)] for __ in range(rows)]
- mtrx[0][0] = 1
- ro, co = 0, 1
- row, col = 0, 0
- for n in range(2, cols * rows + 1):
- if 0 <= row + ro < rows and 0 <= col + co < cols \
- and mtrx[row + ro][col + co] == 0:
- row += ro
- col += co
- mtrx[row][col] = n
- continue
- if ro and (not 0 <= row + ro < rows or mtrx[row + ro][col]):
- co = -1 if ro == 1 else 1
- ro = 0
- if co and (not 0 <= col + co < cols or mtrx[row][col + co]):
- ro = 1 if co == 1 else -1
- co = 0
- row += ro
- col += co
- mtrx[row][col] = n
- for row in mtrx:
- print(*row)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement