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

20-LJEDD2 #80

Merged
merged 2 commits into from
Mar 1, 2024
Merged

20-LJEDD2 #80

merged 2 commits into from
Mar 1, 2024

Conversation

LJEDD2
Copy link
Collaborator

@LJEDD2 LJEDD2 commented Feb 27, 2024

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

๋ฐฑ์ค€ - BFS | ์ƒ๋ฒ” ๋นŒ๋”ฉ

๋ฌธ์ œ ์„ค๋ช…

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

์ œํ•œ ์กฐ๊ฑด

  • ์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค
  • ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ L, R, C๋กœ ์‹œ์ž‘ํ•œ๋‹ค.
    - L(1 โ‰ค L โ‰ค 30)์€ ์ƒ๋ฒ” ๋นŒ๋”ฉ์˜ ์ธต ์ˆ˜ , R(1 โ‰ค R โ‰ค 30)๊ณผ C(1 โ‰ค C โ‰ค 30)๋Š” ์ƒ๋ฒ” ๋นŒ๋”ฉ์˜ ํ•œ ์ธต์˜ ํ–‰๊ณผ ์—ด์˜ ๊ฐœ์ˆ˜.
  • ์‹œ์ž‘ ์ง€์ ์€ 'S'๋กœ ํ‘œํ˜„๋˜๊ณ , ํƒˆ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ์ถœ๊ตฌ๋Š” 'E'๋กœ ํ‘œํ˜„๋œ๋‹ค.
  • ๊ฐ ์ธต ์‚ฌ์ด์—๋Š” ๋นˆ ์ค„์ด ์žˆ์œผ๋ฉฐ, ์ž…๋ ฅ์˜ ๋์€ L, R, C๊ฐ€ ๋ชจ๋‘ 0์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค. ์‹œ์ž‘ ์ง€์ ๊ณผ ์ถœ๊ตฌ๋Š” ํ•ญ์ƒ ํ•˜๋‚˜๋งŒ ์žˆ๋‹ค.

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

2์‹œ๊ฐ„

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

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

  • ์ •์œก๋ฉด์ฒด, ๊ธˆ์œผ๋กœ ์ด๋ค„์ง„ ๋ถ€๋ถ„์€ ์ง€๋‚˜๊ฐˆ ์ˆ˜ ์—†๋‹ค. -> ํƒ์ƒ‰ ๋ฒ”์œ„ ๊ทธ๋ž˜ํ”„ ๋‚ด๋ถ€
  • ์ด๋™ ๊ฐ€๋Šฅ ๋ฒ”์œ„ - ๋™ ์„œ ๋‚จ ๋ถ ์ƒ ํ•˜ , ๋Œ€๊ฐ์„  X -> ์œ„ ์•„๋ž˜๋„ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ถ”๊ฐ€

-> ๋ฌธ์ œ ์ง€๋ฌธ์„ ์ฝ์–ด๋ณด์•˜์„ ๋•Œ, ์ฒ˜์Œ์—๋Š” ๋จธ๋ฆฌ์— ์•ˆ๋“ค์–ด์™”๋‹ค.
์ž…์ถœ๋ ฅ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด์„œ ์œ„, ์•„๋ž˜๋กœ๋„ ๋ฐฉ๋ฌธ์ด ๊ฐ€๋Šฅํ•œ๊ฐ€?! ๋ผ๊ณ  ์ƒ๊ฐ์„ ํ•ด๋‚ผ ์ˆ˜ ์žˆ์—ˆ๊ณ  3์ฐจ์› ๋ฐฐ์—ด์„ ํ™œ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ๋ผ๋Š” ๊ฒƒ์„ ์‰ฝ๊ฒŒ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

2. ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰

  • BFS๋ฅผ ๋Œ๋ ค ์ตœ๋‹จ๊ฑฐ๋ฆฌ(=์ตœ๋‹จ ์‹œ๊ฐ„)๋ฅผ ๊ณ„์‚ฐํ–ˆ๋‹ค.
  • ๋™์„œ๋‚จ๋ถ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ƒํ•˜๊นŒ์ง€ ๊ณ ๋ คํ•˜์—ฌ ์ด๋™ ์ขŒํ‘œ๋ฅผ ์„ค์ •ํ•œ๋‹ค.
    ์˜ˆ์ œ๋ฅผ ์ฝ๊ณ  ์•„๋‹ˆ ์–ด๋–ป๊ฒŒ E๋กœ ๋ฐ”๋กœ ๊ฐˆ ์ˆ˜๊ฐ€ ์žˆ๋Š”๊ฑฐ์•ผ? ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์—ˆ๋Š”๋ฐ ๊ทธ๋ฆผ์„ ํ•จ๊ป˜ ๋ณด๋ฉด ์ดํ•ด๊ฐ€ ์กฐ๊ธˆ ๋” ์‰ฝ๋‹ค !

