백준 구현 대비 이차원배열과연산

yjkim·2023년 7월 19일
0

알고리즘

목록 보기
32/59

접근

열정렬은 원래 그래프를 zip연산 한 후 행정렬을 시행해주는 것과 동일하다 즉 행정렬 함수만 정의하면 해결되는 문제.

코드

def R():
  maxlen=0
  for j in range(len(graph)):
    tempdict={}
    templist=[]
    for i in range(len(graph[j])):
      if graph[j][i]==0:
        continue
      tempdict[graph[j][i]]=graph[j].count(graph[j][i])
    tempdict=sorted(tempdict.items(), key=lambda x:(x[1],x[0]))
    for t in tempdict:
      templist.append(t[0])
      templist.append(t[1])
    maxlen=max(maxlen,len(templist))
    graph[j]=templist
  for i in range(len(graph)):
    graph[i]+=[0]*(maxlen-len(graph[i]))
  
    
def L():
  global graph
  graph=list(map(list,zip(*graph)))
  R()
  graph=list(map(list,zip(*graph)))
  



    




r,c,k=map(int, input().split())
graph=[]
for i in range(3):
  graph.append(list(map(int, input().split())))
time=0
while time<=100:
  if 0<=r-1<len(graph) and 0<=c-1<len(graph[0]) and graph[r-1][c-1]==k:
    print(time)
    exit(0)
  else:
    if len(graph)>=len(graph[0]):
      R()
    else:
      L()
  time+=1

print(-1)
profile
컴퓨터 공부 / 백엔드 개발

1개의 댓글

comment-user-thumbnail
2023년 7월 19일

좋은 글 잘 읽었습니다, 감사합니다.

답글 달기