0927. dxdy(4)

sea·2023년 9월 27일
0

알고리즘

목록 보기
9/14

문제

n*n 크기의 정사각형에 숫자 1부터 순서대로 증가시키며 달팽이 모양으로 채우는 코드 작성.
달팽이 모양이란? 
왼쪽 위 모서리로 시작해 오른쪽, 아래쪽, 왼쪽, 위쪽 순서로 더이상 채울 곳 없을 때까지 회전하는 모양

생각해야 할 점

  1. 정답을 저장할 이차원 배열 활용
  2. dx dy를 활용해 가로 막혔을 때 움직이는 방향을 시계방향으로 90도 회전
  3. 도착 지점이 n을 벗어났을 때에 대한 코드 작성: 범위 함수 in_range
  4. arr[x][y] == 0이 아닐 경우에만 숫자 채우기
n = 4

answer = [
    [0] * n
    for _ in range(n)
]

dxs, dys = [0, 1, 0, -1], [1, 0, -1, 0]

x, y = 0, 0 # 시작은 (0, 0), 여기에서 x 관련 애들은 다 좌표값
dir_num = 0 # 오른쪽부터 시작

answer[x][y] = 1 # 초기값


def in_range(x, y):
    return 0 <= x and x < n and 0 <= y and y < n

# n & n 번 진행
for i in range(2, n * n + 1):
    # 현재 방향 dir 기준으로 그 다음 위치 값 계산
    nx, ny = x + dxs[dir_num], y + dys[dir_num]
    # 더 이상 나갈 수 없다면 시계 방향으로 90도 회전
    if not in_range(nx, ny) or answer[nx][ny] != 0:
        print(i, "번째 진행", ":", "범위를 넘는 nx, ny 확인:", nx, ny, "|", "혹은 0이 아닌 경우일 수도")
        dir_num = (dir_num + 1) % 4
        print("Condition*. IF NOT", "새롭게 지정된 dir_num", dir_num, "|", i, "번째 진행", "|", "dir_num방향:", dir_num, "|", "x, y:", x, y, "|", "새로운 nx, ny:", nx, ny)

    print(i, "번째 진행", ":", "dir_num방향:", dir_num, "/", "x, y:", x, y, "/", "새로운 nx, ny:", nx, ny)
    x, y = x + dxs[dir_num], y + dys[dir_num]
    answer[x][y] = i
    print(i, "번째 진행", ":", "dir_num방향:", dir_num, "/", "x, y:", x, y, "/", "새로운 nx, ny:", nx, ny)

    print(answer)
    
    
  
  >>> output
