-
Notifications
You must be signed in to change notification settings - Fork 0
solve: [W02] SWEA 26070, 26071, 5656, 5658 #10
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 | | ||
| | ๐ ์ฃผ์ | 0-1 BFS | | ||
| | ๐๏ธ ๋์ด๋ | D4 | | ||
| | โฑ๏ธ ์์ ์๊ฐ | 1H (์คํจ) | | ||
| | ๐ ๋ค์ ํ๊ธฐ | ์ | | ||
|
|
||
| --- | ||
|
|
||
| ## 1. ์ ๊ทผ ๋ฐฉ๋ฒ | ||
|
|
||
| * ์ต๋จ ๊ฑฐ๋ฆฌ๊ฐ ์๋ ์ต์ ํ์ ํ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ | ||
| * ์ด๋์ ๋ฐ๋ฅธ ๋น์ฉ์ด ๋ค๋ฆ -> ๋ค์ต์คํธ๋ผ | ||
| * ๊ฐ์ค์น๊ฐ ์ค์ง 0๊ณผ 1 -> ๋ค์ต์คํธ๋ผ ๋์ 0-1 BFS | ||
|
|
||
| ## 2. ์๊ฐ ๋ณต์ก๋ | ||
|
|
||
| **O(N^2 * M)** โ | ||
|
|
||
| * (N * N ์ขํ) * (4 ๋ฐฉํฅ) * (M ๋ชฉํ๋ฌผ ๊ฐ์) * (2 ํ์ ์ฌ๋ถ ์ํ) | ||
|
|
||
| ## 3. ๊ณต๊ฐ ๋ณต์ก๋ | ||
|
|
||
| **O(N^2 * M)** โ | ||
|
|
||
| * ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ๋ฅผ ์ํ `visited` Set๊ณผ ํ์์ ์ํ `deque`์ ์ต๋ ์ํ ๊ณต๊ฐ์ ํฌ๊ธฐ๋งํผ ๋ฐ์ดํฐ๊ฐ ์ ์ฌ | ||
|
|
||
| ## 4. ํต์ฌ ํ์ด ๋ฐฉ๋ฒ | ||
|
|
||
| * **0-1 BFS:** `que.appendleft()`๋ฅผ ์ฌ์ฉํด ๋น์ฉ์ด 0์ธ ์ง์ง์ ๋ฑ์ ์ต์๋จ์ ๋ฐฐ์นํ๊ณ , `que.append()`๋ฅผ ์ฌ์ฉํด ๋น์ฉ์ด 1์ธ ์ฐํ์ ์ ๋ค์ ๋ฐฐ์นํ์ฌ ํ์ ์ฐ์ ์์๋ฅผ ๋ณด์ฅ | ||
| * **์ํ ๊ด๋ฆฌ:** `(x, y, d, jewel, flag)`๋ฅผ ํํ๋ก ๋ฌถ์ด ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ(`visited`)์ ํค๋ก ์ฌ์ฉ | ||
|
|
||
| ## 5. ์ค์ ์ฝ๋ | ||
|
|
||
| ```python | ||
| from collections import deque | ||
|
|
||
|
|
||
| def solution(n, m, board): | ||
| dx, dy = [0, 1, 0, -1], [1, 0, -1, 0] | ||
| visited = set() | ||
| que = deque([(0, 0, 0, 0, 1, False)]) | ||
|
|
||
| while que: | ||
| x, y, d, rotate, jewel, flag = que.popleft() | ||
|
|
||
| if board[x][y] == jewel: | ||
| jewel += 1 | ||
| flag = False | ||
| if jewel == m + 1: | ||
| return rotate | ||
|
|
||
| state = (x, y, d, jewel, flag) | ||
| if state in visited: | ||
| continue | ||
| visited.add(state) | ||
|
|
||
| # ์ง์ง | ||
| nx, ny = x + dx[d], y + dy[d] | ||
| if 0 <= nx < n and 0 <= ny < n: | ||
| que.appendleft((nx, ny, d, rotate, jewel, False)) | ||
|
|
||
| # ํ์ | ||
| if not flag: | ||
| que.append((x, y, (d + 1) % 4, rotate + 1, jewel, True)) | ||
|
|
||
| return -1 | ||
|
|
||
|
|
||
| T = int(input()) | ||
| for test_case in range(1, T + 1): | ||
| N = int(input()) | ||
| BOARD = [list(map(int, input().split())) for _ in range(N)] | ||
| M = max([max(row) for row in BOARD]) | ||
| answer = solution(N, M, BOARD) | ||
| print(f"#{test_case} {answer}") | ||
|
|
||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### ํ๊ณ (์ ํ) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,144 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # [SWEA 26071] ๋ธ๋ก ์ ๊ฑฐ ๊ฒ์ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| | ํญ๋ชฉ | ๋ด์ฉ | | ||||||||||||||||||||||||||||||||||||||||||||||||||
| |:---|:---| | ||||||||||||||||||||||||||||||||||||||||||||||||||
| | ๐ ๋ฌธ์ | https://swexpertacademy.com/main/code/userProblem/userProblemDetail.do?contestProbId=AZwmCVWq3uLHBIT3& | | ||||||||||||||||||||||||||||||||||||||||||||||||||
| | ๐ ์ฃผ์ | ๋ฉ๋ชจ์ด์ ์ด์ + ๋ถํ ์ ๋ณต (DP) | | ||||||||||||||||||||||||||||||||||||||||||||||||||
| | ๐๏ธ ๋์ด๋ | D4 | | ||||||||||||||||||||||||||||||||||||||||||||||||||
| | โฑ๏ธ ์์ ์๊ฐ | 1h 30m (์คํจ) | | ||||||||||||||||||||||||||||||||||||||||||||||||||
| | ๐ ๋ค์ ํ๊ธฐ | ์ | | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 1. ์ ๊ทผ ๋ฐฉ๋ฒ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| * ์ฒ์ ์ฝ๋๋ `permutations`๋ฅผ ์ฌ์ฉํ์ฌ ๋ธ๋ก์ ๊นจ๋ ๋ชจ๋ ์์๋ฅผ ํ์ํ๋ ์์ ํ์ ๋ฐฉ์์ผ๋ก ์ ๊ทผ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| * ์์ด์ ์ด์ฉํ ๋ฐฉ์์ N์ด ์ปค์ง ๊ฒฝ์ฐ ํ์ ๋ฒ์๊ฐ ๊ธฐํ๊ธ์์ ์ผ๋ก ๋์ด๋ ์๊ฐ ์ด๊ณผ๊ฐ ๋ฐ์ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| * ์ต์ ํ๋ฅผ ์ํด ๋ฉ๋ชจ์ด์ ์ด์ ๋์ -> ํน์ ๊ตฌ๊ฐ `[i, j]`์์ ์ฒ์ ๊นฐ ๋ธ๋ก์ด ์๋, **๊ฐ์ฅ ๋ง์ง๋ง์ ๊นฐ ๋ธ๋ก**์ `mid`๋ก ์ค์ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| * ์ด๋ฅผ ํตํด ๋จ์ ๋ธ๋ก๋ค์ด ์๋ก ์ํฅ์ ์ฃผ์ง ์๋ ๋ ๊ฐ์ ๋ ๋ฆฝ์ ์ธ ๋ถ๋ถ ๋ฌธ์ (`[i, mid - 1]`์ `[mid + 1, j]`)๋ก ๋๋๋ ๋ถํ ์ ๋ณต ๋ฐฉ์์ผ๋ก ์ ๊ทผ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 2. ์๊ฐ ๋ณต์ก๋ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| **O(Nยณ)** โ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| * DP ํ ์ด๋ธ์ ์ํ(State) ๊ฐ์๋ `i`๋ถํฐ `j`๊น์ง์ ์กฐํฉ์ด๋ฏ๋ก O(Nยฒ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| * ๊ฐ ์ํ `dp[i][j]`๋ฅผ ๊ณ์ฐํ ๋, ๋ถํ ์ `mid`๋ฅผ `i`๋ถํฐ `j`๊น์ง ์ํํ๋ฉฐ ์ต๋๊ฐ์ ์ฐพ์ผ๋ฏ๋ก O(N)์ ์๊ฐ์ด ์ถ๊ฐ๋ก ์์ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| * ๋ฐ๋ผ์ ์ด O(Nยฒ) * O(N) = O(Nยณ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 3. ๊ณต๊ฐ ๋ณต์ก๋ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| **O(Nยฒ)** โ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| * ๊ณ์ฐ๋ ๊ตฌ๊ฐ ์ ์์ ์ต๋๊ฐ์ ์ ์ฅํ๊ธฐ ์ํ 2์ฐจ์ `dp` ๋ฐฐ์ด์ ํฌ๊ธฐ๊ฐ N x N์ด๋ฏ๋ก O(Nยฒ)์ ๊ณต๊ฐ์ด ํ์ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 4. ํต์ฌ ํ์ด ๋ฐฉ๋ฒ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| * **๋ง์ง๋ง์ ํฐ์ง ๋ธ๋ก ๊ธฐ์ค:** ์ฒ์๋ถํฐ ์ด๋ค ๋ธ๋ก์ ํฐ๋จ๋ฆด์ง ๊ณ ๋ฅด๋ฉด, ํฐ์ง ๋ธ๋ก์ผ๋ก ์ธํด ์ธ์ ๋ธ๋ก์ด ๊ณ์ ๋ณํ์ฌ ๋ถ๋ถ ๋ฌธ์ ๋ฅผ ๋ ๋ฆฝ์ ์ผ๋ก ๋๋ ์ ์์ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| * **๋ฉ๋ชจ์ด์ ์ด์ :** `dp[i][j]`๋ฅผ `i`๋ฒ์งธ๋ถํฐ `j`๋ฒ์งธ ๋ธ๋ก๊น์ง ํฐ๋จ๋ ธ์ ๋ ์ป์ ์ ์๋ ์ต๋ ์ ์๋ก ์ ์ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 5-1. ์ค์ ์ฝ๋ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def solution(n, arr): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| dp = [[-1] * (n) for _ in range(n)] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # ๋จ์ผ ๋ธ๋ก ๋ถ์๊ณ ์ ์ ํ๋ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_boundary_score(i, j, mid): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| left, right = i - 1, j + 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if left >= 0 and right < n: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return arr[left] * arr[right] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| elif left >= 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return arr[left] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| elif right < n: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return arr[right] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return arr[mid] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # ๊ตฌ๊ฐ ์ ์ ์ต๋๊ฐ (๋ถํ ์ ๋ณต) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_max_score(i, j): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if i > j: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if dp[i][j] != -1: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return dp[i][j] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| max_score = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for mid in range(i, j + 1): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cur_score = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| get_max_score(i, mid - 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| + get_boundary_score(i, j, mid) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| + get_max_score(mid + 1, j) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| max_score = max(max_score, cur_score) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| dp[i][j] = max_score | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return max_score | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| return get_max_score(0, n - 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| T = int(input()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for test_case in range(1, T + 1): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| N = int(input()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| K = list(map(int, input().split())) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| answer = solution(N, K) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"#{test_case} {answer}") | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 5-2. ์๋ชป๋ ์ฝ๋ (ํ ์ผ๋ง ๊ฒฝ์ฐ ํต๊ณผ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_score(n,arr,order): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| score = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| for idx,mid in enumerate(order): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| left,right = mid-1, mid+1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| while True: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if arr[left] == 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| left -= 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if arr[right] == 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| right += 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if idx != len(order)-1: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| score += arr[left] * arr[right] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| arr[mid] = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| case_1 = arr[left] * arr[right] + max(arr[left],arr[right]) *2 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| case_2 = arr[mid] * 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| score += max(case_1,case_2) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| return score | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def solution(n, arr): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if n == 1: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return arr[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if n == 2: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return max(arr) * 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| max_score = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ps = permutations(range(1,n-1), n-2) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| for p in ps: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| score = get_score(n,arr[::],p) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+115
to
+125
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. ๐ฏ Functional Correctness | ๐ก Minor | โก Quick win ๋น๊ต์ฉ ์ฝ๋์ ํ์ฌ ์ฝ๋ ๋ธ๋ก์๋ ์์ ์์+from itertools import permutations
+
def solution(n, arr):๐ Committable suggestion
Suggested change
๐ค Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||
| max_score = max(max_score,score) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| return max_score | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+115
to
+128
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 ์์ด ๊ตฌํ์ ์ค์ ๋ณต์ก๋์ TLE ์ํ์ ๊ตฌ์ฒด์ ์ผ๋ก ๊ธฐ๋กํด ์ฃผ์ธ์.
๐ค Prompt for AI AgentsSource: Path instructions |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| T = int(input()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for test_case in range(1, T + 1): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| N = int(input()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| K = list(map(int,input().split())) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| answer = solution(N,K) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"#{test_case} {answer}") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| ### ํ๊ณ (์ ํ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| * "๋ง์ง๋ง์ ์ํํ ์์ "์ ๊ธฐ์ค์ผ๋ก ๋๋๋ ๊ตฌ๊ฐ DP์ ์ ํ์ ์ธ ํ ํฌ๋(๋ฆฌํธ์ฝ๋ 'Burst Balloons' ์ ํ)์ ์์งํด ๋๋ฉด ์ ์ฌํ ๋ฌธ์ ์ ๋ฐ๋ก ์ ์ฉํ ์ ์์ต๋๋ค. MM ๋ฏผ๊ตฌ๋์ด ๊ณต์ ํ์ ๋งํฌ ํ์ด๋ณด์๋ฉด ์ข์ต๋๋ค. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,125 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # [SWEA 5566] ๋ฒฝ๋ ๊นจ๊ธฐ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. ๐๏ธ Data Integrity & Integration | ๐ก Minor | โก Quick win ๋ฌธ์ ๋ฒํธ๋ฅผ 5656์ผ๋ก ํต์ผํด ์ฃผ์ธ์. PR ๋ชฉํ์ ํ์ผ ๊ฒฝ๋ก๋ SWEA 5656์ธ๋ฐ ์ ๋ชฉ๊ณผ ํ์ผ๋ช
์ ๐ค Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | ํญ๋ชฉ | ๋ด์ฉ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |:---|:---| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | ๐ ๋ฌธ์ | https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWXRQm6qfL0DFAUo | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | ๐ ์ฃผ์ | BFS + ๋ฐฑํธ๋ํน | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | ๐๏ธ ๋์ด๋ | ์ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | โฑ๏ธ ์์ ์๊ฐ | 1h 30m | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | ๐ ๋ค์ ํ๊ธฐ | ์ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 1. ์ ๊ทผ ๋ฐฉ๋ฒ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * **๋ฐฐ์ด ํ์ :** 2์ฐจ์ ๋ฐฐ์ด์ ์ค๋ ฅ ์์ฉ(๋น ๊ณต๊ฐ์ ์ฑ์ฐ๋ฉฐ ์๋๋ก ๋จ์ด์ง๋ ๊ฒ)์ ์ฝ๊ฒ ์ฒ๋ฆฌํ๊ธฐ ์ํด ๋ณด๋๋ฅผ ์๊ณ ๋ฐฉํฅ์ผ๋ก 90๋ ํ์ . ์ด์ด ํ์ผ๋ก ๋ณํ๋๋ฉฐ, ๋จ์ํ 0์ ์ ์ธํ ๋ฐฐ์ด์ ๋ฌถ๊ณ ๋ค์ 0์ ์ฑ์ฐ๋ ๋ฐฉ์์ผ๋ก ์ค๋ ฅ์ ๊ตฌํ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * **์ฐ์ ํญ๋ฐ (BFS):** ๋ฒฝ๋์ด ๊นจ์ง ๋ ์ฃผ๋ณ ๋ฒฝ๋๋ ์ฐ์์ ์ผ๋ก ๊นจ์ง๋ฏ๋ก, ์ด๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด BFS ์ฌ์ฉ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * **๋ฐฑํธ๋ํน:** ๋ณด๋์ ์ํ๋ฅผ ๋ฐฑ์ (`prev_board`)ํ๊ณ , ํน์ ์ด์ ๊ตฌ์ฌ์ ๋จ์ด๋จ๋ฆฐ ๋ค(BFS ๋ฐ ์ค๋ ฅ ์ ์ฉ), ๋ค์ ์ฌ๊ท๋ก ๋์ด๊ฐ๋ฉฐ ์ต๋๊ฐ์ ๊ฐฑ์ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 2. ์๊ฐ ๋ณต์ก๋ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| **O(W^N * W * H)** โ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * ๊ตฌ์ฌ์ ๋จ์ด๋จ๋ฆด ์ด์ ์ ํํ๋ ๊ฒฝ์ฐ์ ์๋ ์ต๋ **W^N** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * ๋งค ๊ฒฝ์ฐ๋ง๋ค ์ฐ์ ํญ๋ฐ์ ์ฒ๋ฆฌํ๋ BFS์ ์ค๋ ฅ ์์ฉ ํจ์๊ฐ ์คํ๋๋ฉฐ, ์ด๋ ๋ณด๋์ ๋ชจ๋ ์นธ์ ํ ๋ฒ์ฉ ์ํํ๋ฏ๋ก **O(W * H)** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+21
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 | ๐ก Minor | โก Quick win BFS ๋น์ฉ์ ๋ณต์ก๋์ ๋ฐ์ํด ์ฃผ์ธ์.
๊ฒฝ๋ก ์ง์นจ์ ์๊ฐ ๋ณต์ก๋ ๊ฒ์ฆ ์๊ตฌ์ ๋ฐ๋ผ ์ค์ ๋ฐ๋ณต๋ฌธ ๊ธฐ์ค์ผ๋ก ๊ณ์ฐํ์ต๋๋ค. ๐ค Prompt for AI AgentsSource: Path instructions |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 3. ๊ณต๊ฐ ๋ณต์ก๋ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| **O(N * W * H)** โ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * ๋ฐฑํธ๋ํน ์ฌ๊ท ํธ์ถ ์ต๋ **N** ๊น์ด | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * ๊ฐ ์ฌ๊ท ๊น์ด๋ง๋ค ๋ณด๋์ ์ํ๋ฅผ ์ ์ฅํ๊ธฐ ์ํด `prev_board` ๋ฐฐ์ด(**W * H**)์ ๋ณต์ฌ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * BFS ํ์ ์ ์ฌ์ฉ๋๋ ํ์ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ์ฉ ์งํฉ(Set)์ด ์ต๋ **W * H** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 4. ํต์ฌ ํ์ด ๋ฐฉ๋ฒ โญ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * **๋ฐฐ์ด 90๋ ํ์ :** `board = [list(row) for row in zip(*board[::-1])]` ์ฝ๋๋ฅผ ํตํด 2์ฐจ์ ๋ฐฐ์ด์ ์๊ณ ๋ฐฉํฅ์ผ๋ก ํ์ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * **์ค๋ ฅ ๊ตฌํ:** ๋ฆฌ์คํธ ์ปดํ๋ฆฌํจ์ ์ ์ด์ฉํด 0์ด ์๋ ๊ฐ๋ง ์ถ์ถํ๊ณ , ๊ธฐ์กด ๋ฐฐ์ด์ ๊ธธ์ด์์ ์ถ์ถํ ๊ฐ์ ๊ธธ์ด๋ฅผ ๋บ ๋งํผ 0์ `extend` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * **๊ฐ์ง์น๊ธฐ:** ๋จ์ ํ์(`tries`)๊ฐ 0์ด ๋๊ฑฐ๋, ํ์ฌ๊น์ง ๊นฌ ๋ฒฝ๋ ์(`cnt`)๊ฐ ์ ์ฒด ๋ฒฝ๋ ์(`block_cnt`)์ ๋์ผํด์ง๋ฉด ์กฐ๊ธฐ ์ข ๋ฃ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * **ํ๊ณต ์ฒ๋ฆฌ:** ํน์ ์ด์ ๋ฒฝ๋์ด ํ๋๋ ์๋ ๊ฒฝ์ฐ(ํ๊ณต) ํด๋น ์ด์์์ ์งํ์ ํจ์ค | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 5. ์ค์ ์ฝ๋ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from collections import deque | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def solution(n, w, h, board): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # ์๊ณ 90๋ ํ์ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| board = [list(row) for row in zip(*board[::-1])] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| block_cnt = sum([1 for i in range(w) for j in range(h) if board[i][j] != 0]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max_break_cnt = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dx, dy = [0, 0, 1, -1], [1, -1, 0, 0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _blast(i, j): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nonlocal board | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| visited = set([(i, j)]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| que = deque([(i, j)]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while que: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x, y = que.popleft() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| radius = board[x][y] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| board[x][y] = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if radius <= 1: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for d in range(4): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for r in range(1, radius): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nx, ny = x + dx[d] * r, y + dy[d] * r | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not (0 <= nx < w and 0 <= ny < h): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (nx, ny) in visited: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if board[nx][ny] == 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| visited.add((nx, ny)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| que.append((nx, ny)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return len(visited) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _gravity(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nonlocal board | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for x in range(w): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| arr = [board[x][y] for y in range(h) if board[x][y] != 0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| arr.extend([0] * (h - len(arr))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| board[x] = arr | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _backtracking(tries, cnt): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nonlocal max_break_cnt, board | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if cnt == block_cnt or tries == 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max_break_cnt = max(max_break_cnt, cnt) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for x in range(w): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for y in range(h - 1, -1, -1): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if board[x][y] != 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prev_board = [row[:] for row in board] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cur_cnt = _blast(x, y) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _gravity() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _backtracking(tries - 1, cnt + cur_cnt) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| board = prev_board | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # ํ๊ณต | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _backtracking(tries - 1, cnt) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+96
to
+107
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 ๋น ์ด ์ฌ๊ท ํธ์ถ์ ํ ๋ฒ์ผ๋ก ํฉ์ณ ์ฃผ์ธ์. ๋น ์ด๋ง๋ค ๋ณด๋ ์ํ๊ฐ ์ ํ ๋ฐ๋์ง ์์ ๋์ผํ ๊ฐ์ ์์+ has_empty_column = False
for x in range(w):
for y in range(h - 1, -1, -1):
if board[x][y] != 0:
...
board = prev_board
break
else:
- # ํ๊ณต
- _backtracking(tries - 1, cnt)
+ has_empty_column = True
+
+ if has_empty_column:
+ _backtracking(tries - 1, cnt)๊ฒฝ๋ก ์ง์นจ์ SWEA ์๊ฐ ์ ํ ๋ฐ ๋นํจ์จ ํ์ ์ ๊ฒ ์๊ตฌ๋ฅผ ์ ์ฉํ์ต๋๋ค. ๐ Committable suggestion
Suggested change
๐ค Prompt for AI AgentsSource: Path instructions |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _backtracking(n, 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return block_cnt - max_break_cnt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| T = int(input()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| answer = solution(N, W, H, board) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"#{test_case} {answer}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ### ๐ง ํ๊ณ (์ ํ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. ๐ Maintainability & Code Quality | ๐ก Minor | โก Quick win Heading ๋ค์ ๋น ์ค์ ์ถ๊ฐํด ์ฃผ์ธ์. Markdownlint MD022 ๊ฒฝ๊ณ ๊ฐ ๋ฐ์ํ๋ฏ๋ก ๐งฐ Tools๐ช markdownlint-cli2 (0.23.1)[warning] 124-124: Headings should be surrounded by blank lines (MD022, blanks-around-headings) ๐ค Prompt for AI AgentsSource: Linters/SAST tools |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * ๋ฐฐ์ด์ ํ์ ์ํค๋ ์์ด๋์ด๋ฅผ ์ ์ฉํ๋ฉด ์ค๋ ฅ ์ฒ๋ฆฌ์ ๊ฐ์ ํน์ ์ด๋ ๋ก์ง์ด ์ฌ์์ง๋๋ค. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
๐ฏ Functional Correctness | ๐ก Minor | โก Quick win
์ ์ฒด ์ ๋ ฅ ๊ธฐ์ค ๋ณต์ก๋๋ ๋ช ์ํด ์ฃผ์ธ์.
ํ์ฌ
O(Nยณ)๊ณ์ฐ์ ํ ์คํธ ์ผ์ด์ค 1๊ฐ ๊ธฐ์ค์ผ๋ก๋ ๋ง์ต๋๋ค. ๋ค๋ง ์ค์ ์ฝ๋๋T๊ฐ ํ ์คํธ ์ผ์ด์ค๋ฅผ ์ํํ๋ฏ๋ก ์ ์ฒด ์๊ฐ ๋ณต์ก๋๋O(TยทNยณ)(๋๋O(ฮฃN_tยณ))์ ๋๋ค. ๋ฌธ์์ โํ ์คํธ ์ผ์ด์ค 1๊ฐ ๊ธฐ์คโ์ด๋ผ๊ณ ๋ช ์ํด ์ฃผ์ธ์.์์ ์์
๐ Committable suggestion
๐ค Prompt for AI Agents
Source: Path instructions