๐Ÿ’ป [์ฝ”ํ…Œ09] 14์žฅ ์‹œ๋ฎฌ๋ ˆ์ด์…˜

๊น€๋ฏธ์—ฐยท2024๋…„ 3์›” 18์ผ
0

14์žฅ. ์‹œ๋ฎฌ๋ ˆ์ด์…˜

14-1. ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ ํ’€์ด ๋…ธํ•˜์šฐ

  • ์‹œ๋ฎฌ๋ ˆ์ด์…˜ : ๋ฌธ์ œ์— ์ฃผ์–ด์ง„ ์ƒํ™ฉ์„ ์™„๋ฒฝํ•˜๊ฒŒ ์ดํ•ดํ•˜๊ณ  ์ด๋ฅผ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ณผ์ •
  • ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์„ฑ๋Šฅ์— ์ค‘์ ์„ ๋‘” ๋ฐ˜๋ฉด, ์‹œ๋ฎฌ๋ ˆ์ด์…˜์€ ๊ตฌํ˜„์— ์ค‘์ 

1) ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•

  • ์ ‘๊ทผ๋ฐฉ๋ฒ•1) ํ•˜๋‚˜์˜ ๋ฌธ์ œ๋ฅผ ์ตœ๋Œ€ํ•œ ์—ฌ๋Ÿฌ ๊ฐœ๋กœ ๋ถ„๋ฆฌ
  • ์ ‘๊ทผ๋ฐฉ๋ฒ•2) ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด ๋…๋ฆฝ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„

2) ํ–‰๋ ฌ ์—ฐ์‚ฐ

  • ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ์— ๋งŽ์ด ํ™œ์šฉํ•˜๋Š” ๊ธฐ๋ฒ•
  • ํ–‰๋ ฌ ๋ง์…ˆ๊ณผ ๋บ„์…ˆ, ๊ทธ๋ฆฌ๊ณ  ๊ณฑ์…ˆ
  • ์ „์น˜ ํ–‰๋ ฌ

3) ์ขŒํ‘œ ์—ฐ์‚ฐ

  • ์ขŒํ‘œ ๋ฐฐ์—ด๋กœ ํ‘œํ˜„ํ•˜๊ธฐ
  • ์ขŒํ‘œ ์ด๋™ ์˜คํ”„์…‹๊ฐ’์œผ๋กœ ์‰ฝ๊ฒŒ ํ‘œํ˜„ํ•˜๊ธฐ

4) ๋Œ€์นญ, ํšŒ์ „ ์—ฐ์‚ฐ

  • ์ขŒ์šฐ ๋Œ€์นญ : A[i, j] = A[i, (N-1)-j]
  • 90๋„ ํšŒ์ „(๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ) : A[i, j] = A[j, (N-1)-i]

14-2. ๋ชธํ’€๊ธฐ ๋ฌธ์ œ

  • ๋ฌธ์ œ62_๋ฐฐ์—ด ํšŒ์ „ํ•˜๊ธฐ
def rotation_90(arr):
    arr_size = len(arr)
    result = [[0]*arr_size for _ in range(arr_size)]
    for i in range(arr_size):
        for j in range(arr_size):
            result[i][j] = arr[arr_size-1-j][i]
    return result

def solution(arr, n):
    rotated_arr = arr.copy()
    for _ in range(n):
        rotated_arr = rotation_90(rotated_arr)
    return rotated_arr

# TEST ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค. ์ฃผ์„์„ ํ’€๊ณ  ์‹คํ–‰์‹œ์ผœ๋ณด์„ธ์š”

print(solution(
[
 [1, 2, 3, 4],
 [5, 6, 7, 8],
 [9, 10, 11, 12],
 [13, 14, 15, 16]
], 1))


'''
๋ฐ˜ํ™˜๊ฐ’ : 
[
[13, 9, 5, 1],
[14, 10, 6, 2],
[15, 11, 7, 3],
[16, 12, 8, 4]
]
'''
    

