diff --git "a/H0ngJu/DFS/\355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.py" "b/H0ngJu/DFS/\355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.py" new file mode 100644 index 00000000..7fb0fd9f --- /dev/null +++ "b/H0ngJu/DFS/\355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.py" @@ -0,0 +1,28 @@ +import sys +sys.setrecursionlimit(10**9) +def input() : return sys.stdin.readline().rstrip() + +n = int(input()) +tree = [[] for _ in range(n+1)] +visited = [-1] * (n+1) +visited[1] = 0 + +for i in range(n-1): + v, u, w = map(int, input().split()) + tree[v].append((u, w)) + tree[u].append((v, w)) + +def dfs(start, dis): + for node, node_dis in tree[start]: + if visited[node] == -1: + visited[node] = dis + node_dis + dfs(node, dis + node_dis) + +dfs(1,0) + +far_node = visited.index(max(visited)) +visited = [-1] * (n+1) +visited[far_node] = 0 +dfs(far_node, 0) + +print(max(visited)) \ No newline at end of file diff --git a/H0ngJu/README.md b/H0ngJu/README.md index 3546be9d..ca2eabf0 100644 --- a/H0ngJu/README.md +++ b/H0ngJu/README.md @@ -24,11 +24,10 @@ | 20차시 | 2024.06.03 | 백트래킹 | [스타트와 링크](https://www.acmicpc.net/problem/14889) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/206 | | 21차시 | 2024.06.07 | 그리디 | [행복 유치원](https://www.acmicpc.net/problem/13164) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/208 | | 22차시 | 2024.08.06 | 해시 | [의상](https://school.programmers.co.kr/learn/courses/30/lessons/42578) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/224 | -| 23차시 | 2024.08.10 | 해시 | [베스트앨범](https://school.programmers.co.kr/learn/courses/30/lessons/42579) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/227 | -| 24차시 | 2024.08.17 | BFS | [아기상어](https://www.acmicpc.net/problem/16236) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/233 | -| 25차시 | 2024.08.17 | DFS | [친구비](https://www.acmicpc.net/problem/16562) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/234 | -| 26차시 | 2024.08.24 | 그리디 | [신입사원](https://www.acmicpc.net/problem/1946) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/237 | - +| 23차시 | 2024.08.10 | 해시 | [베스트앨범](https://school.programmers.co.kr/learn/courses/30/lessons/42579) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/227 | +| 24차시 | 2024.08.17 | BFS | [아기상어](https://www.acmicpc.net/problem/16236) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/233 | +| 25차시 | 2024.08.17 | DFS | [친구비](https://www.acmicpc.net/problem/16562) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/234 | +| 26차시 | 2024.08.24 | 그리디 | [신입사원](https://www.acmicpc.net/problem/1946) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/237 | +| 27차시 | 2024.08.27 | DFS | [트리의 지름](https://www.acmicpc.net/problem/1967) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/240 | | 28차시 | 2024.09.04 | 벨만포드 | [타임머신](https://www.acmicpc.net/problem/11657) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/244 | - --- diff --git "a/H0ngJu/\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.py" "b/H0ngJu/\355\225\264\354\213\234/\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.py" similarity index 100% rename from "H0ngJu/\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.py" rename to "H0ngJu/\355\225\264\354\213\234/\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.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 aea5b0d5..cc839c9b 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -79,4 +79,6 @@ | 70차시 | 2024.08.16 | 스택 | 탑 보기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/232 | 71차시 | 2024.08.20 | 다익스트라 | 다익스트라 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/235 | 72차시 | 2024.08.23 | DFS + 트리 | 등산 마니아 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/238 +| 73차시 | 2024.08.26 | BFS | Rain (Small) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/239 +| 74차시 | 2024.08.30 | BFS | 불 켜기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/242 --- diff --git "a/tgyuuAn/\354\231\204\354\240\204 \355\203\220\354\203\211/\353\266\210 \354\274\234\352\270\260.py" "b/tgyuuAn/\354\231\204\354\240\204 \355\203\220\354\203\211/\353\266\210 \354\274\234\352\270\260.py" new file mode 100644 index 00000000..a29d53b4 --- /dev/null +++ "b/tgyuuAn/\354\231\204\354\240\204 \355\203\220\354\203\211/\353\266\210 \354\274\234\352\270\260.py" @@ -0,0 +1,46 @@ +from collections import deque, defaultdict +import sys + +def input(): return sys.stdin.readline().rstrip() + +N, M = map(int, input().split()) +board = [[False for _ in range(N+1)] for _ in range(N+1)] +board[1][1] = True + +switch = defaultdict(list) + +for _ in range(M): + x, y, a, b = map(int, input().split()) + switch[(x, y)].append((a,b)) + +deq = deque() +deq.append((1,1)) +dx = [0, 0, -1, 1] +dy = [-1, 1, 0, 0] +visited = {(1, 1),} +dedicates = {(1, 1),} + +while deq: + now_x, now_y = deq.popleft() + + for turn_on in switch[(now_x, now_y)]: + if turn_on not in dedicates: # <<<------- 이 코드 한줄에 3시간 날림 + dedicates.add(turn_on) + + if turn_on in visited: + deq.append(turn_on) + + for dir in range(4): + new_x = now_x + dx[dir] + new_y = now_y + dy[dir] + + if new_x <= 0 or new_x >= N+1: continue + if new_y <= 0 or new_y >= N+1: continue + if (new_x, new_y) in visited: continue + + visited.add((new_x, new_y)) + + if (new_x, new_y) in dedicates: + deq.append((new_x, new_y)) + +print(len(dedicates)) \ No newline at end of file