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

16-H0ngJu #191

Merged
merged 1 commit into from
May 23, 2024
Merged

16-H0ngJu #191

merged 1 commit into from
May 23, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented May 18, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ ๊ฒŒ์ž„

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

40M

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๊ธˆ๋ฐฉ ๋กœ์ง์„ ์งฐ์Šต๋‹ˆ๋‹ค ํ—ˆํ—ˆ

๋ฌธ์ œ ์š”์•ฝ
๋ฑ€-์‚ฌ๋‹ค๋ฆฌ ๊ฒŒ์ž„์—์„œ ์ฃผ์‚ฌ์œ„๋ฅผ ์กฐ์ž‘ํ•ด์„œ 100๊นŒ์ง€ ๋„๋‹ฌํ•˜์ž@! ์ด๋•Œ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฌ๋Š” ์ตœ์†Œ ํšŸ์ˆ˜๋Š”?


์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ถ„๋ฅ˜

์Œ ๋ฌธ์ œ ๋ณด๊ณ  ๋ฐ”๋กœ ์˜ค ์ด๊ฑฐ BFS๋‹ค~๋กœ ์ƒ๊ฐํ•ด์„œ ๋ฑ๋“ค๊ณ  ๋ฐ”๋กœ ์ ‘๊ทผํ–ˆ์Šต๋‹ˆ๋‹ค ๐Ÿ”ฅ

๊ทธ๋ƒฅ ํ”ํ•œ BFS ๋ฌธ์ œ์™€ ๋น„์Šทํ•ด์„œ ๋กœ์ง์„ ์งœ๋Š” ๋ฐ ํฌ๊ฒŒ ์ฃผ์˜ํ•ด์•ผํ•  ๋ถ€๋ถ„์€ ์—†์—ˆ์Šต๋‹ˆ๋‹ค

๋‹ค๋งŒ, ์ œ ๋กœ์ง์—์„œ๋Š”
์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์žˆ๋Š”์ง€, ๋ฑ€์ด ์žˆ๋Š”์ง€, ๊ทธ๋ƒฅ ์ผ๋ฐ˜ ์นธ์ธ์ง€ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด
check์ด๋ผ๋Š” flag๋ฅผ ๋”ฐ๋กœ ๋‘์—ˆ์Šต๋‹ˆ๋‹ค.


์˜ˆ๋ฅผ ๋“ค์–ด,

ํ˜„์žฌ ์นธ์€ 40, ์ฃผ์‚ฌ์œ„๋Š” 3์ด๊ณ , 43๋ฒˆ์งธ ์นธ์ด ์‚ฌ๋‹ค๋ฆฌ์นธ์ด๋ผ๋ฉด,
๋ฑ€, ์ผ๋ฐ˜์นธ ๋กœ์ง์€ ํ™•์ธํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ
check = 1๋กœ ๋ฐ”๊พธ์–ด ์ฃผ๊ณ , ๋‹ค์Œ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฌ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฌ๊ฒŒ๋˜๋ฉด ์ฃผ์‚ฌ์œ„๊ฐ€ 4๊ฐ€ ๋˜๋ฏ€๋กœ, 44๋ฒˆ์งธ ์นธ์— ๋Œ€ํ•œ ๋กœ์ง์„ ์ง„ํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค



์ตœ์ข… ์ˆ˜๋„ ์ฝ”๋“œ

  1. q๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ์•„๋ž˜๋ฅผ ๋ฐ˜๋ณต
    1. cur(ํ˜„์žฌ ์œ„์น˜)๊ฐ€ 100์ด๋ฉด ์ข…๋ฃŒ.
    2. ์‚ฌ๋‹ค๋ฆฌ, ๋ฑ€, ์ผ๋ฐ˜ ์นธ์ธ์ง€ ํ™•์ธ
    3. ๊ฐ ์ผ€์ด์Šค์— ๋Œ€ํ•˜์—ฌ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์œผ๋ฉด q์— ์‚ฝ์ž…
    4. check = 1๋กœ ๋‘์–ด ๋ถˆํ•„์š”ํ•œ ๋ฐ˜๋ณต๋ฌธ ์‹คํ–‰ ์ค„์ž„



