Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

50-tgyuuAn #175

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
31 changes: 16 additions & 15 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,27 @@
| 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
| 46μ°¨μ‹œ | 2023.03.20 | 트라이 | <a href="https://www.acmicpc.net/problem/27652">AB</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/165
| 47μ°¨μ‹œ | 2023.03.22 | μˆ˜ν•™, 뢄할정볡 | <a href="https://www.acmicpc.net/problem/15824">λ„ˆ λ΄„μ—λŠ” 캑사이신이 λ§›μžˆλ‹¨λ‹€</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/167
| 48μ°¨μ‹œ | 2023.03.25 | 벨만 ν¬λ“œ | <a href="https://www.acmicpc.net/problem/1738">골λͺ©κΈΈ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/171
| 49μ°¨μ‹œ | 2023.03.29 | DP | <a href="https://www.acmicpc.net/problem/10217">KCM Travel</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/174
| 50μ°¨μ‹œ | 2024.04.01 | BFS | <a href="https://www.acmicpc.net/problem/9328">μ—΄μ‡ </a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/175
---