-
Notifications
You must be signed in to change notification settings - Fork 2
/
strategy.hpp
86 lines (82 loc) · 2.74 KB
/
strategy.hpp
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
#pragma once
#include <map>
#include <fstream>
#include "common.hpp"
struct StrategyNode {
bool terminal;
std::string query_word;
std::vector<std::pair<response_t, size_t>> children;
};
struct StrategyTree {
std::vector<StrategyNode> nodes;
StrategyTree(GuessNode *root) {
assert(root->depth == 0);
std::map<GuessNode*, size_t> node_table;
dfs_register(root, node_table);
nodes.resize(node_table.size());
dfs(root, node_table);
}
StrategyTree(const std::string &filename) {
std::ifstream f{filename};
size_t total;
f >> total;
nodes.resize(total);
for (size_t i = 0; i < total; ++i) {
f >> nodes[i].query_word;
if (nodes[i].query_word == "terminal") {
nodes[i].terminal = true;
nodes[i].query_word.clear();
} else {
assert(nodes[i].query_word.length() == LEN);
nodes[i].terminal = false;
size_t children_cnt;
f >> children_cnt;
nodes[i].children.reserve(children_cnt);
for (size_t j = 0; j < children_cnt; ++j) {
std::pair<response_t, size_t> cur;
f >> cur.first >> cur.second;
nodes[i].children.push_back(cur);
}
}
}
}
void dump(const std::string &filename) {
std::ofstream f{filename};
f << nodes.size() << '\n';
for (auto &i : nodes) {
if (i.terminal) {
f << "terminal\n";
} else {
f << i.query_word << ' ' << i.children.size() << ' ';
for (auto &j : i.children) {
f << j.first << ' ' << j.second << ' ';
}
f << '\n';
}
}
}
private:
void dfs_register(GuessNode *node, std::map<GuessNode*, size_t> &node_table) {
size_t cur = node_table.size();
node_table[node] = cur;
if (node->child) {
for (auto &i : node->child->children) {
dfs_register(i.second.get(), node_table);
}
}
}
void dfs(GuessNode *node, std::map<GuessNode*, size_t> &node_table) {
size_t cur = node_table[node];
if (node->child) {
nodes[cur].terminal = false;
nodes[cur].query_word = query_words[node->best_query];
nodes[cur].children.reserve(node->child->children.size());
for (auto &i : node->child->children) {
nodes[cur].children.emplace_back(i.first, node_table[i.second.get()]);
dfs(i.second.get(), node_table);
}
} else {
nodes[cur].terminal = true;
}
}
};