url : https://www.codetree.ai/training-field/frequent-problems/problems/matrix-number-play/description?page=1&pageSize=20&order=tier
Level : Gold 4
유형 : simulation
r,c,k = map(int, input().split())
A = []
for i in range(3):
A.append(list(map(int, input().split())))
# 횟수 계산
def calculate(list_):
dict_ = dict()
for li in list_:
if li == 0: continue
if li in dict_:
cnt = dict_[li]
dict_[li] = cnt + 1
else:
dict_[li] = 1
return dict_
# 0으로 채우기
def fill_zero(tot, max_):
result = []
for t in tot:
while len(t) < max_:
t.append(0)
result.append(t)
return result
# 행 계산
def column():
max_ = -1
tot = []
for a in A:
dict_ = calculate(a)
ans = []
for d in dict_:
ans.append([d, dict_[d]])
ans.sort(key = lambda x: (x[1], x[0]))
tmp = []
for an in ans:
a,b = an
tmp.append(a)
tmp.append(b)
if len(tmp) > 100:
tmp = tmp[:100]
tot.append(tmp)
max_ = max(max_, len(tmp))
fill_zero(tot, max_)
return tot
# 열 계산
def row():
max_ = -1
tot = []
for a in map(list, zip(*A)):
dict_ = calculate(a)
ans = []
for d in dict_:
ans.append([d, dict_[d]])
ans.sort(key = lambda x: (x[1], x[0]))
tmp = []
for an in ans:
a,b = an
tmp.append(a)
tmp.append(b)
if len(tmp) > 100:
tmp = tmp[:100]
tot.append(tmp)
max_ = max(max_, len(tmp))
fill_zero(tot, max_)
return list(map(list, zip(*tot)))
cnt = 0
while True:
if c <= len(A[0]) and r <= len(A):
if k == A[r-1][c-1]:
print(cnt)
break
cnt += 1
if cnt > 100:
print(-1)
break
if len(A) >= len(A[0]):
A = column()
else:
A = row()
조건 체크 :
1. 행(column) >= 열(row) 일 경우
구해야하는 값 : A[r][c] 값이 k가 되기 위한 최소 시간 출력
항상 이런문제를 풀때 헷깔리는 것이 열과 행은 0부터 시작하는 데, 주어지는 값은 1부터 시작을 해서 범위체크랑 값을 출력할 때 에러가 난다.
-> 해결 : 주어지는 값을 1씩 빼고, 길이는 꼭 길이끼리 비교하기