image

  • ์œ—์ธต, ์•„๋ž˜์ธต์„ ๊ฐˆ ์ˆ˜ ์žˆ์„ ๋•Œ, ๊ทธ ๋‹ค์Œ ์—ด์˜ ์œ„์น˜๋งŒ +1 ํ•ด์คŒ์œผ๋กœ์จ ์œ„์น˜ ์ด๋™์ด ๊ฐ€๋Šฅํ•˜๋‹ค .
    ๋”ฐ๋ผ์„œ ๋ฐฉํ–ฅ์„ ์ด 6๊ฐ€์ง€๋ฅผ ์‚ดํŽด๋ด์•ผ ํ•œ๋‹ค.
# 3์ฐจ์›์—์„œ์˜ ์ด๋™์„ ๋‚˜ํƒ€๋‚ด์•ผ ํ•จ.

offset = [(1,0,0),(-1,0,0),(0,-1,0),(0,1,0),(0,0,-1),(0,0,1)]
  • ์ธ๋ฑ์Šค๋ฅผ ๋Œ ๋•Œ, ๋ช‡ ์ธต, ๋ช‡๋ฒˆ์งธ ์œ„์น˜(X,Y)์ธ์ง€ ์ƒ๊ฐํ•˜๋ฉด์„œ, ํ•œ ์ค„์”ฉ ์ถœ๋ ฅํ•ด๋ณด๋ฉฐ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋œ ํ—ท๊ฐˆ๋ฆฐ๋‹ค.
  • ํ˜„์žฌ ์œ„์น˜์—์„œ ์œ„ ์•„๋ž˜๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€, ์ œ์ž๋ฆฌ์—์„œ ๋™์„œ๋‚จ๋ถ ์ด๋™์ด ๊ฐ€๋Šฅํ•œ์ง€๋ฅผ ํŒ๋‹จํ–ˆ๋‹ค.
    • ๋‹ค์Œ ์œ„์น˜๊ฐ€ 'E'๋ผ๋ฉด ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ํ•จ์ˆ˜๋ฅผ ๋น ์ ธ๋‚˜๊ฐ„๋‹ค.
    while queue:
        f ,x, y = queue.popleft()

        for df, dx, dy in offset:
            nf, nx, ny = f + df, x + dx, y + dy
            if not OOB(nf,nx,ny) and not visited[nf][nx][ny] and building[nf][nx][ny] != '#':

                if building[nf][nx][ny] == 'E':
                    return visited[f][x][y]

                visited[nf][nx][ny] = visited[f][x][y] + 1
                queue.append((nf,nx,ny))
  • ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์ž…๋ ฅ๊ณผ ๋™์‹œ์— ์‹œ์ž‘์ง€์ ์„ ์ฐพ๊ณ  ์ถœ๋ ฅ๋ฌธ ๋งˆ์นจํ‘œ ๋นผ๋จน์œผ๋ฉด ์•ˆ๋œ๋‹ค๋Š” ๋ถ€๋ถ„์„ ์ฃผ์˜ํ•˜๋ฉฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
def solve(L, R, C):
    global building

    building = []

    for l in range(L):
        building.append([list(input().rstrip()) for _ in range(R)])
        for r in range(R):
            for c in range(C):
                if building[l][r][c] == "S":
                    sl, sr, sc = l, r, c
        input()

    time = bfs(sl,sr,sc)
    if time:
        return print(f"Escaped in {time} minute(s).")
    else:
        return print('Trapped!')

while True:
    L,R,C = map(int,input().split())
    if not L and not R and not C :
        break
    solve(L, R, C)

3. ์ „์ฒด ์ฝ”๋“œ

from collections import deque

offset = [(1,0,0),(-1,0,0),(0,-1,0),(0,1,0),(0,0,-1),(0,0,1)]

def OOB(floor,x,y):
    return not(0 <= floor < L and 0 <= x < R and 0 <= y < C)

# ํƒ์ƒ‰ํ•˜๋‹ค๊ฐ€ ๋‹ค์Œ ์ธต์œผ๋กœ ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋ฉด ? floor + 1

def bfs(sl,sr,sc):
    visited = [[[0] * C for _ in range(R)] for _ in range(L)]
    visited[sl][sr][sc] = 1
    queue = deque([(sl,sr,sc)])

    while queue:
        f ,x, y = queue.popleft()

        for df, dx, dy in offset:
            # ๋ฐฉ๋ฌธ ๊ฐ€๋Šฅํ•œ ์ธต, x, y๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ 
            nf, nx, ny = f + df, x + dx, y + dy

            if not OOB(nf,nx,ny) and not visited[nf][nx][ny] and building[nf][nx][ny] != '#':

                # ๊ทธ ๋‹ค์Œ ์œ„์น˜๊ฐ€ E๋ผ๋ฉด ํƒˆ์ถœ 
                if building[nf][nx][ny] == 'E':
                    return visited[f][x][y]

                visited[nf][nx][ny] = visited[f][x][y] + 1
                queue.append((nf,nx,ny))

    return None