import sys
from collections import deque

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

N, M = map(int, input().split())
ladder = [list(map(int, input().split())) for _ in range(N)]
snake = [list(map(int, input().split())) for _ in range(M)]
game = [i for i in range(1,101)]
visited = [0 for _ in range(100)]
q = deque()

q.append((1,0))
visited[0] = 1

while q:
    cur, dice = q.popleft()
    if cur == 100:
        print(dice)
        break
    for i in range(1, 7):
        check = 0
        
        if cur + i > 100: continue

        # ์‚ฌ๋‹ค๋ฆฌ ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
        for l, n in ladder:
            if cur + i == l:
                if visited[n-1] == 0:
                    q.append((n, dice+1))
                    visited[n-1] = 1
                check = 1
                break

        if check: continue
        # ๋ฑ€ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
        for s, n in snake:
            if cur + i == s:
                if visited[n-1] == 0:
                    q.append((n, dice+1))
                    visited[n-1] = 1
                check = 1
                break

        if check: continue 
        # ๊ทธ ์™ธ 
        if visited[cur + i-1] == 0:
            q.append((cur + i, dice + 1))
            visited[cur + i-1] = 1

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Copy link

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

  1. ๋ฑ€ ๋˜๋Š” ์‚ฌ๋‹ค๋ฆฌ๋Š” "๋ฐ˜๋“œ์‹œ" ํƒ€์•ผํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ฐ ์นธ ๋ณ„๋กœ ๋ฑ€ ๋˜๋Š” ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์ตœ๋Œ€ 1๊ฐœ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
    ๋”ฐ๋ผ์„œ ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ €์žฅํ•ด๋†“๊ณ , ํ˜„์žฌ ์นธ์— ๋ฑ€ ๋˜๋Š” ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ทธ๊ฑฐ๋ฅผ ํƒ€๊ณ  ์•„๋ž˜ ๋กœ์ง์€ ๋ฌด์‹œํ•˜๋„๋ก ํ•ด๋ฒ„๋ฆฌ๋ฉด ์ฝ”๋“œ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  2. ์ตœ์ข…์ ์œผ๋กœ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฑด ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฌ๋Š” ํšŸ์ˆ˜์ž…๋‹ˆ๋‹ค.
    ์ด๋Š” ์ผ์ข…์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ฌธ์ œ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ, visited ๋ฆฌ์ŠคํŠธ์™€ deque์— ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฐ ํšŸ์ˆ˜๋ฅผ ๋ณ„๋„๋กœ ์ €์žฅํ•˜์ง€ ์•Š๊ณ  distances๋ž€ ๋ฆฌ์ŠคํŠธ ํ•˜๋‚˜๋กœ ๋ชจ๋‘ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

from collections import deque
input = open(0).readline

N, M = map(int, input().split())
distances = [float('inf')] * 101  # ๊ฐ ์นธ์„ ์˜ค๊ธฐ๊นŒ์ง€ ๊ตด๋ฆฐ ์ฃผ์‚ฌ์œ„ ์ตœ์†Œ ํšŸ์ˆ˜

ladder_and_snake = {}  # ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ๋Š” ๊ฐ ์นธ ๋ณ„ ์ตœ๋Œ€ 1๊ฐœ
for _ in range(N + M):
    a, b = map(int, input().split())
    ladder_and_snake[a] = b  # ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ํ•œ ๋ฒˆ์— ์ €์žฅ

q = deque([1])  # ์ตœ์ดˆ ์‹œ์ž‘์ 
distances[1] = 0  # ์‹œ์ž‘์ ์—์„œ์˜ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฐ ํšŸ์ˆ˜๋Š” 0

