-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpletools.cpp
More file actions
171 lines (157 loc) · 5.01 KB
/
Copy pathsimpletools.cpp
File metadata and controls
171 lines (157 loc) · 5.01 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "simpletools.h"
#include <iostream>
std::vector <int> next_string(const std::vector <int> &string, std::size_t n, std::size_t m) {
std::vector <int> new_string = string;
std::vector <bool> used(m, false);
for (std::size_t i = 0; i < n; i++) {
used[string[i]] = true;
}
int mx_empty = -1;
for (int i = (int)m - 1; i >= 0; i--) {
if (!used[i]) {
mx_empty = i;
break;
}
}
int pref = -1;
for (int i = (int)n - 1; i >= 0; i--) {
if (mx_empty < string[i]) {
mx_empty = string[i];
used[string[i]] = false;
continue;
}
pref = i;
break;
}
if (pref != -1) {
std::size_t pr = string[pref];
for (std::size_t i = pr + 1; i < m; i++) {
if (!used[i]) {
used[pr] = false;
used[i] = true;
new_string[pref] = i;
break;
}
}
}
for (int i = pref + 1, j = 0; i < (int)n && j < (int)m; j++) {
if (!used[j]) {
new_string[i++] = j;
}
}
//print_string(new_string);
return new_string;
}
bool query_satisfaction(const std::vector <int> &string,
const std::pair <std::vector <int>, Response> &query,
std::size_t n, std::size_t m) {
std::vector <bool> used(m, false);
int cows = 0, bulls = 0;
for (std::size_t i = 0; i < n; i++) {
used[query.first[i]] = true;
}
for (std::size_t i = 0; i < n; i++) {
cows += used[string[i]];
bulls += (string[i] == query.first[i]);
}
cows -= bulls;
return cows == query.second.get_cows() && bulls == query.second.get_bulls();
}
bool queries_satisfaction(const std::vector <int> &string,
const std::vector <std::pair <std::vector <int>, Response>> &queries,
std::size_t n, std::size_t m) {
for (std::size_t i = 0; i < queries.size(); i++) {
if (!query_satisfaction(string, queries[i], n, m)) {
return false;
}
}
return true;
}
void print_string(const std::vector <int> &string) {
for (std::size_t i = 0; i < string.size(); i++) {
std::cout << string[i] << ' ';
}
std::cout << '\n';
}
bool check_is_first(const std::vector <int> &string) {
for (int i = 0; i < (int)string.size(); i++) {
if (i != string[i]) {
return true;
}
}
return false;
}
std::vector <int> initial_string(std::size_t n) {
std::vector <int> string(n);
for (std::size_t i = 0; i < n; i++) {
string[i] = i;
}
return string;
}
std::vector <std::vector <int>> initial_S(std::size_t n, std::size_t m) {
std::vector <int> string;
std::vector <std::vector<int>> S;
string = initial_string(n);
do {
S.push_back(string);
string = next_string(string, n, m);
} while(check_is_first(string));
return S;
}
std::vector <int> get_N(const std::vector <int> &query_string,
std::vector <std::pair <std::vector <int>, Response>> T,
std::size_t n, std::size_t m) {
std::vector <std::vector <int>> possible_strings;
std::vector <int> string(n);
std::vector <int> N;
for (std::size_t i = 0; i < n; i++) {
string[i] = i;
}
do {
if (queries_satisfaction(string, T, n, m)) {
possible_strings.push_back(string);
}
string = next_string(string, n, m);
} while(check_is_first(string));
for (std::size_t i = 0; i <= n; i++) {
for (std::size_t j = 0; i + j <= n; j++) {
N.push_back(0);
std::pair <std::vector <int>, Response> query = {query_string, Response(i, j)};
for (std::size_t z = 0; z < possible_strings.size(); z++) {
if (query_satisfaction(possible_strings[z], query, n, m)) {
N.back()++;
}
}
}
}
return N;
}
std::vector <std::vector <std::vector <int>>> get_new_S(const std::vector <int> &query_string,
const std::vector <std::vector <int>> &S,
std::vector <std::pair <std::vector <int>, Response>> T,
std::size_t n, std::size_t m) {
std::vector <int> string(n);
std::vector<std::vector<std::vector<int>>> N;
for (std::size_t i = 0; i < n; i++) {
string[i] = i;
}
for (std::size_t i = 0; i <= n; i++) {
for (std::size_t j = 0; i + j <= n; j++) {
N.push_back(std::vector <std::vector <int>> ());
std::pair <std::vector <int>, Response> query = {query_string, Response(i, j)};
for (std::size_t z = 0; z < S.size(); z++) {
if (query_satisfaction(S[z], query, n, m)) {
N.back().push_back(S[z]);
}
}
}
}
return N;
}
int A(int n, int m) {
int result = 1;
for (int i = m - n + 1; i <= m; i++) {
result *= i;
}
return result;
}