DevYoon

[๋ฐฑ์ค€] 9328. ์—ด์‡  (Python) ๋ณธ๋ฌธ

PS/Baekjoon

[๋ฐฑ์ค€] 9328. ์—ด์‡  (Python)

gimewn 2022. 8. 31. 00:56

link ๐Ÿ”— https://www.acmicpc.net/problem/9328

 

9328๋ฒˆ: ์—ด์‡ 

์ƒ๊ทผ์ด๋Š” 1์ธต ๋นŒ๋”ฉ์— ์นจ์ž…ํ•ด ๋งค์šฐ ์ค‘์š”ํ•œ ๋ฌธ์„œ๋ฅผ ํ›”์ณ์˜ค๋ ค๊ณ  ํ•œ๋‹ค. ์ƒ๊ทผ์ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ‰๋ฉด๋„์—๋Š” ๋ฌธ์„œ์˜ ์œ„์น˜๊ฐ€ ๋ชจ๋‘ ๋‚˜ํƒ€๋‚˜ ์žˆ๋‹ค. ๋นŒ๋”ฉ์˜ ๋ฌธ์€ ๋ชจ๋‘ ์ž ๊ฒจ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋ฌธ์„ ์—ด๋ ค๋ฉด ์—ด์‡ ๊ฐ€

www.acmicpc.net

 

๐Ÿ“Œ ์ฃผ์˜ํ•  ์ 

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

- ์—ด์‡ ๋ฅผ ์ฃผ์› ์œผ๋ฉด ์ด์ œ๊ป ๊ฐ€์ง€ ๋ชปํ–ˆ๋˜ ๊ณณ์„ ๊ฐˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ (๋ฌธ์„ ์—ด์–ด์„œ) visit๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค.

 

๐Ÿ–ฅ๏ธ Pypy3 ํ†ต๊ณผ

import sys
from collections import deque

input = sys.stdin.readline
dir = [(-1, 0), (1, 0), (0, -1), (0, 1)]

def BFS():
    global doc
    q = deque()
    q.append((0, 0))
    visit = [[0]*(X+2) for _ in range(Y+2)]
    visit[0][0] = 1
    while q:
        ny, nx = q.popleft()
        for i, j in dir:
            dy, dx = ny+i, nx+j
            if 0 <= dy < Y+2 and 0 <= dx < X+2:
                if visit[dy][dx] == 1: continue
                if board[dy][dx] == '.':
                    visit[dy][dx] = 1
                    q.append((dy, dx))
                elif board[dy][dx] == '$': # ๋ฌธ์„œ์ด๋ฉด
                    board[dy][dx] = '.'
                    visit[dy][dx] = 1
                    doc += 1 # ์ฃผ์›€
                    q.append((dy, dx))
                elif board[dy][dx].islower(): # ํ‚ค ๋ฐœ๊ฒฌ ์‹œ
                    door[ord(board[dy][dx])-ord('a')] = 1 # ๋ฌธ ๋ฆฌ์ŠคํŠธ์— ํ‘œ์‹œ
                    visit = [[0]*(X+2) for _ in range(Y+2)] # visit ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
                    board[dy][dx] = '.'
                    q.append((dy, dx))
                elif board[dy][dx].isupper(): # ๋ฐฉ์ด๋ฉด
                    if door[ord(board[dy][dx])-ord('A')]: # ์—ด์‡  ์žˆ์œผ๋ฉด
                        board[dy][dx] = '.'
                        visit[dy][dx] = 1
                        q.append((dy, dx))
                else:
                    continue

T = int(input())

for tc in range(T):
    Y, X = map(int, input().split())
    board = [[] for _ in range(Y+2)]
    board[0] = board[Y+1] = ['.']*(X+2)
    door = [0]*26
    doc = 0 # ํ›”์นœ ๋ฌธ์„œ ๊ฐฏ์ˆ˜
    for idx in range(1, Y+1): # ์ƒˆ๋กœ์šด ๋งต
        temp_lst = list(input())
        temp_lst.pop() # sys => ์ฒ˜๋ฆฌ
        board[idx] = ['.'] + temp_lst + ['.']
    temp = list(input())
    temp.pop() # sys => ์ฒ˜๋ฆฌ
    for k in temp:
        if k != '0':
            door[ord(k)-ord('a')] = 1 # ๋ฌธ์— ์—ด์‡  ์žˆ๋‹ค๊ณ  ํ‘œ์‹œ
    for y in range(Y+2):
        for x in range(X+2):
            if ord(board[y][x]) >= ord('A') and ord(board[y][x]) <= ord('Z'): # ๋ฐฉ์ด๋ฉด
                if door[ord(board[y][x])-ord('A')]: # ๋ณด์œ  ์ค‘์ธ ์—ด์‡ ๋กœ ์—ด๋ฆฌ๋ฉด ์—ด๊ธฐ
                    board[y][x] = '.'
    BFS()
    print(doc)

