-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathmain.cpp
118 lines (102 loc) · 2.6 KB
/
main.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <string>
#include <vector>
#include <tuple>
#include <cassert>
using std::cin;
using std::cout;
using std::endl;
using std::flush;
using std::string;
using std::to_string;
using std::vector;
using std::tuple;
using std::make_tuple;
using std::get;
//
// Returns a {bulls, cows} result from guessing n.
//
tuple<int, int> guess(const string& n) {
int bulls, cows;
cout << n << endl << flush;
cin >> bulls >> cows;
return make_tuple(bulls, cows);
}
//
// Returns the set of at most 4 unique digits that make up the number we are
// trying to determine in at most 10 calls to guess(...).
//
vector<int> get_digits() {
vector<int> digits;
for (int digit = 0; digit < 10; digit++) {
string query(4, '0' + digit);
auto result = guess(query);
int bulls = get<0>(result);
int cows = get<1>(result);
if (bulls + cows > 0) {
digits.push_back(digit);
}
}
return digits;
}
//
// Returns all combinations of counts that n unique digits can have to form a
// 4 digit number.
//
vector<vector<int>> get_counts(int n) {
assert(n > 0);
assert(n < 5);
switch (n) {
case 1: return {{4}};
case 2: return {{2, 2}, {1, 3}, {3, 1}};
case 3: return {{1, 1, 2}, {1, 2, 1}, {2, 1, 1}};
case 4: return {{1, 1, 1, 1}};
}
return {};
}
//
// Guesses all permutations of the given digits and their counts until a valid
// arrangement is found or all permutations are exhausted. If n is the number
// of unique digits, we guarantee that this will take no more than 40 guesses.
//
// Case n = 1 -> 4! = 24 permutations
//
// Case n = 2 -> (4 * 3) / 2 + 2 * 4 = 14 permutations
//
// Case n = 3 -> 3 * (4 * 3) = 36 permutations
//
// Case n = 4 -> 1 permutation
//
// These permutations are derived from the possible count distributions of n
// unique digits forming a 4 digit number defined in the get_counts(...)
// function above.
//
bool dfs(const vector<int>& digits, vector<int>& counts, string n) { // NOLINT
// Check if the 4 digit number we have built up is valid.
if (n.size() == 4) {
auto result = guess(n);
int bulls = get<0>(result);
return bulls == 4;
}
// Append every digit with count > 0 to the current string and DFS.
for (size_t i = 0; i < digits.size(); i++) {
if (counts[i] > 0) {
counts[i]--;
bool found = dfs(digits, counts, n + to_string(digits[i]));
counts[i]++;
if (found) {
return true;
}
}
}
return false;
}
int main() {
auto digits = get_digits();
for (auto counts : get_counts(digits.size())) {
if (dfs(digits, counts, "")) {
break;
}
}
return 0;
}