Advertisement
namemkazaza

O

Jan 18th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. def isZeros(matrix):
  2.     for char in matrix:
  3.         if 0 in char:
  4.             return True
  5.     return False
  6.  
  7.  
  8. def check():
  9.     global DOWNSIDE, UPSIDE, LEFTSIDE, RIGHTSIDE, DOWNDIAG, UPDIAG
  10.     if i == 0:
  11.         DOWNSIDE = False
  12.         UPSIDE = True
  13.     if i == N - 1:
  14.         DOWNSIDE = True
  15.         UPSIDE = False
  16.     if i != N - 1 and i != 0:
  17.         DOWNSIDE = False
  18.         UPSIDE = False
  19.     if j == 0:
  20.         LEFTSIDE = True
  21.         RIGHTSIDE = False
  22.     if j == M - 1:
  23.         RIGHTSIDE = True
  24.         LEFTSIDE = False
  25.     if j != M - 1 and j != 0:
  26.         LEFTSIDE = False
  27.         RIGHTSIDE = False
  28.  
  29.  
  30. N, M = map(int, input().split())
  31.  
  32. matrix = [[0] * M for _ in range(N)]
  33.  
  34. i = 0
  35. j = 0
  36.  
  37. UPSIDE = True
  38. DOWNSIDE = False
  39. LEFTSIDE = True
  40. RIGHTSIDE = False
  41. UPDIAG = False
  42. DOWNDIAG = True
  43.  
  44. num = 1
  45.  
  46. while isZeros(matrix):
  47.     if matrix[i][j] == 0:
  48.         matrix[i][j] = num
  49.         num += 1
  50.     if (i < N - 1 and LEFTSIDE and DOWNDIAG) or (i < N - 1 and RIGHTSIDE and UPDIAG):
  51.         i += 1
  52.         if LEFTSIDE:
  53.             DOWNDIAG = False
  54.             UPDIAG = True
  55.         if RIGHTSIDE:
  56.             DOWNDIAG = True
  57.             UPDIAG = False
  58.         check()
  59.         continue
  60.     elif (j < M - 1 and UPSIDE and UPDIAG) or (j < M - 1 and DOWNSIDE and DOWNDIAG):
  61.         j += 1
  62.         if UPSIDE:
  63.             DOWNDIAG = True
  64.             UPDIAG = False
  65.         if DOWNSIDE:
  66.             DOWNDIAG = False
  67.             UPDIAG = True
  68.         check()
  69.         continue
  70.     elif i > 0 and j < M - 1 and UPDIAG:
  71.         i -= 1
  72.         j += 1
  73.         check()
  74.         continue
  75.     elif i < N - 1 and j > 0 and DOWNDIAG:
  76.         i += 1
  77.         j -= 1
  78.         check()
  79.         continue
  80.  
  81. for char in matrix:
  82.     print(*char)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement