DevYoon
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ ์ฐพ๊ธฐ (Python) ๋ณธ๋ฌธ
link ๐ https://programmers.co.kr/learn/courses/30/lessons/42839
1๏ธโฃ DFS๋ฅผ ์ฌ์ฉํด์ ํ์ดํ๋ค.
2๏ธโฃ ์ซ์์ ์๋ฆฟ์๊ฐ numbers์ ์๋ฆฟ์๋ณด๋ค ํฌ๋ฉด ์ ๋๋ฏ๋ก, ๋ ๋ฒจ์ด numbers์ ์๋ฆฟ์๋ณด๋ค ๋ ์ปค์ง๋ฉด ๋ฆฌํดํด์ฃผ์๋ค.
3๏ธโฃ num์ด ์ต์ด์ ์ํ('')๊ฐ ์๋๊ณ , DFS์ ๋ณ์๋ก check ๋ฐฐ์ด์ ๋์ด check ์์ ์๋ ์๋ง ๊ฒ์ฌํด์ฃผ์๋ค.
4๏ธโฃ ์๊ฐ ์ด๊ณผ๋ก ๋ต์ด ์ ๋์์ ์์ ๊ฒ์ฌํ๋ for๋ฌธ ์์ break๋ฅผ ๋ฃ์ด์คฌ๋๋ ์ ๋๋ก ๋ต์ด ๋์๋ค.
def solution(numbers):
answer = 0
visit = [0]*len(numbers)
def dfs(level, num, check):
nonlocal answer
if level == len(numbers)+1:
return
flag = 0
if num != '' and int(num) > 1 and int(num) not in check:
for i in range(2, int(num)):
if int(num)%i == 0:
flag = 1
break
if flag == 0:
answer += 1
check.append(int(num))
for idx in range(len(numbers)):
if visit[idx] == 1:continue
new_num = num+numbers[idx]
visit[idx] = 1
dfs(level+1, new_num, check)
visit[idx] = 0
dfs(0, '', [])
return answer