# Baekjoon-17140-이차원 배열과 연산

11 Oct 2022```
from struct import calcsize
r,c,k = map(int,input().split())
r,c = r-1,c-1
A = [list(map(int,input().split())) for _ in range(3)]
board = [[0]*100 for _ in range(100)]
for i in range(3):
for j in range(3):
board[i][j] = A[i][j]
max_row, max_col = 3,3
def cal_R():
global max_col
local_max_col = 0
for i in range(max_row):
row_dict = {}
for j in range(max_col):
if board[i][j] != 0:
if board[i][j] not in row_dict:
row_dict[board[i][j]] = 1
else:
row_dict[board[i][j]] += 1
row_keys = list(row_dict.keys())
row_keys.sort()
count_dict = {}
for num in row_keys:
count = row_dict[num]
if count not in count_dict:
count_dict[count] = [num]
else:
count_dict[count].append(num)
count_keys = list(count_dict.keys())
count_keys.sort()
idx_col = 0
for count in count_keys:
nums = count_dict[count]
for num in nums:
board[i][idx_col] = num
board[i][idx_col+1] = count
idx_col += 2
local_max_col = max(local_max_col, idx_col)
for j in range(idx_col, max_col):
board[i][j] = 0
max_col = local_max_col
def cal_C():
global max_row
local_max_row = 0
for j in range(max_col):
col_dict = {}
for i in range(max_row):
if board[i][j] != 0:
if board[i][j] not in col_dict:
col_dict[board[i][j]] = 1
else:
col_dict[board[i][j]] += 1
col_keys = list(col_dict.keys())
col_keys.sort()
count_dict = {}
for num in col_keys:
count = col_dict[num]
if count not in count_dict:
count_dict[count] = [num]
else:
count_dict[count].append(num)
count_keys = list(count_dict.keys())
count_keys.sort()
idx_row = 0
for count in count_keys:
nums = count_dict[count]
for num in nums:
board[idx_row][j] = num
board[idx_row+1][j] = count
idx_row += 2
local_max_row = max(local_max_row, idx_row)
for i in range(idx_row, max_row):
board[i][j] = 0
max_row = local_max_row
for result in range(101):
if board[r][c] == k:
print(result)
exit()
else:
if max_row>=max_col:
cal_R()
else:
cal_C()
# print('------')
# for i in range(max_row):
# print(*board[i][:max_col])
print(-1)
```