T-coding
백준 1913 달팽이 / Python 본문
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
'Baekjoon' 카테고리의 다른 글
백준 3024 마라톤 틱택토 / Python (0) | 2023.03.19 |
---|---|
백준 2784 가로 세로 퍼즐 / Python (0) | 2023.03.18 |
백준 1074 Z / Python (0) | 2023.03.18 |
백준 1029 그림 교환 / Python (0) | 2023.03.18 |
백준 1022 소용돌이 예쁘게 출력하기 / Python (0) | 2023.03.18 |