반응형
from copy import deepcopy


n= int(input())
q = int(input())
temp_ans = [[0 for _ in range(n)] for k in range(n)  ]

center = n//2

temp_ans[center][center]=1
temp_ans[center-1][center]=2
temp_ans[center-1][center+1]=3
temp_ans[center][center+1]=4
temp_ans[center+1][center+1]=5
temp_ans[center+1][center]=6
temp_ans[center+1][center-1]=7
temp_ans[center][center-1]=8
temp_ans[center-1][center-1]=9
temp_list =[(center,center), (center-1,center), (center-1,center+1), (center,center+1),(center+1,center+1), (center+1,center),(center+1,center-1),(center,center-1),(center-1,center-1)]
now= (center-1, center-1)

root ="up"
if n>3:
  for i in range(10, n**2+1):
    if root =="up":
      if temp_ans[now[0]][ now[1]+1] !=0:
        temp_ans[now[0]-1][now[1]] = i
        now = (now[0]-1, now[1])
      else:
        temp_ans[now[0]][now[1]+1] = i
        now = (now[0], now[1]+1)
        root="right"
      j,k = now
      temp_list.append( (j,k))
      continue
    if root =="right":
      if temp_ans[now[0]+1][ now[1]] !=0:
        temp_ans[now[0]][now[1]+1] = i
        now = (now[0], now[1]+1)

      else:
        temp_ans[now[0]+1][now[1]] = i
        now = (now[0]+1, now[1])

        root="down"
      j,k = now
      temp_list.append( (j,k))
      continue
    if root =="down":
      if temp_ans[now[0]][ now[1]-1] !=0:
        temp_ans[now[0]+1][now[1]] = i
        now = (now[0]+1, now[1])

      else:
        temp_ans[now[0]][now[1]-1] = i
        root="left"
        now = (now[0], now[1]-1)
      j,k = now
      temp_list.append( (j,k))
      continue 
    if root =="left":
      if temp_ans[now[0]-1][ now[1]] !=0:
        temp_ans[now[0]][now[1]-1] = i
        now = (now[0], now[1]-1)

      else:
        temp_ans[now[0]-1][now[1]] = i
        root="up"
        now = (now[0]-1, now[1])
      j,k = now
      temp_list.append( (j,k))
      continue 


for row in temp_ans:
  for col in row:
    print(col, end=" ")
  print(sep='\n')

j,k =  temp_list[q-1]
print(j+1, k+1)
반응형

+ Recent posts