while q:
    u = q.popleft()  # ํ˜„์žฌ ์นธ ์œ„์น˜
    if u == 100:  # ๋„์ฐฉ์ง€๋ฉด stop
        break
    
    if u in ladder_and_snake:  # ์šฐ์„  ํ˜„์žฌ ์นธ์— ๋ฑ€ ๋˜๋Š” ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์žˆ๋Š” ์ง€ ํ™•์ธ
        v = ladder_and_snake[u]
        if distances[u] < distances[v]:  # ๋ชฉ์ ์ง€ ์นธ๊นŒ์ง€ ๊ฐ€๋Š” ๋ฐ ๊ตด๋ฆฐ ์ฃผ์‚ฌ์œ„ ํšŸ์ˆ˜๊ฐ€ ๋” ๋งŽ์€ ๊ฒฝ์šฐ์—๋งŒ
            distances[v] = distances[u]  # ๋ฑ€, ์‚ฌ๋‹ค๋ฆฌ๋Š” ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฌ์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ฐ’ ๊ทธ๋Œ€๋กœ ๋“ค์–ด๊ฐ
            q.append(v)
        continue  # ์žˆ๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ ํƒ€์•ผํ•˜๋ฏ€๋กœ ์•„๋ž˜ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ ค๋ณด๋Š” ๋กœ์ง์— ์ ‘๊ทผํ•˜์ง€ ์•Š์Œ

    for dice in range(1, 7):  # ๋ฑ€, ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์•„๋‹ˆ๋ฉด ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆผ
        v = u + dice  # ๋‹ค์Œ ์นธ์€ ํ˜„์žฌ ์นธ + ์ฃผ์‚ฌ์œ„ ๋ˆˆ
        if v <= 100 and distances[u] + 1 < distances[v]:  # 100์„ ๋„˜์ง€ ์•Š์œผ๋ฉด์„œ ๊ตด๋ฆฐ ํšŸ์ˆ˜๊ฐ€ ๋” ์ ์€ ๊ฒฝ์šฐ์—๋งŒ
            distances[v] = distances[u] + 1  # ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ ธ์œผ๋ฏ€๋กœ + 1
            q.append(v)
            
print(distances[100])  # while๋ฌธ์ด ์ข…๋ฃŒ๋˜๋ฉด ๋ชฉ์ ์ง€ ์นธ์— ์ €์žฅ๋œ ์ฃผ์‚ฌ์œ„ ํšŸ์ˆ˜ ์ถœ๋ ฅ.

์žฌ๋ฐŒ๋Š” ๋ฌธ์ œ์˜€๋„ค์š” :D

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 deque, defaultdict
import sys

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

START = 1
TARGET = 100
N, M = map(int,input().split())
ledder = defaultdict(int)
special = set()

for _ in range(N+M):
    start, destination = map(int,input().split())
    ledder[start] = destination
    special.add(start)

deq = deque([(0,START)])
visited = {START,}
while deq:
    now_count, now_position = deq.popleft()

    if now_position == TARGET:
        print(now_count)
        break

    for dice in range(1,7):
        new_position = now_position + dice
        if new_position in special: new_position = ledder[new_position]
        if new_position in visited: continue
        if new_position > 100: continue
    
        deq.append((now_count+1, new_position))
        visited.add(new_position)

์ €๋„ ํ™์ฃผ๋‹˜์ด๋ž‘ ๋น„์Šทํ•˜๊ฒŒ๋Š” ํ’€์—ˆ๋‹ค๋งŒ ๋กœ์ง์€ ๊ฐ™์€๋ฐ ์กฐ๊ธˆ์€ ์ฝ”๋“œ๊ฐ€ ๋‹ฌ๋ผ์š”!!!!

์ฒดํฌํ•˜์‹œ๋ฉด ์ข‹์„ ๋“ฏ?!!!!

