Advertisement
SoggyCrunch

Snake Grid

May 21st, 2021
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. import sys
  2. n = sys.stdin.read()#Take input of one number
  3. n = int(n)
  4. matrix = [] #This is a empty matrix we will use later to add stuff to
  5. for i in range(n):
  6.     matrix.append([])
  7.     for j in range(n):
  8.         matrix[i].append([])#Fill the matrix with empty indexs. If this were java I wouldn't have had to do this
  9. order = []#This will become a list of where the numbers go. For example, the first thing in here will be 0,0 because 1 goes in spot 0,0
  10. for i in range(2*n+1):#Loop for 2n, so i can reach the last spot in the grid
  11.     temp = []
  12.     for y in range(i, -1, -1):#This chooses where y starts off
  13.         x = y-i
  14.         if abs(y) > n-1 or abs(x) > n-1:
  15.             continue
  16.         temp.append([abs(y),abs(x)])#Add the location
  17.     if i % 2 == 0:
  18.         temp.sort(reverse = True)#we want to go down on even
  19.     else:
  20.         temp.sort(reverse = False)#and up on odds
  21.     for j in range(len(temp)):#Add the indexs to order.
  22.         order.append(temp[j])
  23. for i in range(len(order)):#Assemble Order inside Matrix
  24.     matrix[order[i][0]][order[i][1]] = str(i+1)
  25. for i in range(len(matrix)):#Print matrix
  26.     print(" ".join(matrix[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement