Biggest Prize Number
Back Trackingunknown
c_cpp
8 months ago
1.6 kB
24
Indexable
#include <iostream> #include <cstring> #include <algorithm> using namespace std; int n, k; char a[10]; int price; bool visited[1000000][11]; int toInt(char *x) { int num = 0; for (int i = 0; x[i] != '\0'; i++) { num = num * 10 + (x[i] - '0'); } return num; } void solve(int exchange) { if (exchange == 0) { int currentPrice = toInt(a); if (currentPrice > price) { price = currentPrice; } return; } int currentState = toInt(a); if (visited[currentState][exchange]) { return; } visited[currentState][exchange] = true; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] != a[j]) { swap(a[i], a[j]); solve(exchange - 1); swap(a[i], a[j]); } } } } int main() { freopen("input.txt", "r", stdin); int t; cin >> t; for (int tc = 1; tc <= t; tc++) { cin >> a; cin >> k; n = strlen(a); price = 0; memset(visited, false, sizeof(visited)); solve(k); cout << "Case #" << tc << endl << price << endl; } return 0; } /* 52 123 1 2737 1 772 8 0473 7 441296 9 5525 2 114 2 6575 3 7321 10 1138 4 53179 9 975054 4 0128 9 50665 7 937 2 55960 5 564 9 50308 8 57308 6 215 4 377088 3 340 1 1136 6 37 2 58 8 1375 8 655673 10 29 4 153 3 583185 8 812 7 361 9 394 5 60153 1 65433 4 267825 10 969 9 027147 10 08343 8 57568 1 805683 8 83 8 97 9 7734 5 5005 7 216926 7 3560 7 50 1 679 9 16813 3 1415 8 751 10 */
Editor is loading...
Leave a Comment