Skip to content

Commit

Permalink
2024-04-08 n๊ณผm(5)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhlee777 committed Apr 8, 2024
1 parent 66184fa commit ba48c49
Showing 1 changed file with 36 additions and 0 deletions.
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);
}

0 comments on commit ba48c49

Please sign in to comment.