Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def isZeros(matrix):
- for char in matrix:
- if 0 in char:
- return True
- return False
- def check():
- global DOWNSIDE, UPSIDE, LEFTSIDE, RIGHTSIDE, DOWNDIAG, UPDIAG
- if i == 0:
- DOWNSIDE = False
- UPSIDE = True
- if i == N - 1:
- DOWNSIDE = True
- UPSIDE = False
- if i != N - 1 and i != 0:
- DOWNSIDE = False
- UPSIDE = False
- if j == 0:
- LEFTSIDE = True
- RIGHTSIDE = False
- if j == M - 1:
- RIGHTSIDE = True
- LEFTSIDE = False
- if j != M - 1 and j != 0:
- LEFTSIDE = False
- RIGHTSIDE = False
- N, M = map(int, input().split())
- matrix = [[0] * M for _ in range(N)]
- i = 0
- j = 0
- UPSIDE = True
- DOWNSIDE = False
- LEFTSIDE = True
- RIGHTSIDE = False
- UPDIAG = False
- DOWNDIAG = True
- num = 1
- while isZeros(matrix):
- if matrix[i][j] == 0:
- matrix[i][j] = num
- num += 1
- if (i < N - 1 and LEFTSIDE and DOWNDIAG) or (i < N - 1 and RIGHTSIDE and UPDIAG):
- i += 1
- if LEFTSIDE:
- DOWNDIAG = False
- UPDIAG = True
- if RIGHTSIDE:
- DOWNDIAG = True
- UPDIAG = False
- check()
- continue
- elif (j < M - 1 and UPSIDE and UPDIAG) or (j < M - 1 and DOWNSIDE and DOWNDIAG):
- j += 1
- if UPSIDE:
- DOWNDIAG = True
- UPDIAG = False
- if DOWNSIDE:
- DOWNDIAG = False
- UPDIAG = True
- check()
- continue
- elif i > 0 and j < M - 1 and UPDIAG:
- i -= 1
- j += 1
- check()
- continue
- elif i < N - 1 and j > 0 and DOWNDIAG:
- i += 1
- j -= 1
- check()
- continue
- for char in matrix:
- print(*char)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement