Skip to content

Commit

Permalink
Merge pull request #52 from AlgoLeadMe/13-g0rnn
Browse files Browse the repository at this point in the history
13-g0rnn
  • Loading branch information
g0rnn authored Jan 16, 2025
2 parents 31f1798 + 4874bd4 commit 0844805
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 0844805

Please sign in to comment.