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

24-LJEDD2 #95

Merged
merged 5 commits into from
Mar 17, 2024
Merged

24-LJEDD2 #95

merged 5 commits into from
Mar 17, 2024

Conversation

LJEDD2
Copy link
Collaborator

@LJEDD2 LJEDD2 commented Mar 13, 2024

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

๋ฐฑ์ค€ - ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ | ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ ๊ฒŒ์ž„

๋ฌธ์ œ ์„ค๋ช…

  • ์ฃผ์‚ฌ์œ„๋ฅผ ์กฐ์ž‘ํ•ด ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์ˆ˜๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋ฉด, ์ตœ์†Œ ๋ช‡ ๋ฒˆ๋งŒ์— ๋„์ฐฉ์ ์— ๋„์ฐฉํ•  ์ˆ˜ ์žˆ์„๊นŒ?
  • ํ”Œ๋ ˆ์ด์–ด๋Š” ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ ค ๋‚˜์˜จ ์ˆ˜๋งŒํผ ์ด๋™ํ•œ๋‹ค.
  • ๋„์ฐฉํ•œ ์นธ์ด ์‚ฌ๋‹ค๋ฆฌ๋ฉด, ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ํƒ€๊ณ  ์œ„๋กœ ์˜ฌ๋ผ๊ฐ„๋‹ค. ๋ฑ€์ด ์žˆ๋Š” ์นธ์— ๋„์ฐฉํ•˜๋ฉด, ๋ฑ€์„ ๋”ฐ๋ผ์„œ ๋‚ด๋ ค๊ฐ€๊ฒŒ ๋œ๋‹ค.
    • ์ฆ‰, ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ์ด์šฉํ•ด ์ด๋™ํ•œ ์นธ์˜ ๋ฒˆํ˜ธ๋Š” ์›๋ž˜ ์žˆ๋˜ ์นธ์˜ ๋ฒˆํ˜ธ๋ณด๋‹ค ํฌ๊ณ , ๋ฑ€์„ ์ด์šฉํ•ด ์ด๋™ํ•œ ์นธ์˜ ๋ฒˆํ˜ธ๋Š” ์›๋ž˜ ์žˆ๋˜ ์นธ์˜ ๋ฒˆํ˜ธ๋ณด๋‹ค ์ž‘์•„์ง„๋‹ค.
  • ๊ฒŒ์ž„์˜ ๋ชฉํ‘œ๋Š” 1๋ฒˆ ์นธ์—์„œ ์‹œ์ž‘ํ•ด์„œ 100๋ฒˆ ์นธ์— ๋„์ฐฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ œํ•œ ์กฐ๊ฑด

  • ์‚ฌ๋‹ค๋ฆฌ์˜ ์ˆ˜ N(1 โ‰ค N โ‰ค 15)๊ณผ ๋ฑ€์˜ ์ˆ˜ M(1 โ‰ค M โ‰ค 15)
  • 1๋ฒˆ ์นธ๊ณผ 100๋ฒˆ ์นธ์€ ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ์˜ ์‹œ์ž‘ ๋˜๋Š” ๋์ด ์•„๋‹ˆ๋‹ค.
  • ํ•ญ์ƒ 100๋ฒˆ ์นธ์— ๋„์ฐฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž…๋ ฅ๋งŒ ์ฃผ์–ด์ง„๋‹ค.
  • ๋ชจ๋“  ์นธ์€ ์ตœ๋Œ€ ํ•˜๋‚˜์˜ ์‚ฌ๋‹ค๋ฆฌ ๋˜๋Š” ๋ฑ€์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ๋™์‹œ์— ๋‘ ๊ฐ€์ง€๋ฅผ ๋ชจ๋‘ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

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

1์‹œ๊ฐ„ 30๋ถ„

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

1. ๋ฌธ์ œ ์ง€๋ฌธ ๋”ฐ๋ผ๊ฐ€๊ธฐ

  • ๋จผ์ € ์ฃผ์‚ฌ์œ„๋ฅผ ๊ฐ€์žฅ ์ตœ์†Œ๋กœ ๊ตด๋ ค 100๋ฒˆ ์ง€์ ์— ๋น ๋ฅด๊ฒŒ ๋„์ฐฉํ•˜๋Š” ๊ฒƒ์ด ๋ฌธ์ œ์˜ ๋ชฉํ‘œ๋‹ค.
  • 100๋ฒˆ ์ง€์ ์— ๋น ๋ฅด๊ฒŒ ๋„์ฐฉํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— BFS๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

