티스토리 뷰
문제
https://www.acmicpc.net/problem/17140
풀이
R연산은 행을 정렬하는 것임.
그렇기때문에 각 행마다 딕셔너리를 이용해 원소의 개수를 정리해준 뒤, sorted메서드를 이용해서 (key,value)로 정렬해주었음.
정렬하고나면 각 원소의 길이가 다를 것임. 그래서 longCount라는 변수에 가장 긴 원소의 길이를 저장해주고, 각 행을 확인하며 longCount보다 짧을 시에 뒤에 0을 longCount - len(list_a[i])만큼 추가해주었음.
R연산보다 C연산이 살짝 더 까다로웠지만 큰 차이는 없음.
마찬가지로 각 열마다 정렬을 해주면되는데, list_a[0]의 길이가 뒷 부분에 오도록 해야함. i가 열을 의미하고 j가 행을 의미하는거임.
동일하게 딕셔너리를 이용해서 정리한 뒤 정렬해주었음.
다만 C연산의 temp_arr에 들어있는 원소는 각 열을 의미함. 그러므로 R연산처럼 그대로 냅두는 것이 아니라 원소 하나씩 모아서 다시 배열을 만들어야함. 당연히 longCount를 이용해 모자란 길이에 대해서는 0을 추가해주어야 인덱스 오류가 나지않음.
최대 100까지이므로 메인 반복문을 range(101)까지 반복해주어야함. 그리고 이로 인해 각 R,C연산에서 i가 100인 경우에는 건너뛰도록 해주었음.
import sys
input = sys.stdin.readline
r,c,k = map(int,input().split())
r -= 1
c -= 1
list_a = []
check = False
for _ in range(3):
list_a.append(list(map(int,input().split())))
def R(list):
global list_a
longCount = 0
for i in range(len(list_a)):
if i == 100:
break
temp = {}
for j in range(len(list_a[i])):
if list_a[i][j] == 0:
continue
if list_a[i][j] not in temp:
temp[list_a[i][j]] = 1
else:
temp[list_a[i][j]] += 1
temp_key = sorted(temp.items(), key=lambda item: (item[1],item[0]))
temp = []
for element in temp_key:
temp.append(element[0])
temp.append(element[1])
list_a[i] = temp
longCount = max(longCount,len(list_a[i]))
for i in range(len(list_a)):
if len(list_a[i]) < longCount:
for _ in range(longCount - len(list_a[i])):
list_a[i].append(0)
def C(list):
global list_a
longCount = 0
temp_arr = []
for i in range(len(list_a[0])):
if i == 100:
break
temp = {}
for j in range(len(list_a)):
if list_a[j][i] == 0:
continue
if list_a[j][i] not in temp:
temp[list_a[j][i]] = 1
else:
temp[list_a[j][i]] += 1
temp_key = sorted(temp.items(), key=lambda item: (item[1], item[0]))
temp = []
for element in temp_key:
temp.append(element[0])
temp.append(element[1])
temp_arr.append(temp)
longCount = max(longCount,len(temp))
for i in range(len(temp_arr)):
if len(temp_arr[i]) < longCount:
for _ in range(longCount - len(temp_arr[i])):
temp_arr[i].append(0)
new_arr = []
for i in range(longCount):
temp = []
for j in range(len(temp_arr)):
temp.append(temp_arr[j][i])
new_arr.append(temp)
list_a = new_arr
for i in range(101):
if len(list_a) > r and len(list_a[0]) > c:
if list_a[r][c] == k:
check = True
print(i)
break
row_count = len(list_a)
col_count = len(list_a[0])
if row_count >= col_count:
R(list_a)
else:
C(list_a)
if not check:
print(-1)
_____________________________________________________________________________________________________
시간이 짧게나와서 만족스러움
'PS' 카테고리의 다른 글
[Python] 백준_원판 돌리기(17822) (0) | 2023.03.24 |
---|---|
[Python] 백준_미세먼지 안녕!(17144) (0) | 2023.03.23 |
[Python] 백준_마법사 상어와 파이어스톰(20058) (0) | 2023.03.22 |
[Python] 백준_드래곤커브(15685) (0) | 2023.03.22 |
[Python] 백준_사다리조작(15684) (0) | 2023.03.18 |
- Total
- Today
- Yesterday