[백준 17140번] 이차원 배열과 연산 - Python으로 구현하기
문제 접근
- 행과 열에 대한 연산을 구현해야 되는 경우에는 행에 대한 연산 구현 후 transpose 하여 열도 처리한다.
- 주어진 조건이 많으므로 차근차근히 구현해야한다.
- 행의 정수 빈도 수는 collections 모듈의 Counter를 사용하자.
생각해볼 점
- transpose하지 않고 구하기
오답 분석
- Counting 할때 0을 제거하지 않으면 오답!
- 100개까지만 취급하는 조건 주의
- time이 100인 경우에도 확인 해야한다.
from collections import Counter
r,c,k=map(int,input().split())
A = [list(map(int,input().split())) for _ in range(3)]
r-=1 # 0 index
c-=1
time=0 # 시간
while time<=100:
if r<len(A) and c<len(A[0]) and A[r][c]==k: # 정답 확인
print(time)
break
# C연산인 경우 : transpose
C_flag=False
if len(A)<len(A[0]):
C_flag=True
A=list(zip(*A))
# row에 대한 주어진 연산 구현
max_len=0
tmp_a = []
for now_row in A:
ct = Counter(now_row)
if ct.get(0): # 0은 카운팅하지 않음!
del ct[0]
num_cnt = list(map(list,ct.items()))
num_cnt.sort(key=lambda x :(x[1],x[0])) # 주어진 순서대로 정렬
tmp_a.append(list(sum(num_cnt,[]))[:100]) # 100개를 초과하는 경우 100개 까지만
max_len=max(max_len,len(tmp_a[-1]))
for i in range(len(tmp_a)):
if len(tmp_a[i])<max_len:
tmp_a[i]+=[0]*(max_len-len(tmp_a[i])) # 길이 맞춰주기
A=tmp_a
if C_flag: # C연산 인경우 다시 transpose
A=list(zip(*A))
time+=1
if time>100:
print(-1)
* 틀린 부분이나 더 나은 방법이 있을 수 있습니다. 지적해주시면 감사하겠습니다!!
'posts' 카테고리의 다른 글
[백준 17144번] 미세먼지 안녕! - Python으로 구현하기 (0) | 2019.10.19 |
---|---|
[백준 17143번] 낚시왕 - Python으로 구현하기 (1) | 2019.10.19 |
[백준 17142번] 연구소 3 - Python으로 구현하기 (0) | 2019.10.18 |
[백준 15684번] 사다리 조작 - Python으로 구현하기 (0) | 2019.10.18 |
[백준 14891번] 톱니바퀴 - Python으로 구현하기 (0) | 2019.10.18 |