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. ํฉ๊ฒฉ์๊ฐ ๋๋ ๋ชจ์ ํ
์คํธ