Skip to content

Instantly share code, notes, and snippets.

@inspirit941
Created January 25, 2020 15:46
Show Gist options
  • Save inspirit941/cfec5e3f79c1f57c4c53db734eb9a07d to your computer and use it in GitHub Desktop.
Save inspirit941/cfec5e3f79c1f57c4c53db734eb9a07d to your computer and use it in GitHub Desktop.
import sys
from collections import Counter
r, c, k = map(int, sys.stdin.readline().split())
maps = []
for _ in range(3):
temp = list(map(int, sys.stdin.readline().split()))
maps.append(temp)
time = 0
find = False
while time <= 100:
if r <= len(maps) and c <= len(maps[0]) and maps[r-1][c-1] == k:
print(time)
find = True
break
time += 1
max_col = 0
next_maps = []
# R연산
if len(maps) >= len(maps[0]):
for rows in maps:
next_row = []
count_table = sorted(list(Counter(rows).items()), key = lambda x: (x[1], x[0]))
for num, cnt in count_table:
if num == 0:
continue
next_row.append(num)
next_row.append(cnt)
max_col = max(max_col, len(next_row))
next_maps.append(next_row)
# 가장 긴 row 길이에 맞게 0 개수 채우기
for rows in next_maps:
if len(rows) < max_col:
for _ in range(max_col - len(rows)):
rows.append(0)
maps = next_maps
continue
# c연산
elif len(maps) < len(maps[0]):
# 2차원 list transpose
maps = list(map(list, zip(*maps)))
for rows in maps:
next_row = []
count_table = sorted(list(Counter(rows).items()), key = lambda x: (x[1], x[0]))
for num, cnt in count_table:
if num == 0:
continue
next_row.append(num)
next_row.append(cnt)
max_col = max(max_col, len(next_row))
next_maps.append(next_row)
# 가장 긴 row 길이에 맞게 0 개수 채우기
for rows in next_maps:
if len(rows) < max_col:
for _ in range(max_col - len(rows)):
rows.append(0)
# c연산 결과에 맞게 다시 transpose
maps = list(map(list, zip(*next_maps)))
continue
if not find:
print(-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment