From 0285aa273ea4ebaed99d314b2f3844ebabe7504e Mon Sep 17 00:00:00 2001 From: tgyuuAn Date: Tue, 27 Aug 2024 04:32:25 +0900 Subject: [PATCH] 2024-08-26 --- tgyuuAn/BFS/Rain (Small).py | 53 +++++++++++++++++++++++++++++++++++++ tgyuuAn/README.md | 1 + 2 files changed, 54 insertions(+) create mode 100644 tgyuuAn/BFS/Rain (Small).py diff --git a/tgyuuAn/BFS/Rain (Small).py b/tgyuuAn/BFS/Rain (Small).py new file mode 100644 index 00000000..0e02ba03 --- /dev/null +++ b/tgyuuAn/BFS/Rain (Small).py @@ -0,0 +1,53 @@ +from collections import deque +import sys + +def input(): return sys.stdin.readline().rstrip() + +dx = [0, 0, -1, 1] +dy = [-1, 1, 0, 0] + +T = int(input()) + +for test_idx in range(T): + row, col = map(int, input().split()) + board = [list(map(int,input().split())) for _ in range(row)] + new_board = [[0 for _ in range(col)] for _ in range(row)] + + for height in range(1, 1001): + for start_row in range(row): + for start_col in range(col): + if board[start_row][start_col] >= height: continue + + deq = deque() + deq.append((start_row, start_col)) + history = {(start_row, start_col),} + + visited = set() + visited.add((start_row, start_col)) + + while deq: + now_row, now_col = deq.popleft() + + # 가장자리에 물이 흘렀다는 것이므로 break + if now_row in (0, row-1): break + if now_col in (0, col-1): break + + for dir in range(4): + new_row = now_row + dy[dir] + new_col = now_col + dx[dir] + + if new_row < 0 or new_row >= row: continue + if new_col < 0 or new_col >= col: continue + if (new_row, new_col) in visited: continue + if board[new_row][new_col] >= height: continue + + deq.append((new_row, new_col)) + history.add((new_row, new_col)) + visited.add((new_row, new_col)) + + # 가장자리에 물이 흐르지 않아 break되지 않았으면, + else: + for history_row, history_col in history: + new_board[history_row][history_col] = height - board[history_row][history_col] + + print(f"Case #{test_idx+1}: {sum([sum(row) for row in new_board])}") \ No newline at end of file diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index 73034d56..16b83c60 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -77,4 +77,5 @@ | 68차시 | 2024.08.06 | 그리디 | 가희와 탑 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/226 | 69차시 | 2024.08.10 | 누적합, 수학 | 1의 개수 세기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/228 | 70차시 | 2024.08.16 | 스택 | 탑 보기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/232 +| 73차시 | 2024.08.26 | BFS | Rain (Small) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/239 ---