Skip to content

Commit

Permalink
2025-01-05 스타트와 링크
Browse files Browse the repository at this point in the history
  • Loading branch information
g0rnn committed Jan 5, 2025
1 parent 43c05bb commit 4874bd4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions g0rnn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
| 10차시 | 2024.12.20 | 그래프 | [결혼식](https://www.acmicpc.net/problem/5567) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/40 |
| 11차시 | 2024.12.30 | dp | [기타리스트](https://www.acmicpc.net/problem/1495) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/49 |
| 12차시 | 2025.01.02 | bfs | [회장뽑기](https://www.acmicpc.net/problem/2660) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/50 |
| 13차시 | 2025.01.05 | 백트래킹 | [스타트와 링크](https://www.acmicpc.net/problem/14889) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/52 |


---
50 changes: 50 additions & 0 deletions g0rnn/backtracking/13-g0rnn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Created by 김균호 on 2025. 1. 5..
//
#include <iostream>
#include <climits>
#include <vector>
using namespace std;

int n, minDiff = INT_MAX;
int s[25][25];
vector<bool> visited;

void sumPoint() {
int start = 0, link = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (visited[i] && visited[j]) start += s[i][j] + s[j][i];
else if (!visited[i] && !visited[j]) link += s[i][j] + s[j][i];
}
}
minDiff = min(minDiff, abs(start - link));
}

// 임의의 n/2개를 뽑음
void dfs(int cur, int cnt) {
if (cnt == n / 2) {
sumPoint();
return;
}
for (int i = cur; i <= n; i++) {
if (!visited[i]) {
visited[i] = true;
dfs(i + 1, cnt + 1);
visited[i] = false;
}
}
}

int main() {
cin >> n;
for (int y = 1; y <= n; y++) {
for (int x = 1; x <= n; x++) {
cin >> s[y][x];
}
}
visited = vector<bool>(n + 1, false);
dfs(1, 0);
cout << minDiff;
return 0;
}

0 comments on commit 4874bd4

Please sign in to comment.