https://www.codetree.ai/missions/5/problems/snail-number-square-2/description
import sys
input = sys.stdin.readline
n, m = map(int,input().split())
graph = [[0 for _ in range(m)] for _ in range(n)]
# S -> E -> N -> W
# dx = [0, 1, 0, -1]
# dy = [1, 0, -1, 0]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
d = 0
x = 0
y = 0
for i in range(1, n*m+1):
graph[x][y] = i
nx = x + dx[d]
ny = y + dy[d]
if not (0 <= nx < n and 0 <= ny < m and graph[nx][ny] == 0):
d = (d + 1) % 4
nx = x + dx[d]
ny = y + dy[d]
x = nx
y = ny
for row in graph:
print(*row)
728x90
반응형
'🔻PS > Codetree' 카테고리의 다른 글
[Codetree] 숫자의 개수 Python (0) | 2024.08.07 |
---|---|
[Codetree] 빙빙 돌며 사각형 채우기 Python (0) | 2024.08.06 |
[Codetree] 격자 위의 편안한 상태 Python (0) | 2024.08.06 |
[Codetree] 이상한 진수 2 Python (0) | 2024.08.06 |
[Codetree] 최고의 13위치 Python (0) | 2024.08.05 |