Today
Total
07-05 02:35
관리 메뉴

T-coding

백준 1913 달팽이 / Python 본문

Baekjoon

백준 1913 달팽이 / Python

Tcoding 2023. 3. 18. 22:27
728x90
728x90

https://www.acmicpc.net/problem/1913

 

1913번: 달팽이

N개의 줄에 걸쳐 표를 출력한다. 각 줄에 N개의 자연수를 한 칸씩 띄어서 출력하면 되며, 자릿수를 맞출 필요가 없다. N+1번째 줄에는 입력받은 자연수의 좌표를 나타내는 두 정수를 한 칸 띄어서

www.acmicpc.net

 

 

코드

더보기
n = int(input())
num = int(input())
dx = [0, 1, 0, -1]
dy = [-1, 0, 1, 0]

arr = [[0 for _ in range(n)] for _ in range(n)]
direction = 0
cnt = 1
rep = 1
x, y = n // 2, n // 2
ans_y, ans_x = 0, 0
while cnt <= n ** 2:
    for _ in range(2):
        for _ in range(rep):
            if cnt <= n ** 2:
                arr[y][x] = cnt
                x += dx[direction]
                y += dy[direction]
                cnt += 1
        direction = (direction + 1) % 4
    rep += 1

for i in range(n):
    for j in range(n):
        print(arr[i][j], end=' ')
        if arr[i][j] == num:
            ans_y = i
            ans_x = j
    print()
print(ans_y + 1, ans_x + 1)
728x90
300x250