DevYoon
[๋ฐฑ์ค] 9328. ์ด์ (Python) ๋ณธ๋ฌธ
link ๐ https://www.acmicpc.net/problem/9328
๐ ์ฃผ์ํ ์
- ์๊ทผ์ด๋ ๋น๋ฉ ๊ฐ์ฅ์๋ฆฌ์ ๋ฒฝ์ด ์๋ ๊ณณ์ ํตํด ๋น๋ฉ ์ํ์ ๋๋๋ค ์ ์๋๋ฐ, ๋ฌธ์ ์์ ์ฃผ์ด์ง๋ ๋น๋ฉ์ .์ผ๋ก ๊ฐ์ธ ๋ค์ด๊ฐ ์ ๊ตฌ๋ฅผ ๋ง๋ จํด์ฃผ์๋ค.
- ์ด์ ๋ฅผ ์ฃผ์ ์ผ๋ฉด ์ด์ ๊ป ๊ฐ์ง ๋ชปํ๋ ๊ณณ์ ๊ฐ ์ ์์ผ๋ฏ๋ก (๋ฌธ์ ์ด์ด์) 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 ์ฌ์ฉ!