728x90

product 사용시 * 를 이용해주면 여러 리스트의 원소들을 기준으로 product가 생성이 된다. product 를 사용했는데 메모리 초과가 나왔다. 2차원 리스트를 사용하는 dfs 문제인데 아직 이해가 잘 안된다.

import sys
from collections import deque
from itertools import product
 
def check(k):
 
    for i in range(0,len(k)-1):
        if k[i] == k[i+1]:
            return False
 
    return True
 
 
num = int(input())
 
k = [ ]
 
for i in range(0,num):
    k.append(list(map(int,sys.stdin.readline().split(" "))))
 
index = 0
now = 0
 
start = [ ]
 
for i in k[0]:
    start.append(i)
 
pro = list(product(*k))
 
temp = [ ]
 
for i in range(0,len(pro)):
 
    if check(list(pro[i])) == True:
        temp = list(pro[i])
        break
 
if len(temp)!=0:
 
    for j in range(0,len(list(pro[i]))):
        print(temp[i][j])
 
 
if len(temp)==0:
    print(-1)
 
728x90

+ Recent posts