🧐 깊이 μš°μ„  탐색, DFS (Depth First Search)

https://chan4im.tistory.com/121

 

self.winter.(09-3). DFS (깊이 μš°μ„  탐색) κ°œλ… _ λ°±μ€€ 11724(connected component)

🧐 깊이 μš°μ„  탐색 ( Depth First Search ) πŸ€” κΉŠμ΄μš°μ„ νƒμƒ‰, DFSλž€? 깊이 μš°μ„  탐색, 즉 DFSλŠ” κ·Έλž˜ν”„μ˜ 완전탐색(Exhaustive Search)기법 쀑 ν•˜λ‚˜ - κ·Έλž˜ν”„μ˜ μ‹œμž‘λ…Έλ“œμ—μ„œ μΆœλ°œν•΄ 탐색할 ν•œμͺ½ λΆ„κΈ°λ₯Ό μ •ν•œ

chan4im.tistory.com

 

 

🧐 λ°±μ€€ 2023 (DFS)   Gold V 


πŸ€”
 Algorithm κ³Όμ • 

1. ν•œμžλ¦¬μ†Œμˆ˜ -> 이 ν•œμžλ¦¬ μ†Œμˆ˜μ—μ„œ νŒŒμƒλ˜μ–΄ λ‚˜μ˜¨ λ‘μžλ¦¬μ†Œμˆ˜ -> ... 의 λ°˜λ³΅μ΄λ―€λ‘œ
2. ν•œμžλ¦¬ μ†Œμˆ˜λŠ” [2, 3, 5, 7]이닀.
3. μ—¬κΈ°μ„œ νŒŒμƒλ˜λŠ” μ†Œμˆ˜λŠ” λ‹€μŒκ³Ό κ°™λ‹€.
[2] - 23, 29
[3] - 31, 37
[5] - 53, 59
[7] - 71, 73, 79
μœ„ 과정을 계속 λ°˜λ³΅ν•˜μ—¬ μž…λ ₯된 μžλ¦¬μˆ˜μ— λ§žλŠ” 좜λ ₯을 진행해주면 λœλ‹€.
μ΄λŠ” μž¬κ·€λ₯Ό μ΄μš©ν•œ DFSλ₯Ό 톡해 ν•΄κ²°ν•  수 있으며 μ•„λž˜ 그림을 톡해 κ°„λ‹¨νžˆ 도식화 ν•˜μ˜€λ‹€.


🀫  solution_2023 (μ‹œκ°„μ΄ˆκ³Ό)

import sys, math
sys.setrecursionlimit(10000)
input = sys.stdin.readline

N = int(input())

dec = [i for i in range(2, 8*10**(N))]
dec_front = []
for i in range(2, int(math.sqrt(10**(N+1))) + 1):
    if i in dec:
        dec_front.append(i)
    dec = [j for j in dec if j % i != 0]
decimal = dec_front + dec

single_decimal = [2, 3, 5, 7]

def DFS(num):
    if len(str(num)) == N:
        print(num)
    else:
        for i in range(1, 10, 2):
            if num*10 + i in decimal:
                DFS(num*10 + i)

for i in single_decimal:
    DFS(i)



🀫  solution_2023

import sys
sys.setrecursionlimit(10000)
input = sys.stdin.readline

N = int(input())
single_decimal = [2, 3, 5, 7]

def decimal(num):
    for i in range(2, num//2 + 1):
        if num % i == 0:
            return False
    return True

def DFS(num):
    if len(str(num)) == N:
        print(num)
    else:
        for i in range(1, 10, 2):
            if decimal(num*10 + i):
                DFS(num*10 + i)

for i in single_decimal:
    DFS(i)

cf. // λŠ” μ •μˆ˜ν˜• λ‚˜λˆ—μ…ˆμ΄λ‹€. 

 

 

 

🧐 λ°±μ€€ 13023 (DFS)   Gold V 


πŸ€”
 Algorithm κ³Όμ • 

1. κ·Έλž˜ν”„λ₯Ό μΈμ ‘λ¦¬μŠ€νŠΈλ‘œ μ €μž₯ν•œλ‹€. (무방ν–₯ κ·Έλž˜ν”„μ΄κΈ°μ— μ–‘μͺ½λ°©ν–₯으둜 간선을 λͺ¨λ‘ μ €μž₯ν•œλ‹€.)
2. 방문리슀트λ₯Ό λͺ¨λ‘ False둜 μ΄ˆκΈ°ν™”ν•œλ‹€.
3. DFSλ₯Ό μˆ˜ν–‰ν•˜λŠ”λ°, μ•„λž˜μ™€ 같이 μ§„ν–‰ν•œλ‹€.
4. μ΅œμ’…μ μœΌλ‘œ DFS의 μž¬κ·€ νšŸμˆ˜κ°€ 5회 이상이면 λ˜λ―€λ‘œ 5νšŒκ°€ 된 μˆœκ°„μ΄ λ°”λ‘œ μž„κ³„μ μ΄λ‹€.
5. λ”°λΌμ„œ μž¬κ·€νšŸμˆ˜κ°€ 5회면 λ°”λ‘œ 1을 좜λ ₯ν•˜κ³  5회 미만이라면 0을 좜λ ₯ν•œλ‹€.


🀫  solution_13023

# λ°±μ€€ 11724
# 친ꡬ 관계 5개λ₯Ό μ°ΎλŠ” 문제 = μž¬κ·€ κΉŠμ΄κ°€ 5번 이상이면 λœλ‹€.
import sys
input = sys.stdin.readline

V, m = map(int, input().split())
vertex = [[] for _ in range(V+1)]

# λ°©λ¬Έ 리슀트 생성
visit_list = [False] * (V+1)

for _ in range(m):
    u, v = map(int, input().split())
    vertex[u].append(v)
    vertex[v].append(u)
# print(vertex) => [[1], [0, 2], [1, 3], [2, 4], [3], []]
# 인접 리슀트 생성       [0]   [1]     [2]     [3]    [4]

threshold = False
def DFS(curr_vertex, depth):
    global threshold
    if depth == 5:
        threshold = True
        return
    visit_list[curr_vertex] = True
    for i in vertex[curr_vertex]:
        if not visit_list[i]:
            DFS(i, depth+1)
    visit_list[curr_vertex] = False

for i in range(V):
    DFS(i, 1)
    if threshold:
        break

if threshold:
    print(1)
else:
    print(0)

 

 

 

+ Recent posts