알고리즘/백준
[백준] 6603번(python 파이썬)
G_Gi
2021. 2. 3. 16:21
SMALL
백트래킹의 쉬운 문제이다
숫자가 6개인 조합을 구해주면 된다.
K와 S가 합쳐진 list를 입력받는데 이차원 list로 풀어도 되지만
동적 변수명 할당이라는 것을 봐서 이번에 이렇게 풀어봤다.
동적 변수명할당, 사용
i = 1
while True:
globals()['lotto{}'.format(i)] = list(map(int, input().split()))
if globals()['lotto{}'.format(i)][0] == 0:
break
globals()['visited{}'.format(i)] = [False] * (globals()['lotto{}'.format(i)][0] + 1)
i += 1
lotto1, lotto2, ...
visited1, visited2, ...
lotto1 -> 호출 방법 : globals()['lotto{}'.format(i)]
i = 1
while True:
globals()['lotto{}'.format(i)] = list(map(int, input().split()))
if globals()['lotto{}'.format(i)][0] == 0:
break
globals()['visited{}'.format(i)] = [False] * (globals()['lotto{}'.format(i)][0] + 1)
i += 1
result = list()
def BackTracking(depth, start, lotto, visited):
global string
if depth == 6:
print(' '.join(map(str, result)))
return
for i in range(start, lotto[0]+1):
if visited[i]:
continue
visited[i] = True
result.append(lotto[i])
BackTracking(depth+1, i, lotto, visited)
result.pop()
visited[i] = False
for i in range(1, i):
BackTracking(0, 1, globals()['lotto{}'.format(i)], globals()['visited{}'.format(i)])
print()
LIST