-
Notifications
You must be signed in to change notification settings - Fork 0
solve: [W02] SWEA 5656, 5658, 26070, 26071 #7
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # [SWEA 26070] ๋ณด์ ์์ง ๋ก๋ด | ||
|
|
||
| | ํญ๋ชฉ | ๋ด์ฉ | | ||
| |:---|:---| | ||
| | ๐ ๋ฌธ์ | https://swexpertacademy.com/main/code/userProblem/userProblemDetail.do?contestProbId=AZwmBfua3q3HBIT3 | | ||
| | ๐ ์ฃผ์ | BFS, ๋ค์ต์คํธ๋ผ | | ||
| | ๐๏ธ ๋์ด๋ | D4 | | ||
| | โฑ๏ธ ์์ ์๊ฐ | 1H | | ||
| | ๐ ๋ค์ ํ๊ธฐ | ์ | | ||
|
|
||
| --- | ||
|
|
||
| ## 1. ์ ๊ทผ ๋ฐฉ๋ฒ | ||
| ๋ก๋ด์ด `(0,0)`์์ ์ค๋ฅธ์ชฝ์ ๋ณด๊ณ ์ถ๋ฐํด, **๋ณด์์ ๋ฒํธ ์ค๋ฆ์ฐจ์์ผ๋ก** ํ๋์ฉ ๋ฐฉ๋ฌธํ๋ค. | ||
| ๋น์ฉ์ **ํ์ ํ์**๋ฟ โ ์ ์ง์ 0, 90ยฐ ํ์ ์ +1. | ||
| "๋ณด์ ์ฌ์ด ๊ตฌ๊ฐ๋ง๋ค ์ต์ ํ์ ์"๋ฅผ ๊ฐ๊ฐ ๊ตฌํด ๋์ ํ๋ฉด ๋๋ฏ๋ก, | ||
| ๊ฐ ๊ตฌ๊ฐ์ **๋น์ฉ์ด 0/1์ธ ์ต๋จ๊ฒฝ๋ก(๋ค์ต์คํธ๋ผ)** ๋ก ํผ๋ค. ๋์ฐฉ ์ ์์นยท๋ฐฉํฅ์ ๋ค์ ๊ตฌ๊ฐ์ผ๋ก ๋๊ธด๋ค. | ||
|
|
||
| ## 2. ์๊ฐ ๋ณต์ก๋ | ||
| **O(J ยท nยฒ log n)** โ J = ๋ณด์ ์(์ต๋ ~nยฒ), ๊ตฌ๊ฐ๋ง๋ค ์ํ `(y,x,๋ฐฉํฅ,ํ์ ๊ฐ๋ฅ)` ๋ค์ต์คํธ๋ผ. | ||
| ์ ์ฒด์ ์ผ๋ก๋ ๋๋ต `O(nโด log n)` ๊ท๋ชจ. | ||
|
|
||
| ## 3. ๊ณต๊ฐ ๋ณต์ก๋ | ||
| **O(nยฒ)** โ ๊ฒฉ์ ์ ์ฅ ๋ฐ ํ์ ์ฌ๋ผ๊ฐ๋ ์ํ ์(์์นร๋ฐฉํฅรํ๋๊ทธ = nยฒร4ร2). | ||
|
Comment on lines
+19
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ Performance & Scalability | ๐ Major | โก Quick win
ํ์ฌ๋ ๊ฐ์ ๊ฐ ์ํ๋ฅผ ์ฒ์ ๊บผ๋ผ ๋ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌํ๊ฑฐ๋ ์ต์ ์์ ์์ q = []
+ visited = [[[[False] * 2 for _ in range(4)]
+ for _ in range(n)] for _ in range(n)]
heapq.heappush(q, (0, sy, sx, cur_d, not first_flag))
while q:
cost, y, x, direct, turn_flag = heapq.heappop(q)
+ if visited[y][x][direct][turn_flag]:
+ continue
+ visited[y][x][direct][turn_flag] = True๊ฒฝ๋ก ์ง์นจ์ โ์๊ฐ ๋ณต์ก๋์ ๊ณต๊ฐ ๋ณต์ก๋๊ฐ ์ค์ ์ฝ๋์ ์ผ์นํ๋์ง ์ง์ ๊ณ์ฐโ ๋ฐ โ์ ๋ ฅ ์ต๋์น ๊ธฐ์ค TLE ์ํ์ ๋ฐ๋์ ์ง์ โ ์๊ตฌ๋ฅผ ๋ฐ์ํ์ต๋๋ค. Also applies to: 55-76, 85-85 ๐งฐ Tools๐ช markdownlint-cli2 (0.23.1)[warning] 19-19: Headings should be surrounded by blank lines (MD022, blanks-around-headings) [warning] 23-23: Headings should be surrounded by blank lines (MD022, blanks-around-headings) ๐ค Prompt for AI AgentsSource: Path instructions |
||
|
|
||
| ## 4. ํต์ฌ ํ์ด ๋ฐฉ๋ฒ โญ | ||
| - **์ํ๋ฅผ `(๋น์ฉ, y, x, ๋ฐฉํฅ, ํ์ ๊ฐ๋ฅ์ฌ๋ถ)`** ๋ก ์ ์ํด ํ์ ๋ฃ๋๋ค. | ||
| - **๋น์ฉ = ํ์ ์**: ์ ์ง์ `cost` ๊ทธ๋๋ก, ํ์ ์ `cost+1`. โ 0/1 ๊ฐ์ค์น๋ผ ๋ค์ต์คํธ๋ผ๊ฐ ์ฑ๋ฆฝ. | ||
| - **์ฐ์ ํ์ ๊ธ์ง**: `turn_flag`๋ก ์ ์ด. ํ์ ํ๋ฉด `False`(๋ค์์ ํ์ ๋ถ๊ฐ), ์ ์งํ๋ฉด `True`(ํ์ ๊ฐ๋ฅ) โ ์ ์๋ฆฌ 180ยฐ ๋ฐฉ์ง, ํ์ ์ฌ์ด ์ต์ 1์นธ ์ ์ง ๊ฐ์ . | ||
| - **์์ ์ ์ฝ**: ๋งจ ์ฒ์ ์ถ๋ฐ์ ์์๋ ํ์ ๋ถ๊ฐ(๋ฌด์กฐ๊ฑด ์ ์ง), ์ดํ ๋ณด์ ์ง์ ์์๋ ํ์ 1ํ ๊ฐ๋ฅ. | ||
| - ๋ชฉํ ๋ณด์์ **ํ์์ ์ฒ์ ๊บผ๋ด๋ ์๊ฐ**์ด ์ต์ ๋น์ฉ(๋ค์ต์คํธ๋ผ ์ฑ์ง) โ ๊ทธ๋ ์์นยท๋ฐฉํฅ ๊ฐฑ์ ํ ๋ค์ ๋ณด์์ผ๋ก. | ||
|
|
||
| ## 5. ์ค์ ์ฝ๋ | ||
| ```python | ||
| from collections import deque | ||
| import heapq | ||
|
|
||
| T = int(input()) | ||
| for tc in range(1, T + 1): | ||
| n = int(input()) | ||
| grid = [list(map(int, input().split())) for _ in range(n)] | ||
|
|
||
| d = [(-1, 0), (0, 1), (1, 0), (0, -1)] # ์ ์ฐ ํ ์ข | ||
| num_of_turn = 0 | ||
|
|
||
| # ๋ด๋ถ ๊ฒฉ์์์ ๋ณด์ ์์ง ํ ๋ฒํธ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ | ||
| jewels = [(y, x) for y in range(1, n-1) for x in range(1, n-1) if grid[y][x]] | ||
| jewels.sort(key=lambda cor: grid[cor[0]][cor[1]]) | ||
| jewels = deque(jewels) | ||
|
|
||
| sy, sx = 0, 0 # ์์ ์ขํ | ||
| cur_d = 1 # ์์ ๋ฐฉํฅ: ์ค๋ฅธ์ชฝ | ||
| first_flag = True | ||
|
|
||
| while jewels: | ||
| ey, ex = jewels.popleft() | ||
| q = [] | ||
| # ์ต์ด ์ถ๋ฐ์ ํ์ ๋ถ๊ฐ(False), ์ดํ ๋ณด์ ์ถ๋ฐ์ ํ์ 1ํ ๊ฐ๋ฅ(True) | ||
| heapq.heappush(q, (0, sy, sx, cur_d, not first_flag)) | ||
| first_flag = False | ||
|
|
||
| while q: | ||
| cost, y, x, direct, turn_flag = heapq.heappop(q) | ||
|
|
||
| if y == ey and x == ex: # ๋ชฉํ ๋ณด์ ๋์ฐฉ | ||
| sy, sx = y, x | ||
| num_of_turn += cost # ํ์ ์ ๋์ | ||
| cur_d = direct | ||
| break | ||
|
|
||
| if turn_flag: # ํ์ (๋ค์์ ํ์ ๋ถ๊ฐ) | ||
| heapq.heappush(q, (cost+1, y, x, (direct+1) % 4, False)) | ||
|
|
||
| ny, nx = y + d[direct][0], x + d[direct][1] # ์ ์ง (๋ค์์ ํ์ ๊ฐ๋ฅ) | ||
| if 0 <= ny < n and 0 <= nx < n: | ||
| heapq.heappush(q, (cost, ny, nx, direct, True)) | ||
|
|
||
| print(f'#{tc} {num_of_turn}') | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### ๐ง ํ๊ณ | ||
| - cost ๊ฐ ์ผ๋ฐ์ ์ธ ๋ค์ต์คํธ๋ผ์๋ ๋ค๋ฅด๊ฒ **ํ์ ํ์** ์ธ ๊ฒ๋ง ์ฃผ์ํ๋ฉด ์ด๋ ต์ง ์๋ค. | ||
| - ๊ฐ์ ์ฌ์ง: `visited` ์ ํ์ฉํ์ง ์์๋๋ฐ, ์ด๋ก ์ธํด ์ค๋ณต์ด ์ข ์์ ๊ฒ ๊ฐ๋ค. ๊ฐ์ ํด๋ณด๋ ค ํ๋ค. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| # [SWEA 26071] ๋ธ๋ก ์ ๊ฑฐ ๊ฒ์ | ||
|
|
||
| | ํญ๋ชฉ | ๋ด์ฉ | | ||
| |:---|:---| | ||
| | ๐ ๋ฌธ์ | https://swexpertacademy.com/main/code/userProblem/userProblemDetail.do?contestProbId=AZwmCVWq3uLHBIT3& | | ||
| | ๐ ์ฃผ์ | ๊ตฌ๊ฐ DP (Interval DP ยท Burst Balloons ๊ณ์ด) | | ||
| | ๐๏ธ ๋์ด๋ | D4 | | ||
| | โฑ๏ธ ์์ ์๊ฐ | 3h + | | ||
| | ๐ ๋ค์ ํ๊ธฐ | ์ | | ||
|
|
||
| --- | ||
|
|
||
| ## 1. ์ ๊ทผ ๋ฐฉ๋ฒ | ||
| "์ด๋ค ์์๋ก ๊นฐ๊น"๋ฅผ ์ ๋ฐฉํฅ์ผ๋ก ํ์ํ๋ฉด ์์ด `O(N!)`์ด๋ผ TLE. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ Performance & Scalability | ๐ก Minor | โก Quick win ๋ฐฑํธ๋ํน ๋ณต์ก๋๋ฅผ ์ด ๊ตฌํ์ ๊ฐ ์ ํจ ์ํ๋ง๋ค ์ขยท์ฐ ์ด์์ ์ฐพ๋ ๋ฐ ์ต๋ As per path instructions, ์๊ฐ ๋ณต์ก๋๋ ์ค์ ์ฝ๋์ ๋ฐ๋ณตยทํ์ ๋น์ฉ๊น์ง ์ง์ ๊ณ์ฐํด์ผ ํฉ๋๋ค. Also applies to: 102-112, 127-130, 147-148 ๐ค Prompt for AI AgentsSource: Path instructions |
||
| ๋ฐ์์ ๋ค์ง์ด "์ด ๊ตฌ๊ฐ์์ *๋ง์ง๋ง*์ ๊นจ์ง๋ ๋ธ๋ก์ด ๋๊ตฌ์ธ๊ฐ"๋ฅผ ๊ธฐ์ค์ผ๋ก ์ก๋๋ค. | ||
| ๋ง์ง๋ง์ ๊นจ์ง๋ ๋ธ๋ก `k`๋ ๊ทธ ์๊ฐ ์ด์์ด ๊ตฌ๊ฐ ์ ๋๋ฟ์ด๋ผ ์ ์๊ฐ ํ์ ๋๊ณ , | ||
| `k`๋ฅผ ๊ธฐ์ค์ผ๋ก ์ขยท์ฐ ๊ตฌ๊ฐ์ด **์์ ํ ๋ ๋ฆฝ**์ผ๋ก ๊ฐ๋ผ์ง๋ค โ ๊ตฌ๊ฐ DP๋ก ์ ํ๋ค. | ||
|
|
||
| ## 2. ์๊ฐ ๋ณต์ก๋ | ||
| **O(Nยณ)** โ ๊ตฌ๊ฐ ํญ ร ์ผ์ชฝ ๊ฒฝ๊ณ ร ๋ถํ ์ `k`. ์์ด `O(N!)`์์ ํฌ๊ฒ ๊ฐ์ . | ||
|
|
||
| ## 3. ๊ณต๊ฐ ๋ณต์ก๋ | ||
| **O(Nยฒ)** โ `dp[l][r]` 2์ฐจ์ ํ ์ด๋ธ. | ||
|
|
||
| ## 4. ํต์ฌ ํ์ด ๋ฐฉ๋ฒ โญ | ||
| - ์ ๋์ ๊ฐ `1` **์ผํฐ๋ฌ**์ ๋ถ์ธ๋ค โ "์ด์ ํ์ชฝ๋ง ์์ ๋ ๊ทธ ๊ฐ๋งํผ"์ด `1ร์ด์๊ฐ`์ผ๋ก ์๋ ์ฒ๋ฆฌ. | ||
| - `dp[l][r]` = ๊ฒฝ๊ณ `l`, `r`๋ **๋ฒฝ์ผ๋ก ๋จ๊ธด ์ฑ** ๊ทธ *์ฌ์ด* ๋ธ๋ก์ ์ ๋ถ ๊นฐ ๋์ ์ต๋ ์ ์. | ||
| - `dp[l][r] = b[l] * b[r] + max( dp[l][k] + dp[k][r] )` (`l < k < r`) | ||
| - `k`๋ฅผ ๋ง์ง๋ง์ ๊นจ๋ ์๊ฐ์ ์ ์๊ฐ `b[l] * b[r]` (์๊ธฐ ๊ฐ์ ์ ๋ค์ด๊ฐ). | ||
| - **๋ง์ง๋ง ํ ๋ธ๋ก**์ ์ด์์ด ์์ด ์๊ธฐ ๊ฐ์ ์ป์ โ DP๋ก ๋ชป ์ ํ๋ ์์ธ๋ผ ๋ฐ์์ ์ฒ๋ฆฌ: | ||
| - ๋ง์ง๋ง ๋ธ๋ก `p`๋ฅผ ๊ณ ์ ํด `dp[0][p] + dp[p][n+1] + b[p]`์ ์ต๋๊ฐ์ด ์ ๋ต. | ||
|
|
||
| ## 5. ์ค์ ์ฝ๋ | ||
| ```python | ||
| T = int(input()) | ||
| for tc in range(1, T+1): | ||
|
|
||
| N = int(input()) | ||
| arr = list(map(int ,input().split())) # N | ||
|
|
||
| b = [1] + arr + [1] # 0 ~ n+1 # 1 + 0 ~ n-1 + 1 | ||
|
|
||
| M = N + 2 | ||
|
|
||
| dp = [[0]*(M) for _ in range(M)] | ||
|
|
||
| for length in range(2, M): | ||
|
|
||
| for l in range(M - length): | ||
| r = l + length | ||
|
|
||
| best = dp[l][l+1] + dp[l+1][r] | ||
|
|
||
| for k in range(l+1, r): | ||
| best = max(dp[l][k] + dp[k][r], best) | ||
|
|
||
| dp[l][r] = b[l] * b[r] + best | ||
|
|
||
| ans = max(dp[0][p] + dp[p][N+1] + b[p] for p in range(1, N+1)) | ||
|
|
||
| print(f'#{tc} {ans}') | ||
|
|
||
| ## ๋ฐฑํธ๋ํน - ์๊ฐ์ด๊ณผ | ||
|
|
||
| # T = int(input()) | ||
| # # ์ฌ๋ฌ๊ฐ์ ํ ์คํธ ์ผ์ด์ค๊ฐ ์ฃผ์ด์ง๋ฏ๋ก, ๊ฐ๊ฐ์ ์ฒ๋ฆฌํฉ๋๋ค. | ||
| # for test_case in range(1, T + 1): | ||
| # # /////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| # n = int(input()) | ||
|
|
||
| # arr = list(map(int, input().split())) | ||
|
|
||
| # ans_score = 0 | ||
|
|
||
| # # ์ญ์ ๋ ๊ฑฐ -> 0 | ||
| # # ์์ ์๋ ์์ด | ||
| # # arr[i] = 0 ์ผ๋ก ๋ฐฉ๋ฌธ์ฒ๋ฆฌ | ||
|
|
||
| # def backtrack(cidx, score, remain_block): | ||
| # global ans_score | ||
|
|
||
| # #print(f'cidx: {cidx}, score: {score}, remain_block: {remain_block}') | ||
|
|
||
| # # ์ข ๋ฃ ์กฐ๊ฑด | ||
| # if remain_block == 0: # ๋ธ๋ก ํ์ ์๋ฃ | ||
| # ans_score = max(ans_score, score) | ||
| # return | ||
|
|
||
| # if arr[cidx] == 0: # ๋ถ์ด์ง ๋ธ๋ก์ด๋ฉด | ||
| # return | ||
|
|
||
| # # ํ์ฌ ์์น๋ ํญ์ ์จ์ ํ ๋ธ๋ก | ||
| # # ํ์ฌ ์์น ๊ธฐ์ค ์ค๋ฅธ์ชฝ ์ผ์ชฝ ์ด์ ์๋์ง | ||
|
|
||
| # left_idx = -1 | ||
| # right_idx = -1 | ||
|
|
||
| # left_neighbor = False | ||
| # right_neighbor = False | ||
|
|
||
| # for lidx in range(cidx-1, -1, -1): | ||
| # if arr[lidx]: # ์๋ถ์ด์ง ๋ธ๋ก์ด๋ฉด | ||
| # left_idx = lidx | ||
| # left_neighbor = True | ||
| # break | ||
|
|
||
| # for ridx in range(cidx+1, n): | ||
| # if arr[ridx]: # ์๋ถ์ด์ง ๋ธ๋ก์ด๋ฉด | ||
| # right_idx = ridx | ||
| # right_neighbor = True | ||
| # break | ||
|
|
||
| # if left_neighbor and right_neighbor: | ||
| # score += arr[left_idx] * arr[right_idx] | ||
|
|
||
| # elif left_neighbor: | ||
| # score += arr[left_idx] | ||
|
|
||
| # elif right_neighbor: | ||
| # score += arr[right_idx] | ||
|
|
||
| # else: # ์ด์์์ ๋ | ||
| # score += arr[cidx] | ||
|
|
||
| # save_block = arr[cidx] | ||
| # for i in range(n): | ||
| # arr[cidx] = 0 | ||
|
|
||
| # backtrack(i, score, remain_block - 1) | ||
|
|
||
| # arr[cidx] = save_block | ||
|
|
||
| # for s in range(n): | ||
| # backtrack(s, 0, n) | ||
|
|
||
| # print('#'+str(test_case), ans_score) | ||
|
|
||
| # # /////////////////////////////////////////////////////////////////////////////////// | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### ๐ง ํ๊ณ | ||
| - "์ ๋ฐฉํฅ์ผ๋ก ์ ์ชผ๊ฐ์ง๋ฉด **๋ง์ง๋ง(๋๋ ์ฒ์) ํ๋๋ฅผ ๊ณ ์ **ํด ์์ชฝ์ ๋ ๋ฆฝ์ํจ๋ค"๊ฐ ๊ตฌ๊ฐ DP์ ํต์ฌ ๋ฐ์. | ||
| - ์ผํฐ๋ฌ `1`๋ก ๊ฒฝ๊ณ ์์ธ๋ฅผ, `+ b[p]`๋ก ๋ง์ง๋ง ๋ธ๋ก ์์ธ๋ฅผ ์ฒ๋ฆฌ โ **DP๋ก ๋ชป ์ ๋ ์์ธ๋ ๋ฐ์์ ์ฑ๊ธด๋ค.** | ||
| - ์์ด ํ์ด๋ N์ด ์์ ๋๋ง ํต๊ณผ. ๊ทผ๋ณธ ํด๊ฒฐ์ ๊ตฌ๊ฐ DP `O(Nยณ)`. | ||
| - ๋ฐฑํธ๋ํน์ผ๋ก๋ ์๊ฐ ์ ํ์ด ๊ฑธ๋ฆด ๊ฑฐ๋ผ ์๊ฐํ์ง๋ง, ๋น์ฐํ ํฐ์ก๋ค. | ||
| - DP ๋ ๋ฐฉ์์ ๋ ์ฌ๋ฆฌ์ง ์์ผ๋ฉด ๋๋ฌด ํ๊ธฐ ์ด๋ ค์์ ํ์์ DP ์ฐ์ต์ ์ข ์งํํด์ผ ํ ๋ฏ ํ๋ค. | ||
| - ์ด๋ฒ ๋ฌธ์ ๋ ํ๊ธฐ ๋ชปํด์ CLAUDE ์๊ฒ ์์ผฐ๋ค... | ||
| - ๊ฐ์ ๊ตฌ๊ฐ DP ๋ฌธ์ ์ธ BURST BALLOONS ๋ฌธ์ ๋ ๊ฐ์ด ์ฐ์ตํ ๋งํ๋ค. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| # [SWEA] ๋ฒฝ๋ ๊นจ๊ธฐ | ||
|
|
||
| | ํญ๋ชฉ | ๋ด์ฉ | | ||
| |:---|:---| | ||
| | ๐ ๋ฌธ์ | https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWXRQm6qfL0DFAUo | | ||
| | ๐ ์ฃผ์ | ์์ ํ์(DFS) ยท ์๋ฎฌ๋ ์ด์ (์ฐ์ ํญ๋ฐ BFS) ยท ์ค๋ ฅ | | ||
| | ๐๏ธ ๋์ด๋ | ์ค์ | | ||
| | โฑ๏ธ ์์ ์๊ฐ | 1H | | ||
| | ๐ ๋ค์ ํ๊ธฐ | ์ | | ||
|
|
||
| --- | ||
|
|
||
| ## 1. ์ ๊ทผ ๋ฐฉ๋ฒ | ||
| ๊ตฌ์ฌ ํ๋๋ฅผ ๋จ์ด๋จ๋ฆด ๋ **์ด๋ ์ด์ ๋จ์ด๋จ๋ฆด์ง** ๋งค๋ฒ W๊ฐ์ง ์ ํ โ ์ด N๋ฒ โ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ **DFS ์์ ํ์**. | ||
| ๊ฐ ์ ํ๋ง๋ค (1) ๊ทธ ์ด ๋งจ ์ ๋ฒฝ๋์ ํฐ๋จ๋ฆฌ๊ณ โ (2) ์ซ์๋งํผ ์ํ์ข์ฐ๋ก **์ฐ์ ํญ๋ฐ** โ (3) **์ค๋ ฅ**์ผ๋ก ๋ฒฝ๋ ๋ํ โ (4) ๋ค์ ๊ตฌ์ฌ๋ก ์ฌ๊ท. | ||
| ๊ตฌ์ฌ์ ๋ค ์ฐ๊ฑฐ๋ ๋ฒฝ๋์ด 0์ด ๋๋ฉด ๋จ์ ๋ฒฝ๋ ์๋ฅผ **์ต์๊ฐ์ ๊ฐฑ์ **. | ||
|
|
||
| ## 2. ์๊ฐ ๋ณต์ก๋ | ||
| **O(Wแดบ ยท WยทH)** โ ๋งค ๋จ๊ณ W๊ฐ ์ด ๋ถ๊ธฐ ร ๊น์ด N, ๊ฐ ๋ ธ๋๋ง๋ค ๊ทธ๋ฆฌ๋ ๋ณต์ฌยทํญ๋ฐยท์ค๋ ฅ์ด O(WยทH). | ||
| NยทW๊ฐ ์์ ์์ ํ์์ผ๋ก ํต๊ณผ (๋ชจ์ ์ญ๋ํ ์คํธ ์ ์ฝ ๊ธฐ์ค). | ||
|
|
||
| ## 3. ๊ณต๊ฐ ๋ณต์ก๋ | ||
| **O(N ยท WยทH)** โ ์ฌ๊ท ๊น์ด N๋งํผ ๊ทธ๋ฆฌ๋ ๋ณต์ฌ๋ณธ์ ์คํ์ ์ ์ง. | ||
|
|
||
| ## 4. ํต์ฌ ํ์ด ๋ฐฉ๋ฒ โญ | ||
| - **๋ถ๊ธฐ**: ๊ฐ ์ด์์ ๋งจ ์ ๋ฒฝ๋ ํ๋๋ฅผ ์ฐพ์(์์ผ๋ฉด skip) ๊ทธ ์ํ๋ฅผ ๋ณต์ฌํด ํญ๋ฐ ์๋ฎฌ๋ ์ด์ . | ||
| - **์ฐ์ ํญ๋ฐ(BFS)**: ํ์ ํฐ์ง ๋ฒฝ๋์ ๋ฃ๊ณ , ๋ฒฝ๋ ๊ฐ `block_size`๋งํผ 4๋ฐฉํฅ ๊ฑฐ๋ฆฌ `1..block_size-1`์ ํ์ด ๋ฒฝ๋์ด๋ฉด ํ์ ์ถ๊ฐ โ ์ฐ์ ์ฒ๋ฆฌ. | ||
| - **์ค๋ ฅ**: ๊ฐ ์ด์ ์๋์์๋ถํฐ ์ฑ์ ๋น์นธ ์์ด ์ ๋ ฌ(`gravity_fall`). | ||
| - **์ข ๋ฃยท๊ฐฑ์ **: ๋ฒฝ๋ ํฉ์ด 0์ด๋ฉด 0์ผ๋ก ํ์ , ๊ตฌ์ฌ ์์ง ์ ๋จ์ ๋ฒฝ๋ ์๋ฅผ `min`์ผ๋ก ๊ฐฑ์ . | ||
|
|
||
| ## 5. ์ค์ ์ฝ๋ | ||
| ```python | ||
| import copy | ||
| from collections import deque | ||
|
Comment on lines
+33
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ Performance & Scalability | ๐ต Trivial | โก Quick win
์์ ์์-import copy
from collections import deque
...
- ngrid = copy.deepcopy(grid)
+ ngrid = [row[:] for row in grid]๋ฌธ์์ ์ ์ฒด ๋ณต์ก๋ ํ๊ธฐ As per path instructions, SWEA ํ์ด์์๋ ์ต๋ ์ ๋ ฅ ๊ธฐ์ค TLE ์ํ์ ๋ฐ๋์ ์ ๊ฒํด์ผ ํฉ๋๋ค. Also applies to: 96-97 ๐ค Prompt for AI AgentsSource: Path instructions |
||
|
|
||
| T = int(input()) | ||
| for tc in range(1, T + 1): | ||
| N, W, H = map(int, input().split()) | ||
|
|
||
| arr = [list(map(int, input().split())) for _ in range(H)] | ||
|
|
||
| d = [(-1, 0), (0, 1), (1, 0), (0, -1)] | ||
|
|
||
| remain_blocks = int(1e10) | ||
| def gravity_fall(grid): | ||
| for x in range(W): | ||
| col = [grid[y][x] for y in range(H) if grid[y][x]] | ||
| col = [0] * (H - len(col)) + col | ||
| for y in range(H): | ||
| grid[y][x] = col[y] | ||
| return grid | ||
|
|
||
| # def gravity_fall(grid): | ||
|
|
||
| # h = len(grid) | ||
| # w = len(grid[0]) | ||
|
|
||
| # for x in range(w): | ||
| # tarr = [0]*h | ||
| # tidx = h-1 | ||
|
|
||
| # for y in range(h-1, -1, -1): | ||
| # if grid[y][x]: # ์์ผ๋ฉด | ||
| # tarr[tidx] = grid[y][x] | ||
| # tidx -= 1 | ||
|
|
||
| # for y in range(h): | ||
| # grid[y][x] = tarr[y] | ||
|
|
||
| # return grid | ||
|
|
||
| def dfs(remain_balls, grid): | ||
| global remain_blocks | ||
|
|
||
| if sum(sum(row) for row in grid) == 0: | ||
| remain_blocks = 0 | ||
| return | ||
|
|
||
| if remain_balls == 0: | ||
| remain_blocks = min(sum([grid[y][x] != 0 for y in range(H) for x in range(W)]), remain_blocks) | ||
| return | ||
| for fall_x in range(W): | ||
|
|
||
| find_block = False | ||
|
|
||
| bq = deque([]) | ||
|
|
||
| for y in range(H): | ||
| if grid[y][fall_x]: # ๋ธ๋ก์ด ์๋ค๋ฉด | ||
| find_block = True | ||
| bq.append((y, fall_x)) | ||
| break | ||
|
|
||
| if not find_block: continue | ||
|
|
||
| # ngrid = [row[:] for row in grid] # ๊น์ ๋ณต์ฌ | ||
| ngrid = copy.deepcopy(grid) | ||
|
|
||
| # 2. ๋ธ๋ก ๋ถ์๊ธฐ | ||
|
|
||
| while bq: | ||
| by, bx = bq.popleft() | ||
|
|
||
| block_size = ngrid[by][bx] | ||
| ngrid[by][bx] = 0 | ||
|
|
||
| for size in range(1, block_size): | ||
| for dy, dx in d: | ||
| ny = by + dy * size | ||
| nx = bx + dx * size | ||
|
|
||
| # ๊ทธ๋ฆฌ๋ ์์ด๊ณ , ๋ธ๋ก์ด๋ฉด | ||
| if (0 <= ny < H and 0 <= nx < W) and ngrid[ny][nx]: | ||
| bq.append((ny, nx)) | ||
|
|
||
| # ๋ธ๋ก ๋ถ์์ผ๋ gravity | ||
| ngrid = gravity_fall(ngrid) | ||
|
|
||
| dfs(remain_balls-1, ngrid) | ||
|
|
||
| dfs(N, arr) | ||
| print(f'#{tc} {remain_blocks}') | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### ๐ง ํ๊ณ | ||
| - "์ ํ ๋ถ๊ธฐ(DFS) + ์ํ ๋ณต์ฌ + ์๋ฎฌ๋ ์ด์ (ํญ๋ฐยท์ค๋ ฅ)" ์ ๊ฒฐํฉ. | ||
| - ์ํ๋ฅผ ๋ณต์ฌํด ๋ถ๊ธฐํ๋ฏ๋ก ๊ฐ ์ฌ๊ท๊ฐ ์๋ก ๊ฐ์ญํ์ง ์๋๋ค. ๋ณต์ฌ ๋ฐฉ์(`copy.deepcopy` vs ํ ๋ณต์ฌ)์ ์์๋์. | ||
| - ์ค๋ ฅ ์ฒ๋ฆฌ ๋ถ๋ถ `gravity_fall` ํจ์๋ CLAUDE ๊ฐ ์ถ์ฒํ ์ฝ๋๋ค. ์ฃผ์์ฒ๋ฆฌํ ๊ฑด ๋ณธ์ธ ์ฝ๋์ธ๋ฐ, CLAUDE์ ์ค๋ ฅ ํจ์๊ฐ ์ข ๋ ์ค์๊ฐ ์ ์ ๊ฒ ๊ฐ๋ค. 3์ฐจ์์์์ ์ค๋ ฅ ์ ์ฉ๋ ์๊ฐํด๋ณผ๋ง ํ๋ค. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๐ Maintainability & Code Quality | ๐ก Minor | โก Quick win
Markdown ์ ๋ชฉ๊ณผ ์ฝ๋ ํ์ค ๋ค์ ๋น ์ค์ ์ถ๊ฐํ์ธ์.
markdownlint์ MD022/MD031 ๊ฒฝ๊ณ ๋๋ก ๊ฐ ์ ๋ชฉ ๋ค์๊ณผ ์ฝ๋ ํ์ค ์๋ค์ ๋น ์ค์ด ํ์ํฉ๋๋ค. ์ฝ๋ ํ์ค์python์ธ์ด ํ๊ทธ ์์ฒด๋ ์ฌ๋ฐ๋ฆ ๋๋ค.Also applies to: 19-19, 23-23, 26-26, 33-34, 83-83
๐งฐ Tools
๐ช markdownlint-cli2 (0.23.1)
[warning] 13-13: Headings should be surrounded by blank lines
Expected: 1; Actual: 0; Below
(MD022, blanks-around-headings)
๐ค Prompt for AI Agents
Source: Linters/SAST tools