2 번째 진행 : dir_num방향: 0 / x, y: 0 0 / 새로운 nx, ny: 0 1
2 번째 진행 : dir_num방향: 0 / x, y: 0 1 / 새로운 nx, ny: 0 1
[[1, 2, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
3 번째 진행 : dir_num방향: 0 / x, y: 0 1 / 새로운 nx, ny: 0 2
3 번째 진행 : dir_num방향: 0 / x, y: 0 2 / 새로운 nx, ny: 0 2
[[1, 2, 3, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
4 번째 진행 : dir_num방향: 0 / x, y: 0 2 / 새로운 nx, ny: 0 3
4 번째 진행 : dir_num방향: 0 / x, y: 0 3 / 새로운 nx, ny: 0 3
[[1, 2, 3, 4], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
5 번째 진행 : 범위를 넘는 nx, ny 확인: 0 4 | 혹은 0이 아닌 경우일 수도
Condition*. IF NOT 새롭게 지정된 dir_num 1 | 5 번째 진행 | dir_num방향: 1 | x, y: 0 3 | 새로운 nx, ny: 0 4
5 번째 진행 : dir_num방향: 1 / x, y: 0 3 / 새로운 nx, ny: 0 4
5 번째 진행 : dir_num방향: 1 / x, y: 1 3 / 새로운 nx, ny: 0 4
[[1, 2, 3, 4], [0, 0, 0, 5], [0, 0, 0, 0], [0, 0, 0, 0]]
6 번째 진행 : dir_num방향: 1 / x, y: 1 3 / 새로운 nx, ny: 2 3
6 번째 진행 : dir_num방향: 1 / x, y: 2 3 / 새로운 nx, ny: 2 3
[[1, 2, 3, 4], [0, 0, 0, 5], [0, 0, 0, 6], [0, 0, 0, 0]]
7 번째 진행 : dir_num방향: 1 / x, y: 2 3 / 새로운 nx, ny: 3 3
7 번째 진행 : dir_num방향: 1 / x, y: 3 3 / 새로운 nx, ny: 3 3
[[1, 2, 3, 4], [0, 0, 0, 5], [0, 0, 0, 6], [0, 0, 0, 7]]
8 번째 진행 : 범위를 넘는 nx, ny 확인: 4 3 | 혹은 0이 아닌 경우일 수도
Condition*. IF NOT 새롭게 지정된 dir_num 2 | 8 번째 진행 | dir_num방향: 2 | x, y: 3 3 | 새로운 nx, ny: 4 3
8 번째 진행 : dir_num방향: 2 / x, y: 3 3 / 새로운 nx, ny: 4 3
8 번째 진행 : dir_num방향: 2 / x, y: 3 2 / 새로운 nx, ny: 4 3
[[1, 2, 3, 4], [0, 0, 0, 5], [0, 0, 0, 6], [0, 0, 8, 7]]
9 번째 진행 : dir_num방향: 2 / x, y: 3 2 / 새로운 nx, ny: 3 1
9 번째 진행 : dir_num방향: 2 / x, y: 3 1 / 새로운 nx, ny: 3 1
[[1, 2, 3, 4], [0, 0, 0, 5], [0, 0, 0, 6], [0, 9, 8, 7]]
10 번째 진행 : dir_num방향: 2 / x, y: 3 1 / 새로운 nx, ny: 3 0
10 번째 진행 : dir_num방향: 2 / x, y: 3 0 / 새로운 nx, ny: 3 0
[[1, 2, 3, 4], [0, 0, 0, 5], [0, 0, 0, 6], [10, 9, 8, 7]]
11 번째 진행 : 범위를 넘는 nx, ny 확인: 3 -1 | 혹은 0이 아닌 경우일 수도
Condition*. IF NOT 새롭게 지정된 dir_num 3 | 11 번째 진행 | dir_num방향: 3 | x, y: 3 0 | 새로운 nx, ny: 3 -1
11 번째 진행 : dir_num방향: 3 / x, y: 3 0 / 새로운 nx, ny: 3 -1
11 번째 진행 : dir_num방향: 3 / x, y: 2 0 / 새로운 nx, ny: 3 -1
[[1, 2, 3, 4], [0, 0, 0, 5], [11, 0, 0, 6], [10, 9, 8, 7]]
12 번째 진행 : dir_num방향: 3 / x, y: 2 0 / 새로운 nx, ny: 1 0
12 번째 진행 : dir_num방향: 3 / x, y: 1 0 / 새로운 nx, ny: 1 0
[[1, 2, 3, 4], [12, 0, 0, 5], [11, 0, 0, 6], [10, 9, 8, 7]]
13 번째 진행 : 범위를 넘는 nx, ny 확인: 0 0 | 혹은 0이 아닌 경우일 수도
Condition*. IF NOT 새롭게 지정된 dir_num 0 | 13 번째 진행 | dir_num방향: 0 | x, y: 1 0 | 새로운 nx, ny: 0 0
13 번째 진행 : dir_num방향: 0 / x, y: 1 0 / 새로운 nx, ny: 0 0
13 번째 진행 : dir_num방향: 0 / x, y: 1 1 / 새로운 nx, ny: 0 0
[[1, 2, 3, 4], [12, 13, 0, 5], [11, 0, 0, 6], [10, 9, 8, 7]]
14 번째 진행 : dir_num방향: 0 / x, y: 1 1 / 새로운 nx, ny: 1 2
14 번째 진행 : dir_num방향: 0 / x, y: 1 2 / 새로운 nx, ny: 1 2
[[1, 2, 3, 4], [12, 13, 14, 5], [11, 0, 0, 6], [10, 9, 8, 7]]
15 번째 진행 : 범위를 넘는 nx, ny 확인: 1 3 | 혹은 0이 아닌 경우일 수도
Condition*. IF NOT 새롭게 지정된 dir_num 1 | 15 번째 진행 | dir_num방향: 1 | x, y: 1 2 | 새로운 nx, ny: 1 3
15 번째 진행 : dir_num방향: 1 / x, y: 1 2 / 새로운 nx, ny: 1 3
15 번째 진행 : dir_num방향: 1 / x, y: 2 2 / 새로운 nx, ny: 1 3
[[1, 2, 3, 4], [12, 13, 14, 5], [11, 0, 15, 6], [10, 9, 8, 7]]
16 번째 진행 : 범위를 넘는 nx, ny 확인: 3 2 | 혹은 0이 아닌 경우일 수도
Condition*. IF NOT 새롭게 지정된 dir_num 2 | 16 번째 진행 | dir_num방향: 2 | x, y: 2 2 | 새로운 nx, ny: 3 2
16 번째 진행 : dir_num방향: 2 / x, y: 2 2 / 새로운 nx, ny: 3 2
16 번째 진행 : dir_num방향: 2 / x, y: 2 1 / 새로운 nx, ny: 3 2
[[1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7]]
profile
달려가는중

0개의 댓글