알고리즘 문제풀이/백준
[백준/BOJ] N과 M (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11)
노력의천재
2022. 9. 22. 21:50
N과 M (1)
https://www.acmicpc.net/problem/15649
#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <set>
#include <sstream>
#include <climits>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#define lp1(i, x, n) for(int i = x; i <= n; i++)
using namespace std;
typedef long long ll;
int t, n, m, h, k, r, c, sz, sx, sy, x, y, z, answer = 2147000000, L, R, sum, a, b, f, s, g, u, d, cnt = 1;
int visited[10], selected[10];
void DFS(int now, int cnt) {
if(cnt == m) {
for(int i = 0; i < cnt; i++) {
cout << selected[i] << " ";
}
cout << "\n";
return;
}
for(int i = 1; i <= n; i++) {
if(visited[i] == 0) {
visited[i] = 1;
selected[cnt] = i;
DFS(i, cnt + 1);
visited[i] = 0;
}
}
}
int main() {
//freopen("input.txt", "r", stdin);
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> m;
DFS(1, 0);
}
N과 M (2)
https://www.acmicpc.net/problem/15650
#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <set>
#include <sstream>
#include <climits>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#define lp1(i, x, n) for(int i = x; i <= n; i++)
using namespace std;
typedef long long ll;
int t, n, m, h, k, r, c, sz, sx, sy, x, y, z, answer = 2147000000, L, R, sum, a, b, f, s, g, u, d, cnt = 1;
int visited[10], selected[10];
void DFS(int now, int cnt) {
if(cnt == m) {
for(int i = 0; i < cnt; i++) {
cout << selected[i] << " ";
}
cout << "\n";
return;
}
for(int i = now; i <= n; i++) {
if(visited[i] == 0) {
visited[i] = 1;
selected[cnt] = i;
DFS(i, cnt + 1);
visited[i] = 0;
}
}
}
int main() {
//freopen("input.txt", "r", stdin);
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> m;
DFS(1, 0);
}
N과 M (3)
https://www.acmicpc.net/problem/15651
#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <set>
#include <sstream>
#include <climits>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#define lp1(i, x, n) for(int i = x; i <= n; i++)
using namespace std;
typedef long long ll;
int t, n, m, h, k, r, c, sz, sx, sy, x, y, z, answer = 2147000000, L, R, sum, a, b, f, s, g, u, d, cnt = 1;
int visited[10], selected[10];
void DFS(int now, int cnt) {
if(cnt == m) {
for(int i = 0; i < cnt; i++) {
cout << selected[i] << " ";
}
cout << "\n";
return;
}
for(int i = 1; i <= n; i++) {
if(visited[i] >= 0) {
visited[i]++;
selected[cnt] = i;
DFS(i, cnt + 1);
visited[i]--;
}
}
}
int main() {
//freopen("input.txt", "r", stdin);
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> m;
DFS(1, 0);
}
N과 M (4)
https://www.acmicpc.net/problem/15652
#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <set>
#include <sstream>
#include <climits>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#define lp1(i, x, n) for(int i = x; i <= n; i++)
using namespace std;
typedef long long ll;
int t, n, m, h, k, r, c, sz, sx, sy, x, y, z, answer = 2147000000, L, R, sum, a, b, f, s, g, u, d, cnt = 1;
int visited[10], selected[10];
void DFS(int now, int cnt) {
if(cnt == m) {
for(int i = 0; i < cnt; i++) {
cout << selected[i] << " ";
}
cout << "\n";
return;
}
for(int i = now; i <= n; i++) {
if(visited[i] >= 0) {
visited[i]++;
selected[cnt] = i;
DFS(i, cnt + 1);
visited[i]--;
}
}
}
int main() {
//freopen("input.txt", "r", stdin);
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> m;
DFS(1, 0);
}
5 ~ 11 업데이트 예정 ...