-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsolution.cpp
More file actions
46 lines (40 loc) · 1.14 KB
/
Copy pathsolution.cpp
File metadata and controls
46 lines (40 loc) · 1.14 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
class Solution
{
public:
unordered_map<string, unordered_set<char>> rules;
unordered_set<string> bad; // memo for failed rows
bool dfs(const string &row, int idx, string &next)
{
// If pyramid completed
if (row.size() == 1)
return true;
// If next row is fully built, move up
if (idx == row.size() - 1)
{
if (bad.count(next))
return false;
bool ok = dfs(next, 0, *(new string()));
if (!ok)
bad.insert(next);
return ok;
}
string key = row.substr(idx, 2);
if (!rules.count(key))
return false;
for (char c : rules[key])
{
next.push_back(c);
if (dfs(row, idx + 1, next))
return true;
next.pop_back(); // backtrack
}
return false;
}
bool pyramidTransition(string bottom, vector<string> &allowed)
{
for (auto &s : allowed)
rules[s.substr(0, 2)].insert(s[2]);
string next;
return dfs(bottom, 0, next);
}
};