From 9219428da5d3d8647883c93893fc5b5d8c3a3071 Mon Sep 17 00:00:00 2001 From: TaehyungKim <91403729+clarityth@users.noreply.github.com> Date: Thu, 30 Jul 2026 15:49:56 +0900 Subject: [PATCH 1/7] =?UTF-8?q?solve:=20[W02]=20SWEA=205658=20=EB=B3=B4?= =?UTF-8?q?=EB=AC=BC=EC=83=81=EC=9E=90=20=EB=B9=84=EB=B0=80=EB=B2=88?= =?UTF-8?q?=ED=98=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\353\260\200\353\262\210\355\230\270.md" | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 "studies/week-02/clarityth/5658-\353\263\264\353\254\274\354\203\201\354\236\220\353\271\204\353\260\200\353\262\210\355\230\270.md" diff --git "a/studies/week-02/clarityth/5658-\353\263\264\353\254\274\354\203\201\354\236\220\353\271\204\353\260\200\353\262\210\355\230\270.md" "b/studies/week-02/clarityth/5658-\353\263\264\353\254\274\354\203\201\354\236\220\353\271\204\353\260\200\353\262\210\355\230\270.md" new file mode 100644 index 0000000..64ff1ce --- /dev/null +++ "b/studies/week-02/clarityth/5658-\353\263\264\353\254\274\354\203\201\354\236\220\353\271\204\353\260\200\353\262\210\355\230\270.md" @@ -0,0 +1,87 @@ + + +# [SWEA 5658] [λͺ¨μ˜ SW μ—­λŸ‰ν…ŒμŠ€νŠΈ] λ³΄λ¬Όμƒμž λΉ„λ°€λ²ˆν˜Έ + +| ν•­λͺ© | λ‚΄μš© | +|:---|:---| +| πŸ”— 문제 | https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWXRUN9KfZ8DFAUo | +| πŸ“š 주제 | κ΅¬ν˜„, λ¬Έμžμ—΄ | +| 🎚️ λ‚œμ΄λ„ | λͺ¨μ˜ SW μ—­λŸ‰ν…ŒμŠ€νŠΈ | +| ⏱️ μ†Œμš” μ‹œκ°„ | 30λΆ„ | +| πŸ” λ‹€μ‹œ ν’€κΈ° | μ•„λ‹ˆμ˜€ | + +--- + +## 1. μ ‘κ·Ό 방법 + +* λ‹¨μˆœ κ΅¬ν˜„ 및 λ¬Έμžμ—΄ μ‘°μž‘ μœ ν˜•μœΌλ‘œ νŒλ‹¨ +* ν•œ λ³€μ˜ 길이가 $N/4$μ΄λ―€λ‘œ $N/4$번 νšŒμ „ν•˜λ©΄ λͺ¨λ“  κ°€λŠ₯ν•œ 수 μ‘°ν•© μΆ”μΆœ +* `set`으둜 쀑볡 제거 ν›„ λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ν•˜μ—¬ $K$번째 큰 수λ₯Ό 10μ§„μˆ˜λ‘œ λ³€ν™˜ + + +## 2. μ‹œκ°„ λ³΅μž‘λ„ + + + +**O(NΒ² log N)** β€” +* **νšŒμ „ 및 μŠ¬λΌμ΄μ‹±**: $N/4$회 νšŒμ „ $\times$ λ¬Έμžμ—΄ μŠ¬λΌμ΄μ‹± = $O(N)$ +* **μ •λ ¬**: μ΅œλŒ€ $N$개 수 λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ = $O(N \log N)$ +* **10μ§„μˆ˜ λ³€ν™˜**: $O(N)$ + +## 3. 곡간 λ³΅μž‘λ„ + + +**O(N)** β€” +* μ΅œλŒ€ $N$개 16μ§„μˆ˜ λ¬Έμžμ—΄ μ €μž₯용 `set` 및 `list` μ‚¬μš©ν•¨. + + +## 4. 핡심 풀이 방법 ⭐ + +* **νšŒμ „ μ£ΌκΈ°μ„±**: $N/4$번 νšŒμ „ μ‹œ λͺ¨λ“  16μ§„μˆ˜ 수 μΆ”μΆœ +* **`set` ν™œμš©**: 쀑볡 제거 +* **10μ§„μˆ˜ λ³€ν™˜**: μžλ¦Ώμˆ˜λ³„ `16^p` κ³±ν•˜μ—¬ ν•©μ‚° + + +## 5. μ‹€μ œ μ½”λ“œ + + +```python +T = int(input()) + +for test_case in range(1, T+1): + N, K = map(int, input().split()) + input_str = input() + + rotated_nums = set() + for i in range(N // 4): + rotate_str = (input_str[-i:] + input_str[:-i]) + for j in range(0, len(rotate_str), N // 4): + rotated_nums.add(rotate_str[j : j + N // 4]) + + sorted_rotated_nums = sorted(list(rotated_nums), reverse=True) + + def hex_to_decimal(hex_str): + ans = 0 + offset = 1 + for i in range(len(hex_str) - 1, -1, -1): + c = hex_str[i] + if c.isdigit(): + ans += int(c) * offset + elif c.isalpha(): + ans += ((ord(c) - ord('A')) + 10) * offset + offset *= 16 + return ans + + print(f"#{test_case} {hex_to_decimal(sorted_rotated_nums[K-1])}") +``` + +--- + +### 🧠 회고 (선택) + +* `ord()` 및 진법 λ³€ν™˜ 원리λ₯Ό 볡슡 +* 파이썬 λ‚΄μž₯ν•¨μˆ˜ `int(str, 16)`을 μ“°λ©΄ 더 κ°„κ²°νžˆ ν’€ 수 μžˆλ‹€. From f8b973ed8d68a2840206f4921e9e1ef282c776c1 Mon Sep 17 00:00:00 2001 From: TaehyungKim <91403729+clarityth@users.noreply.github.com> Date: Thu, 30 Jul 2026 16:13:31 +0900 Subject: [PATCH 2/7] =?UTF-8?q?solve:=20[W02]=20SWEA=2026071=20=EB=B8=94?= =?UTF-8?q?=EB=A1=9D=20=EC=A0=9C=EA=B1=B0=20=EA=B2=8C=EC=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...34\352\261\260\352\262\214\354\236\204.md" | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 "studies/week-02/clarityth/26071-\353\270\224\353\241\235\354\240\234\352\261\260\352\262\214\354\236\204.md" diff --git "a/studies/week-02/clarityth/26071-\353\270\224\353\241\235\354\240\234\352\261\260\352\262\214\354\236\204.md" "b/studies/week-02/clarityth/26071-\353\270\224\353\241\235\354\240\234\352\261\260\352\262\214\354\236\204.md" new file mode 100644 index 0000000..e3c5586 --- /dev/null +++ "b/studies/week-02/clarityth/26071-\353\270\224\353\241\235\354\240\234\352\261\260\352\262\214\354\236\204.md" @@ -0,0 +1,113 @@ + + +# [SWEA 26071] 블둝 제거 κ²Œμž„ + +| ν•­λͺ© | λ‚΄μš© | +|:---|:---| +| πŸ”— 문제 | https://swexpertacademy.com/main/code/userProblem/userProblemDetail.do?contestProbId=AZwmCVWq3uLHBIT3 | +| πŸ“š 주제 | DP (ꡬ간 DP / Top-Down λ©”λͺ¨μ΄μ œμ΄μ…˜) | +| 🎚️ λ‚œμ΄λ„ | SWEA λͺ¨μ˜/D4 | +| ⏱️ μ†Œμš” μ‹œκ°„ | 3μ‹œκ°„ | +| πŸ” λ‹€μ‹œ ν’€κΈ° | 예 | + +--- + +## 1. μ ‘κ·Ό 방법 + +* μ²˜μŒμ—” permutations μ΄μš©ν•΄ 블둝을 κ³ λ₯΄λŠ” 경우λ₯Ό λͺ¨λ‘ κ³„μ‚°ν•˜λ €ν–ˆμ§€λ§Œ, +* 순차적으둜 λ¨Όμ € μ œκ±°ν•  블둝을 κ³ λ₯΄λ©΄ 남은 블둝듀이 μΈμ ‘ν•˜κ²Œ λ³€ν•΄ 경우의 μˆ˜κ°€ $N!$둜 ν­λ°œν•˜κ³  μƒνƒœ 관리가 μ–΄λ €μ›Œμ§ +* **μ—­λ°œμƒ (ꡬ간 DP)**: νŠΉμ • ꡬ간 `[start, end]`μ—μ„œ κ°€μž₯ λ§ˆμ§€λ§‰μ— μ œκ±°ν•  블둝 $k$λ₯Ό κΈ°μ€€μœΌλ‘œ 생각 +* $k$번째 블둝이 ν•΄λ‹Ή κ΅¬κ°„μ—μ„œ 제일 λ§ˆμ§€λ§‰μœΌλ‘œ μ œκ±°λœλ‹€λ©΄, $k$의 쒌/우 이웃은 항상 ꡬ간 외뢀인 `blocks[start - 1]`κ³Ό `blocks[end + 1]`둜 κ³ μ • +* λ”°λΌμ„œ `dp[start][end]`λ₯Ό `[start, end]` κ΅¬κ°„μ˜ 블둝을 λͺ¨λ‘ μ œκ±°ν•  λ•Œ μ–»λŠ” μ΅œλŒ€ 점수둜 μ •μ˜ν•˜κ³  λ©”λͺ¨μ΄μ œμ΄μ…˜(Top-Down DP)을 적용 + + +## 2. μ‹œκ°„ λ³΅μž‘λ„ + + + +**O(NΒ³)** β€” +* **ꡬ간 μƒνƒœ 개수**: $0 \le start \le end < N$ 인 κ΅¬κ°„μ˜ 개수 $O(N^2)$. +* **전이(Transition) μ—°μ‚°**: 각 κ΅¬κ°„λ§ˆλ‹€ $k$λ₯Ό $start \sim end$ λ²”μœ„λ‘œ νƒμƒ‰ν•˜λ©° $O(N)$번 비ꡐ +* **총 μ‹œκ°„ λ³΅μž‘λ„**: $O(N^2) \times O(N) = O(N^3)$ (N <= 10) + + +## 3. 곡간 λ³΅μž‘λ„ + + +**O(NΒ²)** β€” +* `dp[N][N]` 2차원 λ©”λͺ¨μ΄μ œμ΄μ…˜ λ°°μ—΄ 생성 $O(N^2)$ +* μž¬κ·€ 호좜 μŠ€νƒ 깊이 μ΅œλŒ€ $O(N)$ + + +## 4. 핡심 풀이 방법 ⭐ + +1. **λ§ˆμ§€λ§‰μ— 제거될 블둝 κΈ°μ€€μ˜ ꡬ간 DP (Interval DP)**: + - "첫 번째둜 κΉ° 블둝"을 κ³ λ₯΄λ©΄ 남은 μ–‘μ˜† μš”μ†Œκ°€ κ°€λ³€μ μ΄μ§€λ§Œ, "ν•΄λ‹Ή κ΅¬κ°„μ—μ„œ λ§ˆμ§€λ§‰μœΌλ‘œ κΉ° 블둝 $k$"λ₯Ό κ³ λ₯΄λ©΄ 쒌우 이웃이 κ³ μ •λ˜μ–΄ 쒌츑 ꡬ간 `[start, k-1]`κ³Ό 우츑 ꡬ간 `[k+1, end]`둜 λΆ„ν•  κ°€λŠ₯ +2. **경계 처리 (`left_val`, `right_val`)**: + - $k$κ°€ 맨 λ§ˆμ§€λ§‰μ— 깨질 λ•Œ 이웃 블둝은 `start - 1`κ³Ό `end + 1` μœ„μΉ˜ + - ꡬ간이 λ°°μ—΄ μ–‘ 끝 경계 밖인 경우(-1) μ˜ˆμ™Έ 처리 +3. **Top-Down λ©”λͺ¨μ΄μ œμ΄μ…˜**: + - `dp[start][end]`에 κ³„μ‚°λœ μ΅œλŒ“κ°’μ„ μ €μž₯ + + +## 5. μ‹€μ œ μ½”λ“œ + + +```python + +T = int(input()) + +for test_case in range(1, T+1): + N = int(input()) + blocks = list(map(int, input().split())) + + if N == 1: + print(f"#{test_case} {blocks[0]}") + continue + + dp = [[-1] * N for _ in range(N)] + + def get_max_score(start, end): + if start > end: + return 0 + + # DP λ©”λͺ¨μ΄μ œμ΄μ…˜ + if dp[start][end] != -1: + return dp[start][end] + + max_val = -float('inf') + + # [start, end] κ΅¬κ°„μ—μ„œ λ§ˆμ§€λ§‰μœΌλ‘œ 깨트릴 블둝 k 탐색 + for k in range(start, end+1): + left_val = blocks[start - 1] if start > 0 else -1 # 쒌츑 이웃 + right_val = blocks[end + 1] if end < N - 1 else -1 # 우츑 이웃 + + if left_val == -1 and right_val > 0: # μ™Όμͺ½ 끝일 경우 + score = right_val + elif right_val == -1 and left_val > 0: # 였λ₯Έμͺ½ 끝일 경우 + score = left_val + elif left_val > 0 and right_val > 0: # 쀑간일 경우 + score = left_val * right_val + elif left_val == -1 and right_val == -1: # 블둝이 였직 ν•˜λ‚˜ 남은 경우 + score = blocks[k] + + # top-down DP: 쒌츑 ꡬ간 μ΅œλŒ“κ°’ + 우츑 ꡬ간 μ΅œλŒ“κ°’ + ν•΄λ‹Ή 블둝 kλ₯Ό 깼을 λ•Œ 점수 + val = get_max_score(start, k-1) + get_max_score(k+1, end) + score + max_val = max(max_val, val) + + dp[start][end] = max_val + return max_val + + print(f"#{test_case} {get_max_score(0, N-1)}") +``` + +--- + +### 🧠 회고 (선택) + +* 아이디어λ₯Ό μƒκ°ν•΄λ‚΄κΈ°κΉŒμ§€μ˜ 과정이 쉽지 μ•Šμ•˜λ‹€ +* ꡬ간 DP μœ ν˜•μ— λŒ€ν•œ 좔가적인 ν•™μŠ΅μ΄ ν•„μš”ν•  것 κ°™λ‹€. \ No newline at end of file From f5419d1c1f39716a403487d26cd02525812b563a Mon Sep 17 00:00:00 2001 From: TaehyungKim <91403729+clarityth@users.noreply.github.com> Date: Thu, 30 Jul 2026 16:13:57 +0900 Subject: [PATCH 3/7] =?UTF-8?q?solve:=20[W02]=20SWEA=205656=20=EB=B2=BD?= =?UTF-8?q?=EB=8F=8C=20=EA=B9=A8=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...75\353\217\214\352\271\250\352\270\260.md" | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 "studies/week-02/clarityth/5656-\353\262\275\353\217\214\352\271\250\352\270\260.md" diff --git "a/studies/week-02/clarityth/5656-\353\262\275\353\217\214\352\271\250\352\270\260.md" "b/studies/week-02/clarityth/5656-\353\262\275\353\217\214\352\271\250\352\270\260.md" new file mode 100644 index 0000000..3d6136e --- /dev/null +++ "b/studies/week-02/clarityth/5656-\353\262\275\353\217\214\352\271\250\352\270\260.md" @@ -0,0 +1,135 @@ + + +# [SWEA 5656] [λͺ¨μ˜ SW μ—­λŸ‰ν…ŒμŠ€νŠΈ] 벽돌 κΉ¨κΈ° + +| ν•­λͺ© | λ‚΄μš© | +|:---|:---| +| πŸ”— 문제 | https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWXRQm6qfL0DFAUo | +| πŸ“š 주제 | κ΅¬ν˜„, 완전탐색(DFS), λ°±νŠΈλž˜ν‚Ή | +| 🎚️ λ‚œμ΄λ„ | λͺ¨μ˜ SW μ—­λŸ‰ν…ŒμŠ€νŠΈ | +| ⏱️ μ†Œμš” μ‹œκ°„ | 1μ‹œκ°„ | +| πŸ” λ‹€μ‹œ ν’€κΈ° | μ•„λ‹ˆμ˜€ | + +--- + +## 1. μ ‘κ·Ό 방법 + +* κ΅¬μŠ¬μ„ $N$번 λ–¨μ–΄λœ¨λ € 맞좜 수 μžˆλŠ” λͺ¨λ“  μœ„μΉ˜(μ—΄ $W$개)의 경우의 수λ₯Ό DFS λ°±νŠΈλž˜ν‚Ή 탐색 +* κ΅¬μŠ¬μ„ 쏠 μ—΄(`col`)을 κ²°μ •ν•˜λ©΄, ν•΄λ‹Ή μ—΄μ—μ„œ κ°€μž₯ 상단에 μžˆλŠ” λ²½λŒμ„ νƒ€κ²©ν•˜μ—¬ 연쇄 폭발 처리 +* 폭발이 μ™„λ£Œλœ ν›„ λ‚¨μ•„μžˆλŠ” λ²½λŒλ“€μ„ μ•„λž˜λ‘œ λ–¨μ–΄λœ¨λ¦¬λŠ” 쀑λ ₯ 처리λ₯Ό μˆ˜ν–‰ν•œ λ’€ λ‹€μŒ λ‹¨κ³„λ‘œ μž¬κ·€ 호좜 +* λ§€ λ‹¨κ³„λ§ˆλ‹€ λ‚¨μ•„μžˆλŠ” 벽돌 수의 μ΅œμ†Ÿκ°’μ„ κ°±μ‹ ν•˜μ—¬ $N$번 κ΅¬μŠ¬μ„ λͺ¨λ‘ 쏜 λ’€ 남은 벽돌의 μ΅œμ†Œ 개수 계산 + + +## 2. μ‹œκ°„ λ³΅μž‘λ„ + + + +**O(W^N Β· H Β· W)** β€” +* **ꡬ슬 νˆ¬ν•˜ 경우의 수**: 총 $N$번 κ΅¬μŠ¬μ„ 쏘며 $W$개 μ—΄ 쀑 ν•˜λ‚˜λ₯Ό μ„ νƒν•˜λ―€λ‘œ $W^N$κ°€μ§€ μƒνƒœ 탐색 ($W \le 12, N \le 4$ μ΄λ―€λ‘œ μ΅œλŒ€ $12^4 = 20,736$κ°€μ§€). +* **단계별 μ—°μ‚°**: 각 λ‹¨κ³„λ§ˆλ‹€ 폭발(μ΅œλŒ€ $H \times W$) 및 쀑λ ₯ 처리($O(H \times W)$) μ§„ν–‰. +* **총 μ‹œκ°„ λ³΅μž‘λ„**: $20,736 \times 15 \times 12 \approx 3,700,000$번 μ—°μ‚° + + +## 3. 곡간 λ³΅μž‘λ„ + + +**O(N Β· H Β· W)** β€” +* **μž¬κ·€ 호좜 μŠ€νƒ**: DFS 깊이 μ΅œλŒ€ $N$개. +* **λ³΄λ“œ 볡사**: λ§€ μž¬κ·€ λ‹¨κ³„λ§ˆλ‹€ $H \times W$크기의 `next_board` deepcopy ($N \times H \times W$). + + +## 4. 핡심 풀이 방법 ⭐ + +1. **DFS λ°±νŠΈλž˜ν‚Ήκ³Ό λ§€ 단계 μ΅œμ†Ÿκ°’ κ°±μ‹ **: + - `ans = min(ans, remain)`을 DFS 첫 쀄에 λ°°μΉ˜ν•˜μ—¬ 항상 κ²°κ³Ό 값을 μ—…λ°μ΄νŠΈ ν–ˆμ–΄μ•Ό ν–ˆλŠ”λ°, 졜초 ν’€μ΄μ‹œ DFS의 depthκ°€ NμΌλ•Œλ§Œ μ—…λ°μ΄νŠΈν•˜λ„λ‘ 잘λͺ» μ„€μ • +2. **연쇄 폭발 처리**: + - 벽돌의 숫자 Kλ₯Ό λ¨Όμ € λ”°λ‘œ λ³΄κ΄€ν•œ λ’€ ν•΄λ‹Ή μœ„μΉ˜λ₯Ό `0`으둜 μ§€μš°κ³ , μƒν•˜μ’Œμš° dx, dy에 λŒ€ν•΄ K-1 λ²”μœ„λ§ŒνΌ 연쇄 ν­λ°œμ„ μž¬κ·€ν•¨μˆ˜λ‘œ κ΅¬ν˜„ (BFS νλ‘œλ„ κ΅¬ν˜„ κ°€λŠ₯) +3. **쀑λ ₯ μ •λ ¬**: + - 각 μ—΄λ³„λ‘œ μ•„λž˜μ—μ„œλΆ€ν„° `0`이 μ•„λ‹Œ μˆ˜λ“€μ„ μŠ€νƒμ— λͺ¨μ€ ν›„ λ°”λ‹₯μ—μ„œλΆ€ν„° μ±„μ›Œ λ„£μ–΄ λ²½λŒμ„ μ•„λž˜λ‘œ 이동 +4. **κ°€μ§€μΉ˜κΈ°**: + - `ans == 0`이 λ˜λŠ” μˆœκ°„ 이미 도달 κ°€λŠ₯ν•œ μ΅œλŒ“κ°’μ΄λ―€λ‘œ μ‘°κΈ° μ’…λ£Œ + + +## 5. μ‹€μ œ μ½”λ“œ + + +```python +T = int(input()) + +dx = [0, 0, -1, 1] +dy = [-1, 1, 0, 0] + +for test_case in range(1, T+1): + N, W, H = map(int, input().split()) + + board = [list(map(int, input().split())) for _ in range(H)] + + def get_brick_cnt(board): + cnt = 0 + for row in board: + for room in row: + if room > 0: + cnt += 1 + return cnt + + def boom(row, col, board): + power = board[row][col] + board[row][col] = 0 + cnt = 1 + + for p in range(1, power): + for i in range(4): + nr, nc = row + p * dy[i], col + p * dx[i] + if 0 <= nr < H and 0 <= nc < W: + if board[nr][nc] > 1: + cnt += boom(nr, nc, board) + board[nr][nc] = 0 + elif board[nr][nc] == 1: + board[nr][nc] = 0 + cnt += 1 + return cnt + + def gravity(board): + for col in range(W): + temp = [] + for row in range(H): + if board[row][col] > 0: + temp.append(board[row][col]) + board[row][col] = 0 + + row = H - 1 + while temp: + board[row][col] = temp.pop() + row -= 1 + + def dfs(depth, board, remain): + global ans + + ans = min(ans, remain) + + if depth == N or ans == 0: + return + + for col in range(W): + for row in range(H): + if board[row][col] > 0: + next_board = [r[:] for r in board] + crashed = boom(row, col, next_board) + gravity(next_board) + dfs(depth + 1, next_board, remain - crashed) + break + + ans = get_brick_cnt(board) + dfs(0, board, ans) + print(f"#{test_case} {ans}") +``` + +### 🧠 회고 (선택) + +* 졜초 ν’€μ΄μ‹œ, productλ₯Ό μ΄μš©ν•΄ ν’€μ—ˆλŠ”λ° λ„ˆλ¬΄ 였래걸렀 μˆ˜μ •ν–ˆλ‹€. +* dfs의 ν˜ΈμΆœμ— λ”°λ₯Έ board의 μƒνƒœ 관리λ₯Ό deepcopy둜 각각 μ „λ‹¬ν•˜λŠ” 게 μ€‘μš”ν–ˆλ‹€. +* 연쇄 폭발 λ‘œμ§μ„ κ΅¬ν˜„ν•˜λŠ”λ° μžˆμ–΄μ„œ 큐λ₯Ό μ΄μš©ν•˜λŠ” 방법이 μžˆλ‹€λŠ” 것을 μ•Œκ²Œ λ˜μ—ˆλ‹€. \ No newline at end of file From b2f16a56f6f7812cf6d78b330e0dee351a0d62e8 Mon Sep 17 00:00:00 2001 From: TaehyungKim <91403729+clarityth@users.noreply.github.com> Date: Thu, 30 Jul 2026 17:58:53 +0900 Subject: [PATCH 4/7] =?UTF-8?q?solve:=20[W02]=20SWEA=205658=20=EB=B3=B4?= =?UTF-8?q?=EC=84=9D=20=EC=88=98=EC=A7=91=20=EB=A1=9C=EB=B4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...30\354\247\221\353\241\234\353\264\207.md" | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 "studies/week-02/clarityth/26070-\353\263\264\354\204\235\354\210\230\354\247\221\353\241\234\353\264\207.md" diff --git "a/studies/week-02/clarityth/26070-\353\263\264\354\204\235\354\210\230\354\247\221\353\241\234\353\264\207.md" "b/studies/week-02/clarityth/26070-\353\263\264\354\204\235\354\210\230\354\247\221\353\241\234\353\264\207.md" new file mode 100644 index 0000000..0bf71c2 --- /dev/null +++ "b/studies/week-02/clarityth/26070-\353\263\264\354\204\235\354\210\230\354\247\221\353\241\234\353\264\207.md" @@ -0,0 +1,126 @@ + + +# [SWEA 5658] [λͺ¨μ˜ SW μ—­λŸ‰ν…ŒμŠ€νŠΈ] 보석 μˆ˜μ§‘ λ‘œλ΄‡ + +| ν•­λͺ© | λ‚΄μš© | +|:---|:---| +| πŸ”— 문제 | https://swexpertacademy.com/main/code/userProblem/userProblemDetail.do?contestProbId=AZwmBfua3q3HBIT3 | +| πŸ“š 주제 | Djikstra | +| 🎚️ λ‚œμ΄λ„ | λͺ¨μ˜ SW μ—­λŸ‰ν…ŒμŠ€νŠΈ | +| ⏱️ μ†Œμš” μ‹œκ°„ | 3μ‹œκ°„ | +| πŸ” λ‹€μ‹œ ν’€κΈ° | μ•„λ‹ˆμ˜€ | + +--- + +## 1. μ ‘κ·Ό 방법 + +* (0, 0)μ—μ„œ 였λ₯Έμͺ½ λ°©ν–₯(β†’)으둜 μΆœλ°œν•˜μ—¬ 1~M번 보석을 μˆœμ„œλŒ€λ‘œ μˆ˜μ§‘ν•˜λŠ” μ΅œμ†Œ νšŒμ „ 횟수 κ΅¬ν•˜κΈ° +* μœ„μΉ˜ 외에 λ°©ν–₯κ³Ό λͺ©ν‘œ 보석 번호λ₯Ό ν¬ν•¨ν•œ 4차원 μƒνƒœ `(d, row, col, target_j)` μ •μ˜ +* λ‘œλ΄‡μ˜ 행동을 **[보석 νšλ“]**, **[제자리 μš°νšŒμ „]**, **[직진 이동]** 3κ°€μ§€λ‘œ λΆ„λ¦¬ν•˜μ—¬ λ‹€μ΅μŠ€νŠΈλΌ(Priority Queue) 탐색 적용 + + +## 2. μ‹œκ°„ λ³΅μž‘λ„ + + +**O(V log V + E)** β€” *(V = N Γ— N Γ— 4 Γ— M)* +* **μƒνƒœ 수 (V)**: μ΅œλŒ€ $10 \times 10 \times 4 \times 10 = 4,000$개 +* **총 μ—°μ‚°**: $O(4000 \log 4000) \approx 4.8 \times 10^4$번으둜 0.01초 이내 μ™„λ£Œ + +## 3. 곡간 λ³΅μž‘λ„ + +**O(NΒ² Β· M)** β€” +* `dist` λ”•μ…”λ„ˆλ¦¬ 및 `pq` νž™ μ €μž₯ 곡간 ($O(N^2 \cdot M)$) + + +## 4. 핡심 풀이 방법 ⭐ + +1. **4차원 μƒνƒœ ν™•μž₯**: + - `(d, row, col, target_j)` νŠœν”Œ μ •μ˜ ν›„ `dist` λ”•μ…”λ„ˆλ¦¬λ‘œ μ΅œλ‹¨ νšŒμ „μˆ˜ 기둝 +2. **λ™μž‘μ˜ 뢄리**: + - **보석 νšλ“**: `target_j + 1` κ°±μ‹  ν›„ `continue` (νšŒμ „ +0) + - **제자리 μš°νšŒμ „**: `nd = (d + 1) % 4`, μœ„μΉ˜ μœ μ§€ (νšŒμ „ +1) + - **직진 이동**: `row + dy[d], col + dx[d]` (νšŒμ „ +0) +3. **μ‹œκ³„ λ°©ν–₯ μ˜€ν”„μ…‹ λ°°μ—΄**: + - `dx = [1, 0, -1, 0]`, `dy = [0, 1, 0, -1]` (0:우, 1:ν•˜, 2:쒌, 3:상)둜 μ„€μ •ν•˜μ—¬ `(d + 1) % 4` μš°νšŒμ „ 처리 + + +## 5. μ‹€μ œ μ½”λ“œ + + +```python +import heapq + +T = int(input()) + +# 0: 우, 1: ν•˜, 2: 쒌, 3: 상 (μ‹œκ³„ λ°©ν–₯) +dx = [1, 0, -1, 0] +dy = [0, 1, 0, -1] + +for test_case in range(1, T + 1): + N = int(input()) + grid = [list(map(int, input().split())) for _ in range(N)] + + jewels = {} + j_idx = 1 + for i in range(N): + for j in range(N): + if grid[i][j] > 0: + jewels[grid[i][j]] = (i, j) + j_idx += 1 + + # dist[(λ°©ν–₯, ν–‰, μ—΄, λͺ©ν‘œ 보석 번호)] = μ΅œμ†Œ νšŒμ „ 횟수 + dist = {} + dist[(0, 0, 0, 1)] = 0 + + ans = float("inf") + + # νž™ μ›μ†Œ: (νšŒμ „ 횟수, λ°©ν–₯, ν–‰, μ—΄, λͺ©ν‘œ 보석 번호) + pq = [(0, 0, 0, 0, 1)] + + while pq: + rotate, d, row, col, target_j = heapq.heappop(pq) + + # 더 적은 νšŒμ „μˆ˜λ‘œ λ°©λ¬Έν•œ μƒνƒœλ©΄ μŠ€ν‚΅ + if rotate > dist.get((d, row, col, target_j), float("inf")): + continue + + # 보석 νšλ“ + if (row, col) == jewels.get(target_j): + next_target = target_j + 1 + + if next_target == j_idx: + ans = rotate + break + + # 보석 번호 증가 + if rotate < dist.get((d, row, col, next_target), float("inf")): + dist[(d, row, col, next_target)] = rotate + heapq.heappush(pq, (rotate, d, row, col, next_target)) + continue + + # νšŒμ „ + nd = (d + 1) % 4 + if (rotate + 1) < dist.get((nd, row, col, target_j), float("inf")): + dist[(nd, row, col, target_j)] = rotate + 1 + heapq.heappush(pq, (rotate + 1, nd, row, col, target_j)) + + # 직진 + n_r, n_c = row + dy[d], col + dx[d] + if 0 <= n_r < N and 0 <= n_c < N: + if rotate < dist.get((d, n_r, n_c, target_j), float("inf")): + dist[(d, n_r, n_c, target_j)] = rotate + heapq.heappush(pq, (rotate, d, n_r, n_c, target_j)) + + print(f"#{test_case} {ans}") +``` + +--- + +### 🧠 회고 (선택) + +* λ‹€μ΅μŠ€νŠΈλΌλ₯Ό 이런 μ‹μœΌλ‘œλ„ ν™œμš© κ°€λŠ₯ν•˜λ‹€λŠ” 것을 μ•Œκ²Œ λ˜μ—ˆλ‹€. +* μƒνƒœλ₯Ό μ €μž₯ν•˜κΈ° μœ„ν•΄ μ–΄λ–€ λ³€μˆ˜λ“€μ„ μ €μž₯ν•΄μ•Όν• μ§€ 고민이 됐닀. From 785879e40b9e62a762e60b9a9f9cb4c6fe9c9cae Mon Sep 17 00:00:00 2001 From: TaehyungKim <91403729+clarityth@users.noreply.github.com> Date: Fri, 31 Jul 2026 00:50:07 +0900 Subject: [PATCH 5/7] =?UTF-8?q?solve:=20[W02]=20SWEA=205656=20=EB=B2=BD?= =?UTF-8?q?=EB=8F=8C=20=EA=B9=A8=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...-\353\262\275\353\217\214\352\271\250\352\270\260.md" | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git "a/studies/week-02/clarityth/5656-\353\262\275\353\217\214\352\271\250\352\270\260.md" "b/studies/week-02/clarityth/5656-\353\262\275\353\217\214\352\271\250\352\270\260.md" index 3d6136e..3e49df2 100644 --- "a/studies/week-02/clarityth/5656-\353\262\275\353\217\214\352\271\250\352\270\260.md" +++ "b/studies/week-02/clarityth/5656-\353\262\275\353\217\214\352\271\250\352\270\260.md" @@ -29,9 +29,8 @@ **O(W^N Β· H Β· W)** β€” -* **ꡬ슬 νˆ¬ν•˜ 경우의 수**: 총 $N$번 κ΅¬μŠ¬μ„ 쏘며 $W$개 μ—΄ 쀑 ν•˜λ‚˜λ₯Ό μ„ νƒν•˜λ―€λ‘œ $W^N$κ°€μ§€ μƒνƒœ 탐색 ($W \le 12, N \le 4$ μ΄λ―€λ‘œ μ΅œλŒ€ $12^4 = 20,736$κ°€μ§€). -* **단계별 μ—°μ‚°**: 각 λ‹¨κ³„λ§ˆλ‹€ 폭발(μ΅œλŒ€ $H \times W$) 및 쀑λ ₯ 처리($O(H \times W)$) μ§„ν–‰. -* **총 μ‹œκ°„ λ³΅μž‘λ„**: $20,736 \times 15 \times 12 \approx 3,700,000$번 μ—°μ‚° +* **ꡬ슬 νˆ¬ν•˜ 경우의 수**: 총 $N$번 κ΅¬μŠ¬μ„ 쏘며 $W$개 μ—΄ 쀑 ν•˜λ‚˜λ₯Ό μ„ νƒν•˜λ―€λ‘œ $W^N$κ°€μ§€ μƒνƒœ 탐색. $W=12, N=4$일 λ•Œ μ‹€μ œ DFS κ°„μ„  μˆ˜λŠ” $W + W^2 + W^3 + W^4 = 22,620$개. +* **단계별 μ—°μ‚°**: 각 κ°„μ„ λ§ˆλ‹€ λ³΄λ“œ 볡사, 폭발(μ΅œλŒ€ $H \times W$) 및 쀑λ ₯ 처리($O(H \times W)$) μ§„ν–‰. ## 3. 곡간 λ³΅μž‘λ„ @@ -49,7 +48,7 @@ 2. **연쇄 폭발 처리**: - 벽돌의 숫자 Kλ₯Ό λ¨Όμ € λ”°λ‘œ λ³΄κ΄€ν•œ λ’€ ν•΄λ‹Ή μœ„μΉ˜λ₯Ό `0`으둜 μ§€μš°κ³ , μƒν•˜μ’Œμš° dx, dy에 λŒ€ν•΄ K-1 λ²”μœ„λ§ŒνΌ 연쇄 ν­λ°œμ„ μž¬κ·€ν•¨μˆ˜λ‘œ κ΅¬ν˜„ (BFS νλ‘œλ„ κ΅¬ν˜„ κ°€λŠ₯) 3. **쀑λ ₯ μ •λ ¬**: - - 각 μ—΄λ³„λ‘œ μ•„λž˜μ—μ„œλΆ€ν„° `0`이 μ•„λ‹Œ μˆ˜λ“€μ„ μŠ€νƒμ— λͺ¨μ€ ν›„ λ°”λ‹₯μ—μ„œλΆ€ν„° μ±„μ›Œ λ„£μ–΄ λ²½λŒμ„ μ•„λž˜λ‘œ 이동 + - 각 열을 μœ„μ—μ„œλΆ€ν„° μˆœνšŒν•΄ `0`이 μ•„λ‹Œ λ²½λŒμ„ λͺ¨μ€ ν›„, `pop()`으둜 λ°”λ‹₯λΆ€ν„° μ±„μ›Œ λ„£μ–΄ λ²½λŒμ„ μ•„λž˜λ‘œ 이동 4. **κ°€μ§€μΉ˜κΈ°**: - `ans == 0`이 λ˜λŠ” μˆœκ°„ 이미 도달 κ°€λŠ₯ν•œ μ΅œλŒ“κ°’μ΄λ―€λ‘œ μ‘°κΈ° μ’…λ£Œ @@ -132,4 +131,4 @@ for test_case in range(1, T+1): * 졜초 ν’€μ΄μ‹œ, productλ₯Ό μ΄μš©ν•΄ ν’€μ—ˆλŠ”λ° λ„ˆλ¬΄ 였래걸렀 μˆ˜μ •ν–ˆλ‹€. * dfs의 ν˜ΈμΆœμ— λ”°λ₯Έ board의 μƒνƒœ 관리λ₯Ό deepcopy둜 각각 μ „λ‹¬ν•˜λŠ” 게 μ€‘μš”ν–ˆλ‹€. -* 연쇄 폭발 λ‘œμ§μ„ κ΅¬ν˜„ν•˜λŠ”λ° μžˆμ–΄μ„œ 큐λ₯Ό μ΄μš©ν•˜λŠ” 방법이 μžˆλ‹€λŠ” 것을 μ•Œκ²Œ λ˜μ—ˆλ‹€. \ No newline at end of file +* 연쇄 폭발 λ‘œμ§μ„ κ΅¬ν˜„ν•˜λŠ”λ° μžˆμ–΄μ„œ 큐λ₯Ό μ΄μš©ν•˜λŠ” 방법이 μžˆλ‹€λŠ” 것을 μ•Œκ²Œ λ˜μ—ˆλ‹€. From 477adaa06810c526bc727c2e104b09e216d66fd1 Mon Sep 17 00:00:00 2001 From: TaehyungKim <91403729+clarityth@users.noreply.github.com> Date: Fri, 31 Jul 2026 00:50:57 +0900 Subject: [PATCH 6/7] =?UTF-8?q?solve:=20[W02]=20SWEA=2026070=20=EB=B3=B4?= =?UTF-8?q?=EC=84=9D=20=EC=88=98=EC=A7=91=20=EB=A1=9C=EB=B4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\354\210\230\354\247\221\353\241\234\353\264\207.md" | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git "a/studies/week-02/clarityth/26070-\353\263\264\354\204\235\354\210\230\354\247\221\353\241\234\353\264\207.md" "b/studies/week-02/clarityth/26070-\353\263\264\354\204\235\354\210\230\354\247\221\353\241\234\353\264\207.md" index 0bf71c2..995f05d 100644 --- "a/studies/week-02/clarityth/26070-\353\263\264\354\204\235\354\210\230\354\247\221\353\241\234\353\264\207.md" +++ "b/studies/week-02/clarityth/26070-\353\263\264\354\204\235\354\210\230\354\247\221\353\241\234\353\264\207.md" @@ -4,12 +4,12 @@ μ•„λž˜ 5μš”μ†ŒλŠ” λ°˜λ“œμ‹œ λͺ¨λ‘ μž‘μ„±ν•©λ‹ˆλ‹€. (μ ‘κ·Ό/μ‹œκ°„/곡간/핡심/μ½”λ“œ) --> -# [SWEA 5658] [λͺ¨μ˜ SW μ—­λŸ‰ν…ŒμŠ€νŠΈ] 보석 μˆ˜μ§‘ λ‘œλ΄‡ +# [SWEA 26070] [λͺ¨μ˜ SW μ—­λŸ‰ν…ŒμŠ€νŠΈ] 보석 μˆ˜μ§‘ λ‘œλ΄‡ | ν•­λͺ© | λ‚΄μš© | |:---|:---| | πŸ”— 문제 | https://swexpertacademy.com/main/code/userProblem/userProblemDetail.do?contestProbId=AZwmBfua3q3HBIT3 | -| πŸ“š 주제 | Djikstra | +| πŸ“š 주제 | λ‹€μ΅μŠ€νŠΈλΌ | | 🎚️ λ‚œμ΄λ„ | λͺ¨μ˜ SW μ—­λŸ‰ν…ŒμŠ€νŠΈ | | ⏱️ μ†Œμš” μ‹œκ°„ | 3μ‹œκ°„ | | πŸ” λ‹€μ‹œ ν’€κΈ° | μ•„λ‹ˆμ˜€ | @@ -26,9 +26,10 @@ ## 2. μ‹œκ°„ λ³΅μž‘λ„ -**O(V log V + E)** β€” *(V = N Γ— N Γ— 4 Γ— M)* +**μΌ€μ΄μŠ€λ‹Ή O((V + E) log V) = O(NΒ² Β· M Β· log(NΒ² Β· M))** * **μƒνƒœ 수 (V)**: μ΅œλŒ€ $10 \times 10 \times 4 \times 10 = 4,000$개 -* **총 μ—°μ‚°**: $O(4000 \log 4000) \approx 4.8 \times 10^4$번으둜 0.01초 이내 μ™„λ£Œ +* μƒνƒœλ‹Ή 간선은 μ΅œλŒ€ 3κ°œμ΄λ―€λ‘œ `E ≀ 3V` +* 전체 μž…λ ₯ κΈ°μ€€ λ³΅μž‘λ„λŠ” `O(T Β· NΒ² Β· M Β· log(NΒ² Β· M))` ## 3. 곡간 λ³΅μž‘λ„ From 1b731666f40f5dd72ea144670c0279d56bb6e0b6 Mon Sep 17 00:00:00 2001 From: TaehyungKim <91403729+clarityth@users.noreply.github.com> Date: Fri, 31 Jul 2026 00:51:23 +0900 Subject: [PATCH 7/7] =?UTF-8?q?solve:=20[W02]=20SWEA=205658=20=EB=B3=B4?= =?UTF-8?q?=EB=AC=BC=EC=83=81=EC=9E=90=20=EB=B9=84=EB=B0=80=EB=B2=88?= =?UTF-8?q?=ED=98=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3\271\204\353\260\200\353\262\210\355\230\270.md" | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git "a/studies/week-02/clarityth/5658-\353\263\264\353\254\274\354\203\201\354\236\220\353\271\204\353\260\200\353\262\210\355\230\270.md" "b/studies/week-02/clarityth/5658-\353\263\264\353\254\274\354\203\201\354\236\220\353\271\204\353\260\200\353\262\210\355\230\270.md" index 64ff1ce..2f606a7 100644 --- "a/studies/week-02/clarityth/5658-\353\263\264\353\254\274\354\203\201\354\236\220\353\271\204\353\260\200\353\262\210\355\230\270.md" +++ "b/studies/week-02/clarityth/5658-\353\263\264\353\254\274\354\203\201\354\236\220\353\271\204\353\260\200\353\262\210\355\230\270.md" @@ -27,16 +27,16 @@ -**O(NΒ² log N)** β€” -* **νšŒμ „ 및 μŠ¬λΌμ΄μ‹±**: $N/4$회 νšŒμ „ $\times$ λ¬Έμžμ—΄ μŠ¬λΌμ΄μ‹± = $O(N)$ -* **μ •λ ¬**: μ΅œλŒ€ $N$개 수 λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ = $O(N \log N)$ -* **10μ§„μˆ˜ λ³€ν™˜**: $O(N)$ +**μ‹œκ°„ λ³΅μž‘λ„: O(NΒ² log N)** β€” +* **νšŒμ „ 및 μΆ”μΆœ**: $N/4$회 Γ— 길이 $N$ 처리 = $O(NΒ²)$ +* **μ •λ ¬**: λ¬Έμžμ—΄ 비ꡐ λΉ„μš©κΉŒμ§€ κ³ λ €ν•˜λ©΄ μ΅œμ•… $O(NΒ² \log N)$ +* **10μ§„μˆ˜ λ³€ν™˜**: μ„ νƒλœ λ¬Έμžμ—΄ ν•˜λ‚˜λ§Œ λ³€ν™˜ν•˜λ―€λ‘œ $O(N)$ ## 3. 곡간 λ³΅μž‘λ„ -**O(N)** β€” -* μ΅œλŒ€ $N$개 16μ§„μˆ˜ λ¬Έμžμ—΄ μ €μž₯용 `set` 및 `list` μ‚¬μš©ν•¨. +**곡간 λ³΅μž‘λ„: O(NΒ²)** β€” +* μ΅œλŒ€ $N$개 λ¬Έμžμ—΄μ„ μ €μž₯ν•˜λ©° 각 λ¬Έμžμ—΄ 길이가 $N/4$μž„. ## 4. 핡심 풀이 방법 ⭐