알고리즘 문제/BOJ
16943번 숫자 재배치
parkit
2019. 4. 16. 22:51
728x90
반응형
https://www.acmicpc.net/problem/16943
기본적인 백트래킹 문제이다.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> #include <set> #include <sstream> #include <tuple> #pragma warning(disable:4996) #pragma comment(linker, "/STACK:336777216") using namespace std; string A, B; vector<char> v; int MAX = -1, b = 0; bool use[12] = { false, }; void simulation(string s) { if (s.length() == A.length()) { if (s.at(0) == '0') return; int a = atoi(s.c_str()); if (a <= b) MAX = max(MAX, a); return; } for (int i = 0; i < v.size(); i++) if (!use[i]) { s += v.at(i); use[i] = true; simulation(s); s = s.substr(0, s.length() - 1); use[i] = false; } } int main(void) { MAX = -1; cin >> A >> B; for (int i = 0; i < A.length(); i++) v.push_back(A.at(i)); b = atoi(B.c_str()); simulation(""); printf("%d\n", MAX); return 0; } | cs |
728x90
반응형