Algorithm(218)
-
[BOJ] 15651번 : N과 M (3)
1. problem : https://www.acmicpc.net/problem/15651 2. solution 1:#include using namespace std;int N, M;int ans[8]; void backTrack(int k) { if (k == M) { for (int i = 0; i > N >> M; backTrack(0);}
2024.08.18 -
[BOJ] 15650번 : N과 M (2)
1. problem :https://www.acmicpc.net/problem/15649 2. solution 1 :#include using namespace std;int N, M; int ans[8];int isUsed[8] = { 0 };int temp = -1; // k = 0일때 비교를 위해 설정;void backtrack(int k,int temp) { if (k == M) { for (int i = 0; i temp) { isUsed[i] = 1; ans[k] = i + 1; backtrack(k + 1,i+1); isUsed[i] = 0; } }}int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> N >> ..
2024.08.18 -
[C++] next_permutation
꿀팁을 배웠다. 순열과 조합을 사용할 때, C++에서는 next_permutation을 사용하면 된다고 한다. 1. 순열을 이용 (예시) 1,2,3 원소 3개를 순서대로 나열하는 법 #include using namespace std;int main(void) { int arr[5] = { 1,2,3 }; do { for (int i = 0; i do while문과 next_permutation을 이용해서 순열을 표현한다. 배열이 오름차순 정렬되어있어야 한다. 2. 조합 (5개의 원소 중에 3개를 뽑는 방법)#include using namespace std;int main(void) { int arr[5] = { 0,0,0,1,1 }; do { for (int i = 0; i 0의 개수는 뽑고자 ..
2024.08.17 -
[BOJ] 9663번 : N-Queen
1. problem : https://www.acmicpc.net/problem/9663 2. solution 1 :#include using namespace std;int N;int isUsed1[14] = { 0 }; // 같은 열 int isUsed2[27] = { 0 }; // 우상향 대각선 int isUsed3[27] = { 0 }; // 우하향 대각선int ans = 0;void queenBatch(int k) { if (k == N) { ans++; return; } for (int col = 0; col > N; queenBatch(0); cout source code 출처 : https://github.com/encrypted-def/basic-algo-lecture/blob/m..
2024.08.17 -
[BOJ] 15649번 : N과 M (1)
1. problem : https://www.acmicpc.net/status?user_id=rudgh99&problem_id=15649&from_mine=1 2. solution 1 :#include using namespace std;int N, M;int nums[10];int isUsed[10];int ans[10];void solve(int k) { if (k == M) { for (int i = 0; i > N >> M; // 값 초기화 for (int i = 0; i backtracking이다. 외우자. 외우자. template이다 생각하고 외우자.source code 출처 : https://github.com/encrypted-def/basic-algo-lecture/blob/maste..
2024.08.17 -
[BOJ] 1182번 : 부분수열의 합
1. problem : https://www.acmicpc.net/problem/1182 2. solution 1 :#include using namespace std;int N, S;int numbers[20];int ans = 0;void findSubsequence(int index, int currentSum) { if (index == N) { if (currentSum == S) { ans++; } return; } findSubsequence(index + 1, currentSum + numbers[index]); findSubsequence(index + 1, currentSum);}int main(void) { ios::sync_with_stdio(0); cin.tie(0); c..
2024.08.17