[W01] Suyoung-Min - SWEA 1767번 - #2
Conversation
📝 WalkthroughWalkthroughSWEA 1767 풀이 문서가 추가되었다. 내부 코어를 대상으로 전선 연결 여부와 네 방향을 백트래킹으로 탐색하며, 연결 코어 수 최대와 전선 길이 최소를 기준으로 가지치기와 격자 복원을 수행한다. Changes프로세서 연결 풀이
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@studies/week-01/Suyoung-Min/1767-프로세서연결하기.md`:
- Line 91: ASCII 인덱스 다이어그램의 코드 펜스에 text 언어 태그를 추가하세요. 해당 문서의 언어 태그가 없는 모든 펜스를
```text로 변경해 Markdown lint 경고 MD040을 해결하고, 다이어그램 내용은 그대로 유지하세요.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 1de2ea0d-f102-4688-8d58-d20b2fad43eb
📒 Files selected for processing (1)
studies/week-01/Suyoung-Min/1767-프로세서연결하기.md
| `rest = len(cores) - cidx`이지 `len(cores) - 1 - cidx`가 아니다. | ||
| `backtrack(cidx, cnum, ...)` 시점에서 `cnum`은 **인덱스 0 ~ cidx-1**만 센 값이고, `cidx`는 아직 미결정 = **앞으로 연결 가능한 후보**다. | ||
|
|
||
| ``` |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
코드 펜스에 언어 태그를 추가하세요.
ASCII 인덱스 다이어그램이므로 text 태그를 사용하면 Markdown lint 경고(MD040)를 해소할 수 있습니다.
수정 예시
-```
+```text📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| ``` |
🧰 Tools
🪛 markdownlint-cli2 (0.23.0)
[warning] 91-91: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@studies/week-01/Suyoung-Min/1767-프로세서연결하기.md` at line 91, ASCII 인덱스 다이어그램의 코드
펜스에 text 언어 태그를 추가하세요. 해당 문서의 언어 태그가 없는 모든 펜스를 ```text로 변경해 Markdown lint 경고
MD040을 해결하고, 다이어그램 내용은 그대로 유지하세요.
Sources: Path instructions, Linters/SAST tools
clarityth
left a comment
There was a problem hiding this comment.
Code Review by clarityth
좋았던 점:
- 2가지 가지치기 조건 도출
- 방문 로직 처리를 위한 visited 배열을 별도로 선언하는 것이 아닌 grid에 직접적으로 기록해 공간복잡도를 줄인 점
이미 제가 작성한 코드보다 훨씬 좋은 코드라고 생각하지만, 추가적으로 제가 생각한 개선점은 다음과 같습니다.
1. 경로 검사 로직 플래그 변수 제거
- 기존 코드 로직에서 무한루프문으로 진행하기 때문에 break 조건이 잘못 설정 됐을 경우의 위험성이 있다고 생각합니다.
- 따라서 아래와 같이 while문의 조건식을 움직일 수 있는 조건식으로 설정하고, break문을 만나지 않고 정상 종료되었을 때(else 블록) 수행할 작업을 작성하면 가독성 측면에서 더 좋을 것 같습니다.
- 또한 방문 처리 마킹 작업에서도 앞서 도출한
cur_line_length를 활용해 for문으로 지정된 횟수만큼만 반복하도록 수정하면 좋을 것 같습니다.
기존 코드
cover_flag = False
cur_line_length = 0
ny, nx = cy, cx
while True:
ny += dy; nx += dx
if ny < 0 or ny >= n or nx < 0 or nx >= n: break
if grid[ny][nx] != 0:
cover_flag = True
break
cur_line_length += 1
if not cover_flag:
# 방문 처리 및 복원 시 while True와 경계 조건 검사 반복
ny, nx = cy, cx
while True:
ny += dy; nx += dx
if ny < 0 or ny >= n or nx < 0 or nx >= n: break
grid[ny][nx] = 2
개선 코드
cur_line_length = 0
ny, nx = cy, cx
while 0 <= ny + dy < n and 0 <= nx + dx < n:
ny += dy; nx += dx
if grid[ny][nx] != 0:
break
cur_line_length += 1
else:
# 방문 처리 (계산된 길이만큼만 for문 반복)
ny, nx = cy, cx
for _ in range(cur_line_length):
ny += dy; nx += dx
grid[ny][nx] = 2
2. 가장자리에 위치한 코어 우선 탐색
- 현재 코드는 좌측 상단부터 우측 하단까지 순회하며 코어의 위치를 기록하고, 그 기록된 순서에 따라 코어들의 연결 여부를 판정하고 있습니다.
- 가장자리와의 거리를 기준으로 오름차순으로 정렬해 외곽에 위치한 코어를 우선적으로 탐색하면, 이후 가지치기 로직과 시너지를 내서 탐색 횟수가 더욱 줄어들 것 같습니다.
기존 코드
for y in range(1, n-1):
for x in range(1, n-1):
if grid[y][x]: cores.append((y, x))
...
cy, cx = cores[cidx]
추가 로직
# 코어를 가장자리와 가까운 순으로 오름차순 정렬
cores.sort(key=lambda c: min(c[0], c[1], n - 1 - c[0], n - 1 - c[1]))
3. 최댓값 표기법
- 초기 최댓값으로
int(1e10)을 사용하고 계시는데, 사실 일반적인 케이스에선 크게 문제가 되지 않지만,float('inf')가 좀 더 의미적으로 명확하게 전달할 수 있을 것 같습니다!
📌 이번 PR 내용
✅ 푼 문제 (SWEA)
🧾 5요소 체크리스트
각 풀이에 아래 5요소를 모두 작성했는지 확인합니다.
📋 규칙 체크
studies/week-XX/<깃허브ID>/<문제번호>-<문제이름>.md)💬 리뷰어에게
🧠 이번 주 회고 (한 줄)
백트래킹 구조 구성에 가지치기의 효율성을 명심하자
Summary by CodeRabbit