- ๋‹ต์€ ๋งž์•˜์œผ๋‚˜, Python3๋กœ๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๋Š” ๊ฒŒ ์‹ ๊ฒฝ์“ฐ์˜€๋‹ค.

- Python3๋กœ๋„ ํ†ต๊ณผํ•˜๊ฒŒ ํ•  ์ˆœ ์—†์„๊นŒ?๐Ÿค” ๊ณ ๋ฏผํ•˜๋ฉฐ ์—ฌ๋Ÿฌ ์‹œ๋„ ๋ฐ ๊ตฌ๊ธ€๋งํ•œ ๊ฒฐ๊ณผ, ํ˜„์žฌ ์ฝ”๋“œ์—์„œ๋Š” BFS ๋‚ด์—์„œ visit ๋ฐฐ์—ด๋งŒ ์ดˆ๊ธฐํ™”ํ•ด์ค€ ๋’ค ๊ณ„์† ๋‹ค์‹œ ๋Œ๋ฆฌ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž„์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

 

๐Ÿ“Œ ๋ฐฉ๋ฒ• 1 (Python 3 ํ†ต๊ณผ) : q ์ดˆ๊ธฐํ™”

- ์œ„์˜ ์ฝ”๋“œ์—์„œ ์—ด์‡ ๋ฅผ ์ฃผ์›Œ์„œ visit์„ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ q๋„ ํ•จ๊ป˜ ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค.

import sys
from collections import deque

input = sys.stdin.readline
dir = [(-1, 0), (1, 0), (0, -1), (0, 1)]

def BFS():
    global doc
    q = deque()
    q.append((0, 0))
    visit = [[0]*(X+2) for _ in range(Y+2)]
    visit[0][0] = 1
    while q:
        ny, nx = q.popleft()
        for i, j in dir:
            dy, dx = ny+i, nx+j
            if 0 <= dy < Y+2 and 0 <= dx < X+2:
                if visit[dy][dx] == 1: continue
                if board[dy][dx] == '.':
                    visit[dy][dx] = 1
                    q.append((dy, dx))
                elif board[dy][dx] == '$': # ๋ฌธ์„œ์ด๋ฉด
                    board[dy][dx] = '.'
                    visit[dy][dx] = 1
                    doc += 1 # ์ฃผ์›€
                    q.append((dy, dx))
                elif board[dy][dx].islower(): # ํ‚ค ๋ฐœ๊ฒฌ ์‹œ
                    door[ord(board[dy][dx])-ord('a')] = 1 # ๋ฌธ ๋ฆฌ์ŠคํŠธ์— ํ‘œ์‹œ
                    visit = [[0]*(X+2) for _ in range(Y+2)] # visit ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
                    q.clear()
                    board[dy][dx] = '.'
                    q.append((dy, dx))
                elif board[dy][dx].isupper(): # ๋ฐฉ์ด๋ฉด
                    if door[ord(board[dy][dx])-ord('A')]: # ์—ด์‡  ์žˆ์œผ๋ฉด
                        board[dy][dx] = '.'
                        visit[dy][dx] = 1
                        q.append((dy, dx))
                else:
                    continue

T = int(input())

for tc in range(T):
    Y, X = map(int, input().split())
    board = [[] for _ in range(Y+2)]
    board[0] = board[Y+1] = ['.']*(X+2)
    door = [0]*26
    doc = 0 # ํ›”์นœ ๋ฌธ์„œ ๊ฐฏ์ˆ˜
    for idx in range(1, Y+1): # ์ƒˆ๋กœ์šด ๋งต
        temp_lst = list(input())
        temp_lst.pop() # sys => ์ฒ˜๋ฆฌ
        board[idx] = ['.'] + temp_lst + ['.']
    temp = list(input())
    temp.pop() # sys => ์ฒ˜๋ฆฌ
    for k in temp:
        if k != '0':
            door[ord(k)-ord('a')] = 1 # ๋ฌธ์— ์—ด์‡  ์žˆ๋‹ค๊ณ  ํ‘œ์‹œ
    for y in range(Y+2):
        for x in range(X+2):
            if ord(board[y][x]) >= ord('A') and ord(board[y][x]) <= ord('Z'): # ๋ฐฉ์ด๋ฉด
                if door[ord(board[y][x])-ord('A')]: # ๋ณด์œ  ์ค‘์ธ ์—ด์‡ ๋กœ ์—ด๋ฆฌ๋ฉด ์—ด๊ธฐ
                    board[y][x] = '.'
    BFS()
    print(doc)

 

