💡문제접근
- 가장 정중앙의 값을 1로 놓고 탐색하는 방법과 [0, 0] 즉, 왼쪽 상단에서부터 순차적으로 탐색하는 방법 두 가지가 존재하는데 후자의 방법을 선택했다.
- 다른 사람이 작성한 코드의 효율성을 확인해봤는데 확연히 차이가 드러나는 것 같다.
💡코드(메모리 : 70040KB, 시간 : 1276ms)
import sys
input = sys.stdin.readline
N = int(input())
digit_number = int(input())
table = [[0] * N for _ in range(N)]
current_number = N * N
x, y = 0, 0
direction = 0
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
while current_number >= 1:
table[y][x] = current_number
if current_number == digit_number:
target_x, target_y = x, y
next_x, next_y = x + dx[direction], y + dy[direction]
if (next_x < 0 or next_x >= N or next_y < 0 or next_y >= N) or table[next_y][next_x] != 0:
direction = (direction + 1) % 4
x, y = x + dx[direction], y + dy[direction]
current_number -= 1
for row in table:
print(" ".join(map(str, row)))
for i in range(N):
for j in range(N):
if table[i][j] == digit_number:
print(i+1, j+1)
break
💡소요시간 : 39m