Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- n = sys.stdin.read()#Take input of one number
- n = int(n)
- matrix = [] #This is a empty matrix we will use later to add stuff to
- for i in range(n):
- matrix.append([])
- for j in range(n):
- matrix[i].append([])#Fill the matrix with empty indexs. If this were java I wouldn't have had to do this
- 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
- for i in range(2*n+1):#Loop for 2n, so i can reach the last spot in the grid
- temp = []
- for y in range(i, -1, -1):#This chooses where y starts off
- x = y-i
- if abs(y) > n-1 or abs(x) > n-1:
- continue
- temp.append([abs(y),abs(x)])#Add the location
- if i % 2 == 0:
- temp.sort(reverse = True)#we want to go down on even
- else:
- temp.sort(reverse = False)#and up on odds
- for j in range(len(temp)):#Add the indexs to order.
- order.append(temp[j])
- for i in range(len(order)):#Assemble Order inside Matrix
- matrix[order[i][0]][order[i][1]] = str(i+1)
- for i in range(len(matrix)):#Print matrix
- print(" ".join(matrix[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement