Skip to content

Commit fd02171

Browse files
committed
[#4]1913 달팽이
1 parent 16fa0e5 commit fd02171

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
n = int(input())
2+
target = int(input())
3+
4+
arr = [[0] * n for _ in range(n)]
5+
6+
# 규칙 상 -> 우 -> 하 -> 좌
7+
dx = [-1, 0, 1, 0]
8+
dy = [0, 1, 0, -1]
9+
10+
x, y = n//2, n//2
11+
arr[x][y] = 1
12+
target_x, target_y = x + 1, y + 1
13+
num = 2 # 다음 채울 숫자
14+
move_count = 1 # 이동 횟수
15+
i = 0 # dx,dy index
16+
17+
# 규칙: 상(1) → 우(1) → 하(2) → 좌(2) → 상(3) → 우(3) → 하(4) → 좌(4) ...
18+
while num <= n * n:
19+
for _ in range(2):
20+
for _ in range(move_count):
21+
x, y = x+dx[i], y+dy[i]
22+
23+
if (0 <= x < n) and (0 <= y < n):
24+
arr[x][y] = num
25+
if num == target:
26+
target_x, target_y = x + 1, y + 1
27+
num += 1
28+
29+
i = (i + 1) % 4
30+
31+
move_count += 1
32+
33+
for row in arr:
34+
print(*row)
35+
36+
print(target_x, target_y)

0 commit comments

Comments
 (0)