프로그래밍/기타
2024 후기 성대경시 초등 4학년 30번 문제
snoopybox
2024. 10. 8. 01:15
초등학교 4학년이 어떻게 이 문제를 연필로 풀 수 있을지 모르겠네요. 저도 연필로 해결할 자신은 없어서... 대신 Python 코드로 풀어보았습니다.
A = ['가', '가', '나', '나', '다', '다', '라', '라']
B = [[], [], [], []]
ans = set()
def dfs(s):
if s == 4:
t = ' '.join([''.join(sorted(x)) for x in B])
if t not in ans:
ans.add(t)
print(t)
return
elif len(B[s]) == 2:
dfs(s+1)
else:
for i in range(8):
if (A[i] != 'X') and (A[i] not in B[s]):
ori = A[i]
B[s].append(ori)
A[i] = 'X'
dfs(s)
A[i] = ori
B[s].pop()
dfs(0)
print(len(ans))