Skip to content

Commit

Permalink
2024-04-01
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed Apr 2, 2024
1 parent 91b36eb commit 1c426f4
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 17 deletions.
97 changes: 97 additions & 0 deletions tgyuuAn/BFS/์—ด์‡ .py
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)
35 changes: 18 additions & 17 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@
| 25์ฐจ์‹œ | 2023.12.26 | ๊ตฌํ˜„ | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/49190#">๋ฐฉ์˜ ๊ฐœ์ˆ˜</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/90
| 26์ฐจ์‹œ | 2023.12.29 | BFS | <a href="https://www.acmicpc.net/problem/3197">๋ฐฑ์กฐ์˜ ํ˜ธ์ˆ˜</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/95
| 27์ฐจ์‹œ | 2024.01.01 | BFS | <a href="https://www.acmicpc.net/problem/9376">ํƒˆ์˜ฅ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/96
| 28์ฐจ์‹œ | 2023.01.04 | ์Šคํƒ | <a href="https://www.acmicpc.net/problem/6549">ํžˆ์Šคํ† ๊ทธ๋žจ์—์„œ ๊ฐ€์žฅ ํฐ ์ง์‚ฌ๊ฐํ˜•</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/99
| 29์ฐจ์‹œ | 2023.01.07 | ๊ทธ๋ฆฌ๋”” | <a href="https://www.acmicpc.net/problem/1083">์†ŒํŠธ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/103
| 30์ฐจ์‹œ | 2023.01.10 | BFS | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/87694#">์•„์ดํ…œ ์ค๊ธฐ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/104
| 31์ฐจ์‹œ | 2023.01.13 | DP | <a href="https://www.acmicpc.net/problem/17485">์ง„์šฐ์˜ ๋‹ฌ ์—ฌํ–‰ (Large)</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/105
| 32์ฐจ์‹œ | 2023.01.16 | ๊ทธ๋ฆฌ๋”” | <a href="https://www.acmicpc.net/problem/1700">๋ฉ€ํ‹ฐํƒญ ์Šค์ผ€์ค„๋ง</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/108
| 33์ฐจ์‹œ | 2023.01.19 | ์ด๋ถ„ ํƒ์ƒ‰ | <a href="https://www.acmicpc.net/problem/1981">๋ฐฐ์—ด์—์„œ ์ด๋™</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/115
| 34์ฐจ์‹œ | 2023.01.22 | ํž™ | <a href="https://www.acmicpc.net/problem/1655">๊ฐ€์šด๋ฐ๋ฅผ ๋งํ•ด์š”</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/116
| 35์ฐจ์‹œ | 2023.01.25 | ์ด๋ถ„ ํƒ์ƒ‰ | <a href="https://www.acmicpc.net/problem/2110">๊ณต์œ ๊ธฐ ์„ค์น˜</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/120
| 36์ฐจ์‹œ | 2023.02.04 | BFS | <a href="https://www.acmicpc.net/problem/4991">๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/126
| 37์ฐจ์‹œ | 2023.02.04 | BFS | <a href="https://www.acmicpc.net/problem/1039">๊ตํ™˜</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/131
| 38์ฐจ์‹œ | 2023.02.15 | DP | <a href="https://www.acmicpc.net/problem/2749">ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜ 3</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/138
| 39์ฐจ์‹œ | 2023.02.18 | DP | <a href="https://www.acmicpc.net/problem/7579">์•ฑ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/139
| 40์ฐจ์‹œ | 2023.02.21 | DP | <a href="https://www.acmicpc.net/problem/31413">์ž…๋Œ€</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/142
| 41์ฐจ์‹œ | 2023.03.04 | DP | <a href="https://www.acmicpc.net/problem/2240">์ž๋‘๋‚˜๋ฌด</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/148
| 42์ฐจ์‹œ | 2023.03.07 | DFS | <a href="https://www.acmicpc.net/problem/2239">์Šค๋„์ฟ </a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/152
| 28์ฐจ์‹œ | 2024.01.04 | ์Šคํƒ | <a href="https://www.acmicpc.net/problem/6549">ํžˆ์Šคํ† ๊ทธ๋žจ์—์„œ ๊ฐ€์žฅ ํฐ ์ง์‚ฌ๊ฐํ˜•</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/99
| 29์ฐจ์‹œ | 2024.01.07 | ๊ทธ๋ฆฌ๋”” | <a href="https://www.acmicpc.net/problem/1083">์†ŒํŠธ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/103
| 30์ฐจ์‹œ | 2024.01.10 | BFS | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/87694#">์•„์ดํ…œ ์ค๊ธฐ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/104
| 31์ฐจ์‹œ | 2024.01.13 | DP | <a href="https://www.acmicpc.net/problem/17485">์ง„์šฐ์˜ ๋‹ฌ ์—ฌํ–‰ (Large)</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/105
| 32์ฐจ์‹œ | 2024.01.16 | ๊ทธ๋ฆฌ๋”” | <a href="https://www.acmicpc.net/problem/1700">๋ฉ€ํ‹ฐํƒญ ์Šค์ผ€์ค„๋ง</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/108
| 33์ฐจ์‹œ | 2024.01.19 | ์ด๋ถ„ ํƒ์ƒ‰ | <a href="https://www.acmicpc.net/problem/1981">๋ฐฐ์—ด์—์„œ ์ด๋™</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/115
| 34์ฐจ์‹œ | 2024.01.22 | ํž™ | <a href="https://www.acmicpc.net/problem/1655">๊ฐ€์šด๋ฐ๋ฅผ ๋งํ•ด์š”</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/116
| 35์ฐจ์‹œ | 2024.01.25 | ์ด๋ถ„ ํƒ์ƒ‰ | <a href="https://www.acmicpc.net/problem/2110">๊ณต์œ ๊ธฐ ์„ค์น˜</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/120
| 36์ฐจ์‹œ | 2024.02.04 | BFS | <a href="https://www.acmicpc.net/problem/4991">๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/126
| 37์ฐจ์‹œ | 2024.02.04 | BFS | <a href="https://www.acmicpc.net/problem/1039">๊ตํ™˜</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/131
| 38์ฐจ์‹œ | 2024.02.15 | DP | <a href="https://www.acmicpc.net/problem/2749">ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜ 3</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/138
| 39์ฐจ์‹œ | 2024.02.18 | DP | <a href="https://www.acmicpc.net/problem/7579">์•ฑ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/139
| 40์ฐจ์‹œ | 2024.02.21 | DP | <a href="https://www.acmicpc.net/problem/31413">์ž…๋Œ€</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/142
| 41์ฐจ์‹œ | 2024.03.04 | DP | <a href="https://www.acmicpc.net/problem/2240">์ž๋‘๋‚˜๋ฌด</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/148
| 42์ฐจ์‹œ | 2024.03.07 | DFS | <a href="https://www.acmicpc.net/problem/2239">์Šค๋„์ฟ </a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/152
| 43์ฐจ์‹œ | 2024.03.10 | ์ด๋ถ„ ํƒ์ƒ‰ | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/64062">์ง•๊ฒ€๋‹ค๋ฆฌ ๊ฑด๋„ˆ๊ธฐ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/157
| 44์ฐจ์‹œ | 2023.03.13 | ํŠธ๋ผ์ด | <a href="https://www.acmicpc.net/problem/14725">๊ฐœ๋ฏธ๊ตด</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/159
| 45์ฐจ์‹œ | 2023.03.16 | ํŠธ๋ผ์ด | <a href="https://www.acmicpc.net/problem/31413">ํŠธ๋ผ์ด</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/162
| 44์ฐจ์‹œ | 2024.03.13 | ํŠธ๋ผ์ด | <a href="https://www.acmicpc.net/problem/14725">๊ฐœ๋ฏธ๊ตด</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/159
| 45์ฐจ์‹œ | 2024.03.16 | ํŠธ๋ผ์ด | <a href="https://www.acmicpc.net/problem/31413">ํŠธ๋ผ์ด</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/162
| 50์ฐจ์‹œ | 2024.04.01 | BFS | <a href="https://www.acmicpc.net/problem/9328">์—ด์‡ </a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/175
---

0 comments on commit 1c426f4

Please sign in to comment.