image

  • ๋ฌธ์ œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋„์ฐฉํ•œ ์นธ์ด ์‚ฌ๋‹ค๋ฆฌ๋‹ค? ์˜ฌ๋ผ๊ฐ€๊ณ , ๋ฑ€์ด๋‹ค? ๋‚ด๋ ค๊ฐ„๋‹ค.
    • ๋„์ฐฉํ•œ ๋™์‹œ์— ์ด๋™์„ ํ•ด์ฃผ๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋”•์…”๋„ˆ๋ฆฌ์˜ Key:Value๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ขŒํ‘œ๋ฅผ ํ…”๋ ˆํฌํŠธ ์‹œ์ผœ์ฃผ๋„๋ก ํ•˜์˜€๋‹ค.
jump = {}
for _ in range(n+m):
    loc = list(map(int, input().split()))
    # ํ…”๋ ˆํฌํŠธ!
    jump[loc[0]] = loc[1]

def BFS():
    ...
    # ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ ์ง€์—ญ์„ ๋งŒ๋‚˜๋ฉด ์ผ๋‹จ ์ด๋™ ๋จผ์ € ํ•ด์•ผ ํ•œ๋‹ค. (์ฃผ์˜)
                if next_x in jump:
                    next_x = jump[next_x] # ํ…”๋ ˆํฌํŠธ !
...

2. ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰

  • 1๋ฒˆ๋ถ€ํ„ฐ 6๋ฒˆ๊นŒ์ง€ ์–ด๋–ค ์ˆซ์ž๋งŒํผ ์ด๋™์„ ํ•ด์•ผ ๋น ๋ฅด๊ฒŒ 100๋ฒˆ์— ๋„์ฐฉํ• ์ง€, ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ์ž˜ ํƒ€์•ผ ๋น ๋ฅด๊ฒŒ ๋„๋‹ฌํ•  ์ง€๋ฅผ ๊ณ ๋ฏผํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • ํ˜„์žฌ ์œ„์น˜์—์„œ +1์นธ๋ถ€ํ„ฐ +6์นธ๊นŒ์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ณด๋ฉด์„œ ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ์ด์šฉํ•˜๋ฉด์„œ ์ฃผ์‚ฌ์œ„ ํšŸ์ˆ˜๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฐพ๋Š”๋‹ค.
        for d in range(1, 7):
            next_x = cur_x + d

            if next_x <= 100 and not visited[next_x]:
                # ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ ์ง€์—ญ์„ ๋งŒ๋‚˜๋ฉด ์ผ๋‹จ ์ด๋™ ๋จผ์ € ํ•ด์•ผ ํ•œ๋‹ค. (์ฃผ์˜)
                if next_x in jump:
                    next_x = jump[next_x] # ํ…”๋ ˆํฌํŠธ !
                if not visited[next_x]:
                    visited[next_x] = True
                    queue.append([next_x, dice_count+ 1])
  • ๋ฌด์กฐ๊ฑด 100์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ž…๋ ฅ๋งŒ ์ฃผ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— 100๋ฒˆ ์ง€์ ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๋Š” ๊ณ ๋ คํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
  • ์ด๋™ํ•œ ์นธ, ํ˜„์žฌ ์นธ์ด 100์ผ ๊ฒฝ์šฐ ์ฃผ์‚ฌ์œ„ ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  exit()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•œ๋‹ค.

3. ์ตœ์ข… ์ฝ”๋“œ

from collections import deque

def BFS():
    queue = deque([[1, 0]])
    visited[1] = True
    while queue:
        cur_x, dice_count = queue.popleft()

        if cur_x == 100:
            print(dice_count)
            exit()

        for d in range(1, 7):
            next_x = cur_x + d

            if next_x <= 100 and not visited[next_x]:
                # ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ ์ง€์—ญ์„ ๋งŒ๋‚˜๋ฉด ์ผ๋‹จ ์ด๋™ ๋จผ์ € ํ•ด์•ผ ํ•œ๋‹ค. (์ฃผ์˜)
                if next_x in jump:
                    next_x = jump[next_x] # ํ…”๋ ˆํฌํŠธ !
                if not visited[next_x]:
                    visited[next_x] = True
                    queue.append([next_x, dice_count+ 1])

# ์‚ฌ๋‹ค๋ฆฌ์™€ ๋ฑ€
n,m = map(int,input().split())
visited = [False for _ in range(101)]
jump = {}
for _ in range(n+m):
    loc = list(map(int, input().split()))
    # ํ…”๋ ˆํฌํŠธ!
    jump[loc[0]] = loc[1]
BFS()

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

Copy link
Collaborator

@Hwangyerin Hwangyerin left a comment

Choose a reason for hiding this comment

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

๋ฌธ์ œ๋ฅผ ์ฝ๊ณ  ์‚ฌ๋‹ค๋ฆฌ์ผ ๊ฒฝ์šฐ์™€ ๋ฑ€์ผ ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ„๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค..(์™œ์ธ์ง€ ์•„์ง๋„ ์˜๋ฌธ)

์ •์€๋‹˜ ๋•๋ถ„์— ์•„์ง bfs/dfs ๊ตฌํ˜„์€ ์–ด๋ ต์ง€๋งŒ ์Šค์Šค๋กœ๋ฅผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ bfs/dfs ์— ๋…ธ์ถœ์‹œํ‚ค๊ฒŒ ๋˜๋„ค์š”:) ์ „๋ณด๋‹ค ์ฝ”๋“œ๋ฅผ ์ฝ๋Š”๋ฐ ๋ถ€๋‹ด์ด ์ค„์–ด๋“ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค! :)

$+$ ์ˆ˜๋„์ฝ”๋“œ ๊ทธ๋ฆผ ๊ฐœ์ธ์ ์œผ๋กœ ๋งˆ์Œ์— ๋“œ๋„ค์š”. ๋ฑ€..ใ…‚.....p... python?

@LJEDD2
Copy link
Collaborator Author

LJEDD2 commented Mar 17, 2024

๋ฌธ์ œ๋ฅผ ์ฝ๊ณ  ์‚ฌ๋‹ค๋ฆฌ์ผ ๊ฒฝ์šฐ์™€ ๋ฑ€์ผ ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ„๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค..(์™œ์ธ์ง€ ์•„์ง๋„ ์˜๋ฌธ)

@Hwangyerin ์ €๋„ ๊ทธ๋ž˜์„œ ์ฒ˜์Œ์— ๋”ฐ๋กœ ์กฐ๊ฑด๋ฌธ ํŒ ๋‹ค๊ฐ€ x+k, x-k๋กœ ์ด๋™๊ณผ ๋น„์Šทํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ฌ์•„๋ฒ„๋ฆฐ ...
๋ฑ€ ๊ท€์—ฝ์ฃ  ๊ทธ๋Ÿผ์š” ํ•˜ํ•˜ํ•˜

janghw0126

This comment was marked as resolved.

@janghw0126 janghw0126 self-requested a review March 17, 2024 10:45
Copy link
Member

@janghw0126 janghw0126 left a comment

Choose a reason for hiding this comment

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

์ €๋„ ์‚ฌ๋‹ค๋ฆฌ์™€ ๋ฑ€์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ ์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋‹ค๊ฐ€ ๋„ˆ๋น„์šฐ์„ ํƒ์ƒ‰์—์„œ ๋ง‰ํ˜€ ์ •์€๋‹˜์˜ PR์„ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค๐Ÿ‘€
๊น”๋”ํ•œ ์ฝ”๋“œ์™€ ์นœ์ ˆํ•œ ๊ทธ๋ฆผ ์„ค๋ช… ๋•๋ถ„์— ๋ฌธ์ œ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํŒŒ์•…ํ•˜๊ณ  ์ œ ์†์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๋”ฐ๋ผ์ ์„ ์ˆ˜ ์žˆ์—ˆ์–ด์š”!

์ด๋ฒˆ PR๋„ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค๋žŒ์ฅ๐Ÿฅœ๐Ÿฅœ

Copy link
Member

@avocado-13 avocado-13 left a comment

Choose a reason for hiding this comment

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

์ •์€๋‹˜์˜ ์ฝ”๋“œ๋ฅผ ์ฝ์œผ๋ฉด ์–ด๋ ค์› ๋˜ ๋ฌธ์ œ๊ฐ€ ์‰ฌ์›Œ๋ณด์ด๋Š” ๋งค์ง.. . ๐Ÿช„
์ด๋ฒˆ์ฐจ์‹œ pr๋„ ์ˆ˜๊ณ  ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค ๐ŸŽฉโ™ก

Comment on lines +29 to +32
for _ in range(n+m):
loc = list(map(int, input().split()))
# ํ…”๋ ˆํฌํŠธ!
jump[loc[0]] = loc[1]
Copy link
Member

Choose a reason for hiding this comment

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

์˜ค ์ €๋„ 2๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„๊นŒ์ง€, ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„๊ฐ€์ง€ ๋‚˜๋ˆ ์„œ ์ž…๋ ฅ๋ฐ›์•„์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์—ˆ๋Š”๋ฐ, ๊ตณ์ด ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†์—ˆ๊ตฐ์š”! ํ…”๋ ˆํฌํŠธ๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ํ™œ์šฉํ•˜์‹  ๋ถ€๋ถ„๋„ ์ธ์ƒ๊นŠ์Šต๋‹ˆ๋‹ค!!

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.

4 participants