From 1c426f4e6bc1fbdb9ee83b8d13372706ab6cec9a Mon Sep 17 00:00:00 2001 From: tgyuu-An Date: Mon, 1 Apr 2024 10:50:46 +0900 Subject: [PATCH] 2024-04-01 --- "tgyuuAn/BFS/\354\227\264\354\207\240.py" | 97 +++++++++++++++++++++++ tgyuuAn/README.md | 35 ++++---- 2 files changed, 115 insertions(+), 17 deletions(-) create mode 100644 "tgyuuAn/BFS/\354\227\264\354\207\240.py" diff --git "a/tgyuuAn/BFS/\354\227\264\354\207\240.py" "b/tgyuuAn/BFS/\354\227\264\354\207\240.py" new file mode 100644 index 00000000..36494506 --- /dev/null +++ "b/tgyuuAn/BFS/\354\227\264\354\207\240.py" @@ -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) \ No newline at end of file diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index 5fd3dcb3..bf65daa1 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -29,22 +29,23 @@ | 25차시 | 2023.12.26 | 구현 | 방의 개수 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/90 | 26차시 | 2023.12.29 | BFS | 백조의 호수 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/95 | 27차시 | 2024.01.01 | BFS | 탈옥 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/96 -| 28차시 | 2023.01.04 | 스택 | 히스토그램에서 가장 큰 직사각형 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/99 -| 29차시 | 2023.01.07 | 그리디 | 소트 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/103 -| 30차시 | 2023.01.10 | BFS | 아이템 줍기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/104 -| 31차시 | 2023.01.13 | DP | 진우의 달 여행 (Large) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/105 -| 32차시 | 2023.01.16 | 그리디 | 멀티탭 스케줄링 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/108 -| 33차시 | 2023.01.19 | 이분 탐색 | 배열에서 이동 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/115 -| 34차시 | 2023.01.22 | 힙 | 가운데를 말해요 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/116 -| 35차시 | 2023.01.25 | 이분 탐색 | 공유기 설치 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/120 -| 36차시 | 2023.02.04 | BFS | 로봇 청소기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/126 -| 37차시 | 2023.02.04 | BFS | 교환 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/131 -| 38차시 | 2023.02.15 | DP | 피보나치 수 3 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/138 -| 39차시 | 2023.02.18 | DP | | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/139 -| 40차시 | 2023.02.21 | DP | 입대 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/142 -| 41차시 | 2023.03.04 | DP | 자두나무 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/148 -| 42차시 | 2023.03.07 | DFS | 스도쿠 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/152 +| 28차시 | 2024.01.04 | 스택 | 히스토그램에서 가장 큰 직사각형 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/99 +| 29차시 | 2024.01.07 | 그리디 | 소트 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/103 +| 30차시 | 2024.01.10 | BFS | 아이템 줍기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/104 +| 31차시 | 2024.01.13 | DP | 진우의 달 여행 (Large) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/105 +| 32차시 | 2024.01.16 | 그리디 | 멀티탭 스케줄링 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/108 +| 33차시 | 2024.01.19 | 이분 탐색 | 배열에서 이동 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/115 +| 34차시 | 2024.01.22 | 힙 | 가운데를 말해요 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/116 +| 35차시 | 2024.01.25 | 이분 탐색 | 공유기 설치 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/120 +| 36차시 | 2024.02.04 | BFS | 로봇 청소기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/126 +| 37차시 | 2024.02.04 | BFS | 교환 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/131 +| 38차시 | 2024.02.15 | DP | 피보나치 수 3 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/138 +| 39차시 | 2024.02.18 | DP | | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/139 +| 40차시 | 2024.02.21 | DP | 입대 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/142 +| 41차시 | 2024.03.04 | DP | 자두나무 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/148 +| 42차시 | 2024.03.07 | DFS | 스도쿠 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/152 | 43차시 | 2024.03.10 | 이분 탐색 | 징검다리 건너기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/157 -| 44차시 | 2023.03.13 | 트라이 | 개미굴 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/159 -| 45차시 | 2023.03.16 | 트라이 | 트라이 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/162 +| 44차시 | 2024.03.13 | 트라이 | 개미굴 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/159 +| 45차시 | 2024.03.16 | 트라이 | 트라이 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/162 +| 50차시 | 2024.04.01 | BFS | 열쇠 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/175 ---