From 71047c8fad899e703e51f203f427edc586a86b6c Mon Sep 17 00:00:00 2001 From: yeseoLee Date: Fri, 24 Jul 2026 15:53:05 +0900 Subject: [PATCH] =?UTF-8?q?solve:=20[W01]=20SWEA=201767=20=ED=94=84?= =?UTF-8?q?=EB=A1=9C=EC=84=B8=EC=84=9C=20=EC=97=B0=EA=B2=B0=ED=95=98?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\352\262\260\355\225\230\352\270\260.md" | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 "studies/week-01/yeseolee/1767-\355\224\204\353\241\234\354\204\270\354\204\234 \354\227\260\352\262\260\355\225\230\352\270\260.md" diff --git "a/studies/week-01/yeseolee/1767-\355\224\204\353\241\234\354\204\270\354\204\234 \354\227\260\352\262\260\355\225\230\352\270\260.md" "b/studies/week-01/yeseolee/1767-\355\224\204\353\241\234\354\204\270\354\204\234 \354\227\260\352\262\260\355\225\230\352\270\260.md" new file mode 100644 index 0000000..7cfee7e --- /dev/null +++ "b/studies/week-01/yeseolee/1767-\355\224\204\353\241\234\354\204\270\354\204\234 \354\227\260\352\262\260\355\225\230\352\270\260.md" @@ -0,0 +1,107 @@ +# [SWEA 1767] 프로세서 연결하기 + +| 항목 | 내용 | +|:---|:---| +| 🔗 문제 | https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV4suNtaXFEDFAUf | +| 📚 주제 | 백트래킹 | +| 🎚️ 난이도 | D4 | +| ⏱️ 소요 시간 | 2h | +| 🔁 다시 풀기 | 예 | + +--- + +## 1. 접근 방법 + +백트래킹 문제라는 것을 파악하고 풀었다. +경계 제거를 잘못 작성하여, 시간 초과가 발생했다. +AI를 통해 정답 코드를 확인했다. + +``` +# 잘못 작성한 코드 +for i in range(1, n): + for j in range(1, n): + if board[i][j] == 1: + cores.append((i, j)) +``` + +## 2. 시간 복잡도 + + +코어 수는 (7 ≤ N ≤ 12)이고, 각 코어는 상/하/좌/우/미포함 5가지 경우의 수를 가진다. +경계에 코어가 없을 경우, 최악의 경우 5^N 이나 그런 케이스에서는 시간 초과가 발생한다. + +**O( )** — + +## 3. 공간 복잡도 + +O(N^2) board 크기 이상을 요구하지 않는다. + +**O( )** — + +## 4. 핵심 풀이 방법 ⭐ + +1. 백트래킹 +2. 가장 자리에 있는 core는 이미 연결된 상태 (제거하지 않으면 시간 초과) + + +## 5. 실제 코드 + + +```python +def solution(n, board): + cores = [] + for i in range(n): + for j in range(n): + if board[i][j] == 1: + if i == 0 or i == n - 1 or j == 0 or j == n - 1: + continue + cores.append((i, j)) + + answer = [0, float("inf")] + dx, dy = [1, -1, 0, 0], [0, 0, -1, 1] + + def backtracking(idx, cnt, length): + nonlocal answer + if answer[0] < cnt or (answer[0] == cnt and answer[1] > length): + answer = [cnt, length] + + for i in range(idx, len(cores)): + # 전선-상,하,좌,우 + x, y = cores[i] + for d in range(4): + possible = True + path = [] + nx, ny = x + dx[d], y + dy[d] + while 0 <= nx < n and 0 <= ny < n: + if board[nx][ny] != 0: + possible = False + break + path.append((nx, ny)) + nx, ny = nx + dx[d], ny + dy[d] + if possible: + for nx, ny in path: + board[nx][ny] = -1 + backtracking(i + 1, cnt + 1, length + len(path)) + for nx, ny in path: + board[nx][ny] = 0 + # 전선 X + backtracking(i + 1, cnt, length) + + backtracking(0, 0, 0) + return answer[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)] + answer = solution(N, board) + print(f"#{test_case} {answer}") +``` + +--- + +### 🧠 회고 (선택) + +시간초과를 발생하지 않기 위해서는 경계에 있는 코어는 제외하는 것이 가장 중요하다. +추가로 `answer`를 갱신할 수 없는 경우는 가지치기를 통해 `backtracking`을 최적화 할 수 있다. \ No newline at end of file