티스토리 뷰

문제

 

https://www.acmicpc.net/problem/17140

 

17140번: 이차원 배열과 연산

첫째 줄에 r, c, k가 주어진다. (1 ≤ r, c, k ≤ 100) 둘째 줄부터 3개의 줄에 배열 A에 들어있는 수가 주어진다. 배열 A에 들어있는 수는 100보다 작거나 같은 자연수이다.

www.acmicpc.net

 

 

풀이

 

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)

 

 

_____________________________________________________________________________________________________

 

시간이 짧게나와서 만족스러움

댓글
최근에 올라온 글
Total
Today
Yesterday