๋„ˆ๋ฌด๋„ˆ๋ฌด ๊ณ ์ƒํ•˜์…จ์”๋‹ˆ๋‹ค (__) ๐Ÿ‘๐Ÿ‘๐Ÿ‘

Comment on lines +7 to +8
ladder = [list(map(int, input().split())) for _ in range(N)]
snake = [list(map(int, input().split())) for _ in range(M)]
Copy link
Member

Choose a reason for hiding this comment

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

๋‘ ๊ฐœ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์€๋ฐ ์ œ๊ฐ€ ๋ดค์„ ๋• ๋‘ ๊ฐœ์˜ ์„ฑ๊ฒฉ์ด ๋น„์Šทํ•œ ๊ฒƒ ๊ฐ™์•„์„œ ๊ตณ์ด ๋‚˜๋ˆŒ ํ•„์š” ์—†์„ ๊ฒƒ ๊ฐ™์•„์š”!

Copy link
Collaborator

Choose a reason for hiding this comment

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

์ €๋„ ํ•˜๋‚˜๋กœ ํ–ˆ์Šต๋‹ˆ๋‘ฅ

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์•— ์ƒ๊ฐํ•ด๋ณด๋‹ˆ๊นŒ ๊ทธ๋Ÿฌ๋„ค์š”
์–ด์ฐจํ”ผ a,b๋ฅผ ๋ฐ›์•˜์„ ๋•Œ a>b์ด๋ฉด ๋ฑ€์ด๊ณ  a<b๋ฉด ์‚ฌ๋‹ค๋ฆฌ๋‹ˆ๊นŒ ..

๋ฑ€ ์‚ฌ๋‹ค๋ฆฌ ํ•ด์„œ ๊ทธ๋ƒฅ ๊ตฌ๋ถ„์ง€์–ด๋ฒ„๋ ธ๋„ค์š” ..
์ƒ๊ฐ์น˜๋„ ๋ชปํ–ˆ๋˜ ๋ถ€๋ถ„์ธ๋ฐ ์ฝ”๋ฉ˜ํŠธ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ท ~~

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

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

๊ตฌํ˜„์€ ์•ˆ์–ด๋ ค์› ๋Š”๋ฐ bfs ์ž„์„ ๋– ์šธ๋ฆฌ๋Š”๊ฒŒ ์ ค ์–ด๋ ค์› ๋„ค์š”.. ์ˆจ๋ฐ”๊ผญ์งˆ ๋ฌธ์ œ๋ž‘ ๋น„์Šทํ•œ ๊ฒƒ ๊ฐ™์•„์š”!!

image
import java.io.BufferedReader
import java.io.InputStreamReader

const val MAX = 987_654_321
fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val (n, m) = br.getInputs()
    val arr = Array(101) { MAX }
    val q = ArrayDeque<Int>()

    val letterOrSnake = mutableMapOf<Int, Int>()
    repeat(n) {
        val (x, y) = br.getInputs()
        letterOrSnake[x] = y
    }
    repeat(m) {
        val (u, v) = br.getInputs()
        letterOrSnake[u] = v
    }

    arr[1] = 0
    q.add(1)

    while (q.isNotEmpty()) {
        val now = q.removeFirst()
        val nextOrNull = letterOrSnake[now]
        if (nextOrNull != null) {
            if (arr[nextOrNull] > arr[now]) {
                arr[nextOrNull] = arr[now]
                q.add(nextOrNull)
            }
            continue
        }
        for (i in 1..6) {
            val next = now + i
            if (next > 100) {
                continue
            }
            if (arr[next] > arr[now] + 1) {
                arr[next] = arr[now] + 1
                q.add(next)
            }
        }
    }
    println(arr[100])
}

private fun BufferedReader.getInputs() = readLine().split(" ").map { it.toInt() }