print(solution(
[
 [1, 2, 3, 4],
 [5, 6, 7, 8],
 [9, 10, 11, 12],
 [13, 14,15,16]
 ], 2))


'''
๋ฐ˜ํ™˜๊ฐ’ : 
[
[16, 15, 14, 13],
[12, 11, 10, 9],
[8, 7, 6, 5],
[4, 3, 2, 1]
]
'''
  • ๋ฌธ์ œ63_๋‘ ํ–‰๋ ฌ์„ ๊ณฑํ•œ ํ›„ ์ „์น˜ ํ–‰๋ ฌ ๋งŒ๋“ค๊ธฐ
def mutiply_matrix(matrix1, matrix2):
    result = [[0]*3 for _ in range(3)]
    for i in range(3):
        for j in range(3):
            for k in range(3):
                result[i][j] += matrix1[i][k] * matrix2[k][j]
    return result


def transpose_matrix(matrix):
    result = [[0]*3 for _ in range(3)]
    for i in range(3):
        for j in range(3):
            result[i][j] = matrix[j][i]
    return result    


def solution(matrix1, matrix2):
    result = mutiply_matrix(matrix1, matrix2)
    result = transpose_matrix(result)
    return result

# TEST ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค. ์ฃผ์„์„ ํ’€๊ณ  ์‹คํ–‰์‹œ์ผœ๋ณด์„ธ์š”

print(solution(
[
 [1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]
],

[
 [9, 8, 7],
 [6, 5, 4],
 [3, 2, 1]
]))


'''
๋ฐ˜ํ™˜๊ฐ’ : 
[
 [30, 84, 138],
 [24, 69, 114],
 [18, 54, 90]
]
'''


print(solution(
[
 [2, 4, 6],
 [1, 3, 5],
 [7, 8, 9]
],

[
 [9, 1, 2],
 [4, 5, 6],
 [7, 3, 8]
]))


'''
๋ฐ˜ํ™˜๊ฐ’ : 
[
 [76, 56, 158],
 [40, 31, 74],
 [76, 60, 134]
]
'''
  • ๋ฌธ์ œ64_๋‹ฌํŒฝ์ด ์ˆ˜์—ด ๋งŒ๋“ค๊ธฐ
def is_not_in_box(row, col, n):
    return (row<0 or row>=n or col<0 or col>=n)

def solution(n):
    sequence = [[0]*n for _ in range(n)]
    move = [[0, 1], [1, 0], [0, -1], [-1, 0]]
    num_move = 0
    row, col = 0, 0
    sequence[row][col] = 1
    drow, dcol = move[num_move]
    
    for i in range(2, n*n+1):
        next_row, next_col = row + drow, col + dcol
        if is_not_in_box(next_row, next_col, n) or (sequence[next_row][next_col] != 0):
            num_move += 1
            num_move %= 4
            drow, dcol = move[num_move]
            next_row, next_col = row + drow, col + dcol
            
        row, col = next_row, next_col
        
        sequence[row][col] = i
    return sequence

# TEST ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค. ์ฃผ์„์„ ํ’€๊ณ  ์‹คํ–‰์‹œ์ผœ๋ณด์„ธ์š”
print(solution(3))

'''
๋ฐ˜ํ™˜๊ฐ’ : 
[
 [1, 2, 3],
 [8, 9, 4],
 [7, 6, 5]
]
'''

print(solution(4))

'''
๋ฐ˜ํ™˜๊ฐ’ : 
[
 [1, 2, 3, 4],
 [12, 13, 14, 5],
 [11, 16, 15, 6],
 [10, 9, 8, 7]
]
'''

14-3. ํ•ฉ๊ฒฉ์ž๊ฐ€ ๋˜๋Š” ๋ชจ์˜ ํ…Œ์ŠคํŠธ

0๊ฐœ์˜ ๋Œ“๊ธ€