def solve(L, R, C):
    global building

    building = []

    for l in range(L):
        building.append([list(input().rstrip()) for _ in range(R)])

        for r in range(R):
            for c in range(C):
                if building[l][r][c] == "S":
                   # ์‹œ์ž‘์ง€์  ์œ„์น˜ ์ €์žฅ 
                    sl, sr, sc = l, r, c

        input()

    time = bfs(sl,sr,sc)

    if time:
        return print(f"Escaped in {time} minute(s).")
    else:
        return print('Trapped!')

# MAIN 
while True:
    L,R,C = map(int,input().split())
    if not L and not R and not C :
        break
    solve(L, R, C)

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

  • 3์ฐจ์› ๋ฐฐ์—ด ํƒ์ƒ‰ ๋ฌธ์ œ ๋ญ”๊ฐ€ ์–ด๋ ต๋‹ค.

@9kyo-hwang
Copy link
Contributor

์ด ์‚ฌ๋žŒ ๋˜ ์ƒˆ๋ฒฝ๊นŒ์ง€ ์•ˆ์žค๊ตฌ๋งŒ

@Hwangyerin
Copy link
Collaborator

ใ„ทใ„ฑใ„ทใ„ฑใ„ทใ„ฑใ„ทใ„ฑ

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.

์™! 3์ฐจ๋ฐฐ์—ด๊นŒ์ง€ ์จ์•ผํ•œ๋‹ค๋‹ˆ.. ์ฒ˜์Œ์—๋Š” ์ดํ•ด๊ฐ€ ์•ˆ๋๋Š”๋ฐ ์ •์€๋‹˜์˜ ๊ทธ๋ฆผ์„ ๋ณด๊ณ  ํ•„์š”ํ•  ์ˆ˜ ๋ฐ–์— ์—†๊ตฌ๋‚˜ ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค.

์—ญ์‹œ ์ด๋ฒˆ PR๋„ ์ •์€๋‹˜์ด ์ข‹์•„ํ•˜์‹œ๋Š” ๊ฒŒ์ž„๊ฐ™์€ ๋ฌธ์ œ๊ตฐ์š” ใ…Žใ…‹ใ…‹
ALM ์ €ํฌํŒ€์›์˜ ํด๋”๋ฅผ ๋ณด๋Š”๋ฐ ๋‹ค๋ฅธ ๋ถ„ ํด๋”์—๋Š” ์žˆ๋Š”๋ฐ ์œ ์ผํ•˜๊ฒŒ ์ •์€๋‹˜์˜ ํด๋” ์•ˆ์— ์ˆ˜ํ•™ ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ์—†๋”๋ผ๊ตฌ์š”.?
๋‹ค์Œ ๋ฌธ์ œ๋Š” ์ •์ˆ˜๋ก ์ด๋‚˜ ์ˆ˜ํ•™ ๋ฌธ์ œ ์–ด๋– ์‹ ๊ฐ€์š”๐Ÿ˜€(๊ฐ•์š” ์ ˆ๋Œ€ ์•„๋‹˜ ์ œ์•ˆ์ž…๋‹ˆ๋‹ค :))

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.

์ €๋„ BFS๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋Š”๋ฐ 3์ฐจ ๋ฐฐ์—ด๊นŒ์ง€ ์จ์•ผ ๋˜๋Š” ๋ณต์žกํ•œ ๋ฌธ์ œ์ธ ์ค„ ๋ชจ๋ฅด๊ณ  ์‚ฝ์งˆ๋งŒ ํ–ˆ๋„ค์š”....ใ…Ž๐Ÿ˜‚๐Ÿ˜‚

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

์ด๋ฒˆ 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.

3์ฐจ์› ๋ฆฌ์ŠคํŠธ๋Š” ์ฒ˜์Œ๋ด์„œ ๋„ˆ๋ฌด ์‹ ์„ ํ•˜๋„ค์š” ๐Ÿ˜ฎ 3์ฐจ์›์˜ ๊ฒฝ์šฐ ์ด๋™์€ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ•˜๋‚˜ ์‹ถ์—ˆ๋Š”๋ฐ offset์œผ๋กœ ์ •๋ง ๊น”๋”ํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜์…”์„œ ํฐ ๊ฐ๋ช…์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค...
์ด๋ฒˆ์ฐจ์‹œ PR๋„ ์ˆ˜๊ณ ํ•˜์…จ์–ด์š” ๐Ÿ‘๐Ÿป

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