Word ladder: Difference between revisions

m
C++ code made simpler and a bit faster
(→‎{{header|Haskell}}: added the simple and efficient solution)
m (C++ code made simpler and a bit faster)
Line 59:
}
return result;
}
 
// Return true if v contains e.
template <typename vector_type, typename element_type>
bool contains(const vector_type& v, const element_type& e) {
return std::find(v.begin(), v.end(), e) != v.end();
}
 
Line 79 ⟶ 73:
auto curr = queue.front();
queue.erase(queue.begin());
for (auto i = poss.begin(); i != poss.end());) {
std::vector<std::string> next;
for (const std::string& str :if poss(!one_away(*i, curr.back())) {
if (one_away(str, curr.back())) ++i;
next.push_back(str)continue;
}
if (contains(next, to) == *i) {
curr.push_back(to);
std::cout << join(curr.begin(), curr.end(), " -> ") << '\n';
return true;
}
poss.erase(std::remove_if(poss.begin(), poss.end(),
[&next](const std::string& str) {
return contains(next, str);
}),
poss.end());
for (const auto& str : next) {
std::vector<std::string> temp(curr);
temp.push_back(str*i);
queue.push_back(std::move(temp));
i = }poss.erase(i),;
}
}
1,777

edits