Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7-dhlee777 #31

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dhlee777/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
| 3차시 | 2024.03.23 | union-find | [집합의 표현](https://www.acmicpc.net/problem/1717) | [#14](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/14)|
| 4차시 | 2024.03.25 | BFS | [바이러스](https://www.acmicpc.net/problem/2606) | [#17](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/17)|
| 5차시 | 2024.03.30 | mst | [최소스패닝트리](https://www.acmicpc.net/problem/1197) | [#21](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/21)|
| 6차시 | 2024.04.04 | backtracking | [스타트와링크](https://www.acmicpc.net/problem/14889) | [#29](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/29)|
| 7차시 | 2024.04.08 | backtracking | [n과m(5)](https://www.acmicpc.net/problem/15654) | [#31](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/31)|




Expand Down
36 changes: 36 additions & 0 deletions dhlee777/backtracking/n과m(5).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int num_size, picked_size;
int num[8]; //�Է¹��� ���ڵ��� �����ϴ� �迭
bool visited[8]; //�湮�ߴ��� Ȯ���ϱ����� �迭
vector<int>v; //���ڵ��� ������� �ֱ����� ����
void dfs(int cnt, int vt) { //cnt�� �湮���� ī����,vt�� ���� ���ڸ� ��Ÿ����.
if (cnt == picked_size) { // �湮�� ���� �Է¹��� �����ϴ� ������ �������,dfs(3,2)�� ��� ��°�ڸ����� num[2]�϶��� �������� �������ִ� �Լ�

for (auto k : v) {
cout << k << " "; //���Ϳ� �ִ� ������ ���
}
cout << "\n";
}
for (int j = 0; j < num_size; j++) { //������ �ƴ϶� �����̹Ƿ� �ε���0���� Ž������
if (!visited[j]) { //num[j]�� �湮���� �ʾҴٸ�
visited[j] = true; //�湮 ǥ�ø� �Ͽ��ְ�
v.push_back(num[j]); //���Ϳ� �� ���� �־��ش�.
dfs(cnt + 1, j); //ī��Ʈ�� �÷��ְ� j���̵��� �ٽ� Ž������
v.pop_back(); //Ž���� �����ٸ� ���ͳ����� �� �ϳ��� ���ش�.
visited[j] = false; //�ٸ� ��츦���� j�� �湮���ߴٰ� �ٲ��ش�.
}
}
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> num_size >> picked_size;
for (int i = 0; i < num_size; i++) {
cin >> num[i];
}
sort(num, num + num_size); //�Է¹��� ���� ���׹����̹Ƿ� �������� ������ �����ش�.
dfs(0, 0);
}