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 | 29 | 30 |
Tags
- ue5
- 수학
- BFS
- c++
- 스택
- 문자열
- 트리
- 구현
- 우선순위 큐
- 유니티
- 다익스트라
- 백준
- 시뮬레이션
- XR Interaction Toolkit
- 누적 합
- 알고리즘
- 브루트포스
- 유니온 파인드
- 정렬
- Unreal Engine 5
- 재귀
- Team Fortress 2
- 백트래킹
- 다이나믹 프로그래밍
- 자료구조
- 그리디 알고리즘
- DFS
- 투 포인터
- VR
- 그래프
Archives
- Today
- Total
1일1알
백준 16943번 숫자 재배치 C++ 본문

a에 있는 숫자들을 백트래킹을 이용하여 새로운 숫자를 만들고, b보다 작은 값들 중 가장 큰 값을 구했다.
처음 시작이 0인 숫자는 제외하였다.
#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 <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
string a;
int b;
int ans = -1;
vector<int> nums;
vector<bool> visited(11, false);
void BT(int len, int num) {
if (len >= nums.size()) {
if (num < b) {
ans = max(ans, num);
}
return;
}
for (int i = 0; i < nums.size(); i++) {
if (len == 0 && nums[i] == 0) continue;
if (visited[i]) continue;
visited[i] = true;
BT(len + 1, num * 10 + nums[i]);
visited[i] = false;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> a >> b;
for (int i = 0; i < a.length(); i++) {
int num = a[i] - '0';
nums.push_back(num);
}
BT(0, 0);
cout << ans;
};
'알고리즘' 카테고리의 다른 글
백준 16958번 텔레포트 C++ (0) | 2022.01.19 |
---|---|
백준 16945번 매직 스퀘어로 변경하기 C++ (0) | 2022.01.18 |
백준 16922번 로마 숫자 만들기 C++ (0) | 2022.01.16 |
백준 2210번 숫자판 점프 C++ (0) | 2022.01.16 |
백준 1068번 트리 C++ (0) | 2022.01.15 |