Comment on lines +7 to +8
ladder = [list(map(int, input().split())) for _ in range(N)]
snake = [list(map(int, input().split())) for _ in range(M)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ €๋„ ํ•˜๋‚˜๋กœ ํ–ˆ์Šต๋‹ˆ๋‘ฅ

Comment on lines +26 to +44
# ์‚ฌ๋‹ค๋ฆฌ ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
for l, n in ladder:
if cur + i == l:
if visited[n-1] == 0:
q.append((n, dice+1))
visited[n-1] = 1
check = 1
break

if check: continue
# ๋ฑ€ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
for s, n in snake:
if cur + i == s:
if visited[n-1] == 0:
q.append((n, dice+1))
visited[n-1] = 1
check = 1
break

Copy link
Collaborator

Choose a reason for hiding this comment

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

ํ•˜๋‚˜๋กœ ํ•ฉ์น  ์ˆ˜ ์žˆ๊ฒ ๋„ค์šฉ

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

๊ทธ๋Ÿฌ๋„ค์š” ์ด์ œ check๋„ ํ•„์š” ์—†๋„ค์š” .. ๐Ÿ”จ๐Ÿ”จ

@tgyuuAn
Copy link
Member

tgyuuAn commented May 21, 2024

์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ladder ์–๊ตฐ์š”....?
ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹
@SeongHoonC ใ…‹ใ…‹ใ…‹ใ…‹ใ…Œใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹์ €๋„ ๋ณ€์ˆ˜๋ช… ํ‹€๋ฆผ์šฌใ…‹ใ…‹ใ…‹ใ…‹

@H0ngJu
Copy link
Collaborator Author

H0ngJu commented May 21, 2024

  1. ๋ฑ€ ๋˜๋Š” ์‚ฌ๋‹ค๋ฆฌ๋Š” "๋ฐ˜๋“œ์‹œ" ํƒ€์•ผํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ฐ ์นธ ๋ณ„๋กœ ๋ฑ€ ๋˜๋Š” ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์ตœ๋Œ€ 1๊ฐœ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
    ๋”ฐ๋ผ์„œ ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ €์žฅํ•ด๋†“๊ณ , ํ˜„์žฌ ์นธ์— ๋ฑ€ ๋˜๋Š” ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ทธ๊ฑฐ๋ฅผ ํƒ€๊ณ  ์•„๋ž˜ ๋กœ์ง์€ ๋ฌด์‹œํ•˜๋„๋ก ํ•ด๋ฒ„๋ฆฌ๋ฉด ์ฝ”๋“œ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. ์ตœ์ข…์ ์œผ๋กœ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฑด ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฌ๋Š” ํšŸ์ˆ˜์ž…๋‹ˆ๋‹ค.
    ์ด๋Š” ์ผ์ข…์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ฌธ์ œ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ, visited ๋ฆฌ์ŠคํŠธ์™€ deque์— ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฐ ํšŸ์ˆ˜๋ฅผ ๋ณ„๋„๋กœ ์ €์žฅํ•˜์ง€ ์•Š๊ณ  distances๋ž€ ๋ฆฌ์ŠคํŠธ ํ•˜๋‚˜๋กœ ๋ชจ๋‘ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
from collections import deque
input = open(0).readline

N, M = map(int, input().split())
distances = [float('inf')] * 101  # ๊ฐ ์นธ์„ ์˜ค๊ธฐ๊นŒ์ง€ ๊ตด๋ฆฐ ์ฃผ์‚ฌ์œ„ ์ตœ์†Œ ํšŸ์ˆ˜

ladder_and_snake = {}  # ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ๋Š” ๊ฐ ์นธ ๋ณ„ ์ตœ๋Œ€ 1๊ฐœ
for _ in range(N + M):
    a, b = map(int, input().split())
    ladder_and_snake[a] = b  # ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ํ•œ ๋ฒˆ์— ์ €์žฅ

q = deque([1])  # ์ตœ์ดˆ ์‹œ์ž‘์ 
distances[1] = 0  # ์‹œ์ž‘์ ์—์„œ์˜ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฐ ํšŸ์ˆ˜๋Š” 0

while q:
    u = q.popleft()  # ํ˜„์žฌ ์นธ ์œ„์น˜
    if u == 100:  # ๋„์ฐฉ์ง€๋ฉด stop
        break
    
    if u in ladder_and_snake:  # ์šฐ์„  ํ˜„์žฌ ์นธ์— ๋ฑ€ ๋˜๋Š” ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์žˆ๋Š” ์ง€ ํ™•์ธ
        v = ladder_and_snake[u]
        if distances[u] < distances[v]:  # ๋ชฉ์ ์ง€ ์นธ๊นŒ์ง€ ๊ฐ€๋Š” ๋ฐ ๊ตด๋ฆฐ ์ฃผ์‚ฌ์œ„ ํšŸ์ˆ˜๊ฐ€ ๋” ๋งŽ์€ ๊ฒฝ์šฐ์—๋งŒ
            distances[v] = distances[u]  # ๋ฑ€, ์‚ฌ๋‹ค๋ฆฌ๋Š” ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฌ์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ฐ’ ๊ทธ๋Œ€๋กœ ๋“ค์–ด๊ฐ
            q.append(v)
        continue  # ์žˆ๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ ํƒ€์•ผํ•˜๋ฏ€๋กœ ์•„๋ž˜ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ ค๋ณด๋Š” ๋กœ์ง์— ์ ‘๊ทผํ•˜์ง€ ์•Š์Œ

    for dice in range(1, 7):  # ๋ฑ€, ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์•„๋‹ˆ๋ฉด ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆผ
        v = u + dice  # ๋‹ค์Œ ์นธ์€ ํ˜„์žฌ ์นธ + ์ฃผ์‚ฌ์œ„ ๋ˆˆ
        if v <= 100 and distances[u] + 1 < distances[v]:  # 100์„ ๋„˜์ง€ ์•Š์œผ๋ฉด์„œ ๊ตด๋ฆฐ ํšŸ์ˆ˜๊ฐ€ ๋” ์ ์€ ๊ฒฝ์šฐ์—๋งŒ
            distances[v] = distances[u] + 1  # ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ ธ์œผ๋ฏ€๋กœ + 1
            q.append(v)
            
print(distances[100])  # while๋ฌธ์ด ์ข…๋ฃŒ๋˜๋ฉด ๋ชฉ์ ์ง€ ์นธ์— ์ €์žฅ๋œ ์ฃผ์‚ฌ์œ„ ํšŸ์ˆ˜ ์ถœ๋ ฅ.

์žฌ๋ฐŒ๋Š” ๋ฌธ์ œ์˜€๋„ค์š” :D

์˜คํ™ .. ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ €์žฅํ•˜๊ณ  ๊ตณ์ด visited๋กœ ๋”ฐ๋กœ ๋ฐฉ๋ฌธ ๊ด€๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š๋Š”๋‹ค..!!
์ดํ•ดํ–ˆ์Šต๋‹ˆ๋‹ค !! ๋”ฐ๋กœ coutn ์•ˆ๋‘๋Š”๊ฒŒ ์ €๋ฒˆ @alstjr7437 ํ† ๋ฉ”์ดํ†  ๋ฌธ์ œ๊ฐ™๊ตฐ์š”

๋‹ด์— ์จ๋จน๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿ˜Ž

@alstjr7437
Copy link
Member

ํ™์ฃผ๋‹˜ ๋ฌธ์ œ ์ดํ•ดํ•˜๊ณ  ๋กœ์ง์„ ์ดํ•ดํ–ˆ๋Š”๋ฐ ๋”ฐ๋กœ ๋ฆฌ๋ทฐ๋ฅผ ๋Šฆ๊ฒŒ ๋‹ฌ์•„์„œ ๊ทธ๋Ÿฐ ์ ๋„ ์žˆ์ง€๋งŒ
ํ™์ฃผ๋‹˜ ์ฝ”๋“œ์™€ ์—„์ฒญ ๋น„์Šทํ•˜๊ฒŒ ๋‚˜์˜ค๋”๋ผ๊ตฌ์š”!!!

๊ทธ๋ž˜์„œ ๊ทธ๋ƒฅ ํ™์ฃผ๋‹˜ ์ฝ”๋“œ์—์„œ ๋ฐ˜๋ณต๋˜๋Š” ๋ถ€๋ถ„

  1. snake, labber๋ถ€๋ถ„
  2. ์‚ฌ๋‹ค๋ฆฌ ๋ฑ€ ๊ฒ€์‚ฌ ๋ถ€๋ถ„

๋“ฑ์„ ์ค„์—ฌ์„œ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค!!!

import sys
from collections import deque

input = sys.stdin.readline

N, M = map(int, input().split())
unique = [list(map(int, input().split())) for _ in range(N+M)]
game = [i for i in range(1,101)]
visited = [0 for _ in range(100)]
q = deque()

q.append((1,0))
visited[0] = 1

while q:
    cur, dice = q.popleft()
    if cur == 100:
        print(dice)
        break
    for i in range(1, 7):
        check = 0
        
        if cur + i > 100: continue

        # ์‚ฌ๋‹ค๋ฆฌ ๋˜๋Š” ๋ฑ€ ๊ฒ€์‚ฌ
        for start, end in unique:
            if cur + i == start:
                if visited[end-1] == 0:
                    q.append((end, dice+1))
                    visited[end-1] = 1
                check = 1
                break

        if check: continue

        # ๊ทธ ์™ธ 
        if visited[cur + i-1] == 0:
            q.append((cur + i, dice + 1))
            visited[cur + i-1] = 1

์‹œ๊ฐ„๊ณผ ๋ฉ”๋ชจ๋ฆฌ๋Š” ๋ง‰ ์—„์ฒญ ํฐ์ฐจ์ด๊ฐ€ ์žˆ์ง„ ์•Š๋„ค์š”!
image

Comment on lines +7 to +8
ladder = [list(map(int, input().split())) for _ in range(N)]
snake = [list(map(int, input().split())) for _ in range(M)]
Copy link
Member

Choose a reason for hiding this comment

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

์–ด์งœํ”ผ ๋ฐ›์„๋•Œ start end๋Š” ๊ฐ™๋”๋ผ๊ตฌ์š”!!
์‚ฌ๋‹ค๋ฆฌ ๋จผ์ € ๋ฐ›์ง€๋งŒ ์‚ฌ๋‹ค๋ฆฌ๋Š” ์œ„๋กœ ์˜ฌ๋ผ๊ฐ€๋Š”๋ฐ
start -> end๋กœ start < end๊ฐ€ ๋˜๊ณ 

๋ฑ€์€ ์•„๋ž˜๋กœ ๋‚ด๋ ค๊ฐ€์ง€๋งŒ input์€ ๋™์ผํ•˜๊ฒŒ
start -> end๋กœ start > end๊ฐ€ ๋˜๋Š” ๋‹ค๋ฅธ์ ๋งŒ ์žˆ์„๋ฟ ๊ฐ™๋”๋ผ๊ตฌ์š”!

import sys
from collections import deque

def input(): return sys.stdin.readline().rstrip()
Copy link
Member

Choose a reason for hiding this comment

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

input ๋ฐ”๊พธ์‹ค๋•Œ ์ด๋ ‡๊ฒŒ ์“ฐ์‹œ๋Š”๊ตฐ์š”!!
์ €๋Š”

input = sys.stdin.readline

์œผ๋กœ ๋ฐ”๊ฟ”์„œ ์‹ ๊ธฐํ•˜๋„ค์š”!!

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.

5 participants