ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 4179 ๋ถˆ! with Python
    PS 2021. 8. 28. 01:22
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 4179 ๋ถˆ!

    ๐Ÿ’ก ์กฐ๊ฑด ๋ฐ ํ’€์ด

    1. R * C ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ์ง€ํ›ˆ์ด๊ฐ€ ๋ฏธ๋กœ์—์„œ ํƒˆ์ถœ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ.
    2. R * C ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์€ ์ตœ๋Œ€ 1000 * 1000
    3. BFS ์œ ํ˜•์˜ ๋ฌธ์ œ
    4. ๋ฏธ๋กœ์˜ ๋ฒฝ์— ๋ถ™์–ด์žˆ์œผ๋ฉด ํƒˆ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
    5. ๋ถˆ์„ ๋จผ์ € ์ง€๋ฅธ ํ›„, ์ง€ํ›ˆ์ด์˜ ์ด๋™ ๊ฐ€๋Šฅ ๊ฒฝ๋กœ๋ฅผ ์‚ดํ•€๋‹ค.
    6. ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด ํ•œ ๋ฒˆ ๊ฐ”๋˜ ๊ณณ์€ ๋‹ค์‹œ ๊ฐ€์ง€ ์•Š๋Š”๋‹ค.

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin
    from collections import deque
    
    dx, dy = [1, -1, 0, 0], [0, 0, 1, -1]
    r, c = map(int, stdin.readline().split())
    board, res = [], 0
    
    # ์ง€ํ™˜์ด์™€ ๋ถˆ๋‚œ ๊ณณ ์ €์žฅํ•  ๋ณ€์ˆ˜
    F, J = deque(), deque()
    
    for i in range(r):
        data = list(stdin.readline().rstrip())
        for j in range(c):
            if data[j] == 'J':
                J.append((i, j))
            if data[j] == 'F':
                F.append((i, j))
    
        board.append(data)
    
    
    def bfs():
        global F, J, res
    
        while 1:
            res += 1
            temp = []
            while F:
                x, y = F.popleft()
                for i in range(4):
                    nx, ny = x + dx[i], y + dy[i]
                    if -1 < nx < r and -1 < ny < c:
                        if board[nx][ny] == '.' or board[nx][ny] == '$':
                            temp.append((nx, ny))
                            board[nx][ny] = 'F'
            F = deque(temp)
    
            temp = []
            while J:
                x, y = J.popleft()
                if x == 0 or y == 0 or x == r - 1 or y == c - 1:
                    return res
    
                for i in range(4):
                    nx, ny = dx[i] + x, dy[i] + y
                    if -1 < nx < r and -1 < ny < c and board[nx][ny] == '.':
                        temp.append((nx, ny))
                        board[x][y] = '$'
                        board[nx][ny] = 'J'
    
            J = deque(temp)
            if not J:
                return False
    
    
    if bfs():
        print(res)
    else:
        print('IMPOSSIBLE')

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    4 4
    ####
    #JF#
    #..#
    #..#

    ์‹คํ–‰๊ฒฐ๊ณผ

    3

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. ๊ณผ์ •๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ์šฉ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ์™€ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋–ด๋‹ค.
    2. ์ž„์‹œ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ๋ถˆ๊ณผ ์ง€ํ›ˆ์ด๊ฐ€ ์›€์ง์ผ ๋•Œ ์ด๋™ ๊ฐ€๋Šฅํ•œ ๊ฒฝ๋กœ๋ฅผ ๋ชจ๋‘ ๋„ฃ์–ด์ฃผ๊ณ  while ๋ฌธ์ด ์ข…๋ฃŒ๋˜์—ˆ์„ ๋•Œ ํ์— ์‚ฝ์ž…
    3. ์ง€ํ›ˆ์ด๊ฐ€ ์ง€๋‚˜๊ฐ„ ๊ณณ์€ '$'๋กœ ๋ณ€๊ฒฝ
    4. ๋ถˆ์€ '$' ์™€ '.' ๋ฅผ, ์ง€ํ›ˆ์ด๋Š” ์˜ค๋กœ์ง€ '.' ๋งŒ ๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ์ฒ˜๋ฆฌ.
    5. ์ง€ํ›ˆ์ด๊ฐ€ ์›€์ง์ผ ๋•Œ, Queue ์—์„œ ํ˜„์žฌ ์ง€ํ›ˆ์ด์˜ ์ขŒํ‘œ๋ฅผ ๋ฝ‘์•„, ๋ฒฝ์— ์œ„์น˜ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ํƒˆ์ถœ ์„ฑ๊ณต.
    6. if x == 0 or y == 0 or x == r - 1 or y == c - 1:
    7. ๋” ์ด์ƒ ์ง€ํ›ˆ์ด๊ฐ€ ์›€์ง์ผ ๊ฒฝ๋กœ๊ฐ€ ์—†๋‹ค๋ฉด ํƒˆ์ถœ ์‹คํŒจ
    8. if not J:

    ๐Ÿ’พ ๋Š๋‚€์ 

    • BFS ์œ ํ˜• ๋ฌธ์ œ์—์„œ ๋‚œ์ด๋„๊ฐ€ ์‹ค๋ฒ„2 ~ ๊ณจ๋“œ4 ๋กœ๋งŒ ์˜ฌ๋ผ๊ฐ€๋„ ํ—ค๋งค๋Š” ๋ชจ์Šต์„ ๋ณด์˜€๋‹ค.
    • ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ์šฉ ๋ฐฐ์—ด์˜ ๋‹ค์–‘ํ•œ ์‚ฌ์šฉ๋ฒ•์„ ๋ˆˆ์— ์ตํžˆ๊ณ  ์‘์šฉํ•  ์ค„ ์•Œ์•„์•ผ๊ฒ ๋‹ค.
    • ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ์šฉ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „, ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋œฐ ๊ฐ์ธ์ง€ ์žด ์ค„ ์•Œ์•„์•ผ๊ฒ ๋‹ค.
    • ๋ฌธ์ œ๋ฅผ ๋‚˜๋ฆ„๋Œ€๋กœ ํ•ด์„ํ•˜๊ณ  ์••์ถ•ํ•˜๋Š” ๋Šฅ๋ ฅ์„ ๋” ํ‚ค์›Œ์•ผ๊ฒ ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.