๐Ÿ“Œ ๋ฐฉ๋ฒ• 2 (Python 3 ํ†ต๊ณผ) : BFS ์ข…๋ฃŒ

- ์œ„์˜ ์ฝ”๋“œ์™€ ๋‹ฌ๋ฆฌ ์—ด์‡ ๋ฅผ ์ฃผ์› ์„ ๋•Œ visit์„ ์ดˆ๊ธฐํ™”ํ•˜๊ฑฐ๋‚˜ ํ•˜์ง€ ์•Š๊ณ , door ๋ฆฌ์ŠคํŠธ์— ์ฃผ์› ์Œ์„ ํ‘œ์‹œํ•ด์ค€๋‹ค.

- ํ‚ค๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ๋•Œ๊นŒ์ง€ BFS๋ฅผ ๊ณ„์† ๋Œ๋ ค์ค€๋‹ค.

import sys
from collections import deque

input = sys.stdin.readline
dir = [(-1, 0), (1, 0), (0, -1), (0, 1)]

def unlock():
    global door
    if sum(door):
        for y in range(Y+2):
            for x in range(X+2):
                if board[y][x].isupper():
                    if door[ord(board[y][x])-ord('A')]:
                        board[y][x] = '.'
        door = [0]*26
def BFS():
    global doc
    q = deque()
    q.append((0, 0))
    visit = [[0]*(X+2) for _ in range(Y+2)]
    visit[0][0] = 1
    while q:
        ny, nx = q.popleft()
        for i, j in dir:
            dy, dx = ny+i, nx+j
            if 0 <= dy < Y+2 and 0 <= dx < X+2:
                if visit[dy][dx] == 1: continue
                if board[dy][dx] == '*': continue
                elif board[dy][dx].isupper():continue
                elif board[dy][dx] == '$': # ๋ฌธ์„œ๋ฉด ์ค๊ธฐ
                    doc += 1
                elif board[dy][dx].islower(): # ํ‚ค ๋ฐœ๊ฒฌ ์‹œ
                    door[ord(board[dy][dx])-ord('a')] = 1 # ๋ฌธ ๋ฆฌ์ŠคํŠธ์— ํ‘œ์‹œ
                board[dy][dx] = '.'
                visit[dy][dx] = 1
                q.append((dy, dx))

T = int(input())

for tc in range(T):
    Y, X = map(int, input().split())
    board = [[] for _ in range(Y+2)]
    board[0] = board[Y+1] = ['.']*(X+2)
    door = [0]*26
    doc = 0 # ํ›”์นœ ๋ฌธ์„œ ๊ฐฏ์ˆ˜
    for idx in range(1, Y+1): # ์ƒˆ๋กœ์šด ๋งต
        temp_lst = list(input())
        temp_lst.pop() # sys => ์ฒ˜๋ฆฌ
        board[idx] = ['.'] + temp_lst + ['.']
    temp = list(input())
    temp.pop() # sys => ์ฒ˜๋ฆฌ
    for k in temp:
        if k != '0':
            door[ord(k)-ord('a')] = 1 # ๋ฌธ์— ์—ด์‡  ์žˆ๋‹ค๊ณ  ํ‘œ์‹œ
    for y in range(Y+2):
        for x in range(X+2):
            if ord(board[y][x]) >= ord('A') and ord(board[y][x]) <= ord('Z'): # ๋ฐฉ์ด๋ฉด
                if door[ord(board[y][x])-ord('A')]: # ๋ณด์œ  ์ค‘์ธ ์—ด์‡ ๋กœ ์—ด๋ฆฌ๋ฉด ์—ด๊ธฐ
                    board[y][x] = '.'
    while True:
        unlock()
        BFS()
        if sum(door) == 0:
            break
    print(doc)

 

โœ๏ธ ๋ฐฉ๋ฒ• 1(1272ms)๋ณด๋‹ค๋Š” ๋ฐฉ๋ฒ• 2(920ms)๊ฐ€ ์‹œ๊ฐ„์ด ๋” ์ ๊ฒŒ ๊ฑธ๋ ธ๊ณ , ๋ฉ”๋ชจ๋ฆฌ๋Š” ๋™์ผํ•˜๊ฒŒ 32580kb ์‚ฌ์šฉ!