Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

29-SeongHoonC #213

Merged
merged 1 commit into from
Jul 8, 2024
Merged

29-SeongHoonC #213

merged 1 commit into from
Jul 8, 2024

Conversation

SeongHoonC
Copy link
Collaborator

πŸ”— 문제 링크

μ„œμšΈμ— 온 κΈ°λ…μœΌλ‘œ μ„œμšΈμ˜ μ§€ν•˜μ²  ν’€μ—ˆμŠ΅λ‹ˆλ‹€ ν•˜ν•˜

μ„œμšΈμ˜ μ§€ν•˜μ² 
image

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

으으 μ˜€λžœλ§Œμ— ν‘Έλ‹ˆκΉ 또 적응이 μ•ˆλ˜λ„€μš”. μ‹œκ°„μ΄ μ’€ 더 κ±Έλ ΈμŠ΅λ‹ˆλ‹€.

1μ‹œκ°„ 30λΆ„

✨ μˆ˜λ„ μ½”λ“œ

Map κ³Ό BFS 둜 ν’€μ—ˆμŠ΅λ‹ˆλ‹€.

given

  1. n ν˜Έμ„ μ—μ„œ 갈 수 μžˆλŠ” μ§€ν•˜μ²  역을 Map 으둜 μ €μž₯
  2. X μ—­μ—μ„œ νƒˆ 수 μžˆλŠ” ν˜Έμ„ μ„ Map 으둜 μ €μž₯
  3. λͺ‡ 번의 ν™˜μŠΉμœΌλ‘œ λ°©λ¬Έν–ˆλŠ”μ§€ μ €μž₯ν•˜λŠ” Visited Map 생성
  4. μ„œμšΈμ—­(좜발)을 ν¬ν•¨ν•˜λŠ” ν˜Έμ„ μ΄λ©΄ λ°”λ‘œ λ°©λ¬Έμ²˜λ¦¬ν•˜κ³  큐에 λ„£μŠ΅λ‹ˆλ‹€

when & then

  1. 이미 도착역을 λ°©λ¬Έν–ˆλ‹€λ©΄ -> 0 좜λ ₯ ν›„ μ’…λ£Œ
  2. BFS 둜 큐에 λ“€μ–΄μžˆλŠ” μ—­(now)μ—μ„œ νƒˆ 수 μžˆλŠ” ν˜Έμ„ μ˜ 갈 수 μžˆλŠ” 역듀을 μ°ΎμŠ΅λ‹ˆλ‹€.
  3. 도착역이면 now κΉŒμ§€ ν™˜μŠΉν•œ 횟수 + 1 을 좜λ ₯
  4. λ°©λ¬Έν•œ 적 μ—†μœΌλ©΄ now κΉŒμ§€ ν™˜μŠΉν•œ 횟수 + 1 둜 λ°©λ¬Έμ²˜λ¦¬ν•©λ‹ˆλ‹€.
  5. 이미 λ°©λ¬Έν–ˆμœΌλ©΄ λ¬΄μ‹œν•©λ‹ˆλ‹€.

KeyPoint

도착역이 μ„œμšΈμ—­κ³Ό 같은 ν˜Έμ„ μ— μžˆλ‹€λ©΄ 0 을 좜λ ₯ν•˜κ³  λ°”λ‘œ μ’…λ£Œν•œλ‹€.

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from collections import defaultdict, deque
import sys

def input(): return sys.stdin.readline().rstrip()

line_graph = defaultdict(list)
link_graph = defaultdict(list)

start_lines = list()
destination_line = -1
N = int(input())

for line in range(N):
    line_info = list(map(int, input().split()))[1:]

    for line_number in line_info:
        if line_number == 0: start_lines.append(line)

        link_graph[line_number].append(line)
        
    line_graph[line].extend(line_info)

destination_line_number = int(input())

for line, line_info in line_graph.items():

    for line_number in line_info:
        if line_number == destination_line_number: 
            destination_line = line
            break
        
    if destination_line != -1: break

if destination_line != -1:
    answer = int(1e9)
    
    for start_line in start_lines:
        visited = {start_line,}
        deq = deque([(0, start_line)])
        while deq:
            now_count, now_line = deq.popleft()
        
            if now_line == destination_line:
                answer = min(answer, now_count)
                break
        
            for line_number in line_graph[now_line]:
                for linked_line in link_graph[line_number]:
                    if linked_line in visited: continue
        
                    visited.add(linked_line)
                    deq.append((now_count+1, linked_line))
    
    print(answer) if answer != int(1e9) else print(-1)                
else: print(-1)

저도 μ„±ν›ˆλ‹˜μ΄λž‘ λΉ„μŠ·ν•˜κ²Œ ν‘Ό 것 κ°™μ•„μš”.

근데 λ§ˆμ§€λ§‰μ— 도착역 κ³„μ‚°ν•˜λŠ” 둜직이 째까 λ‹€λ₯Έλ“―!!!!!!

μž¬λ°Œλ”° μž¬λ°Œλ”° μž¬λ°Œλ”°

@tgyuuAn tgyuuAn removed the request for review from alstjr7437 July 5, 2024 14:33
@SeongHoonC SeongHoonC merged commit 4467fda into main Jul 8, 2024
6 checks passed
@SeongHoonC SeongHoonC deleted the 29-SeongHoonC branch July 8, 2024 03:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants