From 5604374f89c2a9a1a76cec3026a26eb7c0f0febb Mon Sep 17 00:00:00 2001 From: kaswhy Date: Sun, 14 Dec 2025 22:09:49 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=207=EC=A3=BC=EC=B0=A8=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- soyeon/week7/archery.cpp | 62 ++++++++++++++++++++++++++++++++++++++++ soyeon/week7/nqueen.cpp | 32 +++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 soyeon/week7/archery.cpp create mode 100644 soyeon/week7/nqueen.cpp diff --git a/soyeon/week7/archery.cpp b/soyeon/week7/archery.cpp new file mode 100644 index 0000000..caf2ac2 --- /dev/null +++ b/soyeon/week7/archery.cpp @@ -0,0 +1,62 @@ +#include +#include +using namespace std; + +vector best; +int bestDiff; + +int calcDiff(const vector& ryan, const vector& apeach) { + int r = 0, a = 0; + for (int i = 0; i < 11; i++) { + int score = 10 - i; + if (ryan[i] == 0 && apeach[i] == 0) continue; + if (ryan[i] > apeach[i]) r += score; + else a += score; // 같은 발을 맞히면 어피치 + } + return r - a; +} + +// 낮은 점수(0점=인덱스10)부터 더 많이 맞힌 배열이 우선 +bool betterByRule(const vector& cand, const vector& curBest) { + for (int i = 10; i >= 0; i--) { + if (cand[i] != curBest[i]) return cand[i] > curBest[i]; + } + return false; +} + +void dfs(int idx, int arrowsLeft, const vector& apeach, vector& ryan) { + if (idx == 10) { // 0점 칸 + ryan[10] = arrowsLeft; + int diff = calcDiff(ryan, apeach); + if (diff > 0) { + if (diff > bestDiff || (diff == bestDiff && betterByRule(ryan, best))) { + bestDiff = diff; + best = ryan; + } + } + ryan[10] = 0; // backtrack + return; + } + + // 이 점수 칸을 이긴 경우: apeach[idx] + 1발 필요 + int need = apeach[idx] + 1; + if (need <= arrowsLeft) { + ryan[idx] = need; + dfs(idx + 1, arrowsLeft - need, apeach, ryan); + ryan[idx] = 0; + } + + // 포기(0발) + dfs(idx + 1, arrowsLeft, apeach, ryan); +} + +vector solution(int n, vector info) { + bestDiff = -1; + best.clear(); + + vector ryan(11, 0); + dfs(0, n, info, ryan); + + if (bestDiff <= 0) return vector{-1}; + return best; +} diff --git a/soyeon/week7/nqueen.cpp b/soyeon/week7/nqueen.cpp new file mode 100644 index 0000000..16f8aef --- /dev/null +++ b/soyeon/week7/nqueen.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +int n; +int answer = 0; +vector col, diag1, diag2; + +void dfs(int row) { + if (row == n) { + answer++; + return; + } + + for (int c = 0; c < n; c++) { + if (col[c] || diag1[row + c] || diag2[row - c + n - 1]) + continue; + + col[c] = diag1[row + c] = diag2[row - c + n - 1] = true; + dfs(row + 1); + col[c] = diag1[row + c] = diag2[row - c + n - 1] = false; + } +} + +int solution(int N) { + n = N; + col.assign(n, false); + diag1.assign(2 * n - 1, false); + diag2.assign(2 * n - 1, false); + + dfs(0); + return answer; +} From d4f3448c9be7b9578e478ed76f6b7aabc8e0bea4 Mon Sep 17 00:00:00 2001 From: kaswhy Date: Sun, 14 Dec 2025 22:12:00 +0900 Subject: [PATCH 2/3] Revert "hojoo/week6 (#31)" This reverts commit fff3a77fe8c65844e231895c6c5374ec71bdf072. --- hojoo/week6/eng_chain.py | 14 --------- hojoo/week6/sheep_wolf-dp.py | 61 ------------------------------------ 2 files changed, 75 deletions(-) delete mode 100644 hojoo/week6/eng_chain.py delete mode 100644 hojoo/week6/sheep_wolf-dp.py diff --git a/hojoo/week6/eng_chain.py b/hojoo/week6/eng_chain.py deleted file mode 100644 index ddc1ed3..0000000 --- a/hojoo/week6/eng_chain.py +++ /dev/null @@ -1,14 +0,0 @@ -# 주어진 단어를 순회하며 끝말잇기 규칙에 어긋나면 게임 종료 -# 시간 복잡도: O(n) - -def solution(n, words): - for i in range(1, len(words)): - if words[i-1][-1] != words[i][0] or words[i] in words[:i]: - return [(i % n) + 1, (i // n) + 1] - - return [0, 0] - -if __name__ == "__main__": - print(solution(3, ["tank", "kick", "know", "wheel", "land", "dream", "mother", "robot", "tank"])) - print(solution(5, ["hello", "observe", "effect", "take", "either", "recognize", "encourage", "ensure", "establish", "hang", "gather", "refer", "reference", "estimate", "executive"])) - print(solution(2, ["hello", "one", "even", "never", "now", "world", "draw"])) \ No newline at end of file diff --git a/hojoo/week6/sheep_wolf-dp.py b/hojoo/week6/sheep_wolf-dp.py deleted file mode 100644 index fce03ac..0000000 --- a/hojoo/week6/sheep_wolf-dp.py +++ /dev/null @@ -1,61 +0,0 @@ -# 비트마스크 + dp -# 시간 복잡도: O(N * 2^N) -def solution(info, edges): - n = len(info) - parent = [-1] * n - for p, c in edges: - parent[c] = p - - total_masks = 1 << n - sheep_count = [0] * total_masks - wolf_count = [0] * total_masks - - # mask를 구성하는 가장 낮은 비트부터 채우며 양/늑대 수 미리 계산 - for mask in range(1, total_masks): - lsb = mask & -mask - node = (lsb.bit_length() - 1) - prev_mask = mask ^ lsb - sheep_count[mask] = sheep_count[prev_mask] + (info[node] == 0) - wolf_count[mask] = wolf_count[prev_mask] + (info[node] == 1) - - candidate = [False] * total_masks - start_mask = 1 # 루트(0번 노드) 방문 상태 - if wolf_count[start_mask] < sheep_count[start_mask]: - candidate[start_mask] = True - - ans = 0 - for mask in range(total_masks): - if not candidate[mask]: - continue - - sheep = sheep_count[mask] - wolf = wolf_count[mask] - - if wolf >= sheep: - continue - - ans = max(ans, sheep) - - # 아직 방문하지 않은 노드 중 부모가 방문된 노드만 확장 - for node in range(n): - if mask & (1 << node): - continue - - parent_node = parent[node] - if parent_node != -1 and not (mask & (1 << parent_node)): - continue - - next_mask = mask | (1 << node) - next_sheep = sheep_count[next_mask] - next_wolf = wolf_count[next_mask] - - if next_wolf >= next_sheep: - continue - - candidate[next_mask] = True - - return ans - - -if __name__ == "__main__": - print(solution([0,0,1,1,1,0,1,0,1,0,1,1], [[0,1],[1,2],[1,4],[0,8],[8,7],[9,10],[9,11],[4,3],[6,5],[4,6],[8,9]])) \ No newline at end of file From cce13c8a94bc646cca3f4a1e1a50bf91560ce822 Mon Sep 17 00:00:00 2001 From: kaswhy Date: Sun, 21 Dec 2025 21:42:50 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=208=EC=A3=BC=EC=B0=A8=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- soyeon/week8/hanoi.cpp | 16 ++++++++++++++++ soyeon/week8/snail.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 soyeon/week8/hanoi.cpp create mode 100644 soyeon/week8/snail.cpp diff --git a/soyeon/week8/hanoi.cpp b/soyeon/week8/hanoi.cpp new file mode 100644 index 0000000..c15a109 --- /dev/null +++ b/soyeon/week8/hanoi.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +void hanoi(int n, int from, int via, int to, vector>& answer) { + if (n == 0) return; + + hanoi(n - 1, from, to, via, answer); + answer.push_back({from, to}); + hanoi(n - 1, via, from, to, answer); +} + +vector> solution(int n) { + vector> answer; + hanoi(n, 1, 2, 3, answer); + return answer; +} diff --git a/soyeon/week8/snail.cpp b/soyeon/week8/snail.cpp new file mode 100644 index 0000000..311a368 --- /dev/null +++ b/soyeon/week8/snail.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +vector solution(int n) { + vector> a(n, vector(n, 0)); + vector answer; + + int x = -1, y = 0; + int num = 1; + + for (int i = 0; i < n; i++) { + for (int j = i; j < n; j++) { + if (i % 3 == 0) { // 아래 + x++; + } else if (i % 3 == 1) { // 오른쪽 + y++; + } else { // 왼쪽 위 + x--; + y--; + } + a[x][y] = num++; + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0; j <= i; j++) { + answer.push_back(a[i][j]); + } + } + + return answer; +}