-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
115 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import sys | ||
from collections import deque, defaultdict | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
dx = [0, 0, -1, 1] | ||
dy = [-1, 1, 0, 0] | ||
|
||
T = int(input()) | ||
|
||
for _ in range(T): | ||
|
||
H, W = map(int, input().split()) | ||
building = [["." for _ in range(W+2)]] | ||
door_info = defaultdict(set) | ||
keys_i_have = set() | ||
|
||
for row in range(H): | ||
_input = "." | ||
_input += input() | ||
_input += "." | ||
|
||
for col in range(W+2): | ||
if _input[col] not in ("*", ".", "$") and _input[col].isupper(): | ||
door_info[_input[col]].add((row+1, col)) | ||
|
||
building.append(list(_input)) | ||
|
||
building.append(["." for _ in range(W+2)]) | ||
|
||
keys_info = input() | ||
if keys_info != "0": | ||
keys_i_have.update(set(keys_info)) | ||
|
||
answer = 0 | ||
visited = set() | ||
locked_doors_to_access = set() | ||
|
||
deq = deque([(0, 0)]) | ||
while deq: | ||
now_row, now_col = deq.popleft() | ||
|
||
for dir in range(4): | ||
new_row = now_row + dy[dir] | ||
new_col = now_col + dx[dir] | ||
|
||
if new_row < 0 or new_row >= H+2: continue | ||
if new_col < 0 or new_col >= W+2: continue | ||
if (new_row, new_col) in visited: continue | ||
if building[new_row][new_col] == "*": continue | ||
|
||
# print(now_row, now_col,building[new_row][new_col]) | ||
# print(locked_doors_to_access) | ||
# print(keys_i_have) | ||
# print() | ||
|
||
if building[new_row][new_col] == "$": | ||
answer += 1 | ||
visited.add((new_row, new_col)) | ||
deq.append((new_row, new_col)) | ||
continue | ||
|
||
# ๋ฌธ์ ๋ง๋ฌ์ ๊ฒฝ์ฐ, ์ด ๋ ๊น์ง ์ป์ ์ด์ ๋ก ์ด ์ ์๋ ์ง ํ์ธํจ. ์๋ ๊ฒฝ์ฐ ์ ๊ทผํ ์ ์๋ ๋ฌธ ๋ชฉ๋ก์ ์ถ๊ฐ | ||
if building[new_row][new_col].isalpha() and building[new_row][new_col].isupper(): | ||
|
||
# ์ด์ ๋ฅผ ์ด๋ฏธ ๊ฐ์ง๊ณ ์๋ ๊ฒฝ์ฐ | ||
if building[new_row][new_col].lower() in keys_i_have: | ||
building[new_row][new_col] = "." | ||
visited.add((new_row, new_col)) | ||
deq.append((new_row, new_col)) | ||
|
||
# ์ด์ ๊ฐ ์์ด์ ๋ฌธ์ ๋ชป ์ด ๊ฒฝ์ฐ ์ ๊ทผํ ์ ์๋ ๋ฌธ ๋ชฉ๋ก์ ์ถ๊ฐ | ||
else: locked_doors_to_access.add((new_row, new_col)) | ||
|
||
continue | ||
|
||
# ์ด์ ๋ฅผ ํ๋ํ์ ๊ฒฝ์ฐ, ์ด ๋ ๊น์ง ๋ง๋ ๋ฌธ๋ค ์ค์ ์ด ์ ์๋ ๊ฒ๋ค์ queue์ ๋ฃ์ | ||
if building[new_row][new_col].isalpha() and building[new_row][new_col].islower(): | ||
keys_i_have.add(building[new_row][new_col]) | ||
visited.add((new_row, new_col)) | ||
deq.append((new_row, new_col)) | ||
|
||
for can_open_row, can_open_col in door_info[building[new_row][new_col].upper()]: | ||
if (can_open_row, can_open_col) in locked_doors_to_access: | ||
building[can_open_row][can_open_col] = "." | ||
visited.add((can_open_row, can_open_col)) | ||
deq.append((can_open_row, can_open_col)) | ||
locked_doors_to_access.discard((can_open_row, can_open_col)) | ||
|
||
continue | ||
|
||
# ๋น ๊ณต๊ฐ์ผ ๊ฒฝ์ฐ, ๊ทธ๋ฅ ์ง๋๊ฐ | ||
if building[new_row][new_col] == ".": | ||
visited.add((new_row, new_col)) | ||
deq.append((new_row, new_col)) | ||
|
||
print(answer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters