Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- XR Interaction Toolkit
- 다익스트라
- 백트래킹
- BFS
- 정렬
- 알고리즘
- Team Fortress 2
- 우선순위 큐
- 백준
- c++
- 트리
- 시뮬레이션
- VR
- DFS
- 재귀
- 투 포인터
- 그래프
- 자료구조
- 브루트포스
- 다이나믹 프로그래밍
- 유니티
- 누적 합
- 스택
- Unreal Engine 5
- 유니온 파인드
- 문자열
- 수학
- ue5
- 그리디 알고리즘
- 구현
Archives
- Today
- Total
1일1알
백준 2661번 좋은수열 C++ 본문
백트래킹을 진행할때 1,2,3 순서대로 채워주면서 채운다음에 나쁜 수열인지 검사한다. 나쁜 수열이면 걸러주고 좋은 수열만 계속 백트래킹을 진행시키다가 n만큼 채워지면 그게 제일 작은 좋은 수열이다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
char chs[3] = { '1','2','3' };
int n;
void BT(string str) {
int line = str.length() - 2;
int len = 1;
bool isGood = true;
for (int i = line; i >= 0; i--) {
int leftStart = i - len + 1;
int leftEnd = i;
int rightStart = i + 1;
int rightEnd = i + len;
if (leftStart < 0) break;
string left = "";
string right = "";
for (int j = leftStart; j <= leftEnd; j++) {
left += str[j];
}
for (int j = rightStart; j <= rightEnd; j++) {
right += str[j];
}
if (left == right) {
isGood = false;
break;
}
len++;
}
if (!isGood) return;
if (str.length() >= n) {
cout << str;
exit(0);
}
for (int i = 0; i < 3; i++) {
BT(str + chs[i]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
BT("");
}
'알고리즘' 카테고리의 다른 글
백준 2665번 미로만들기 C++ (0) | 2022.08.26 |
---|---|
백준 16637번 괄호 추가하기 C++ (0) | 2022.08.23 |
백준 4179번 불! C++ (0) | 2022.08.21 |
백준 17471번 게리맨더링 C++ (0) | 2022.08.20 |
백준 2458번 키 순서 C++ (0) | 2022.08.19 |