-
Notifications
You must be signed in to change notification settings - Fork 0
/
words_path.cpp
89 lines (80 loc) · 1.89 KB
/
words_path.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
#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
using namespace std;
set<string> load_dict(const char *path, const int length)
{
set<string> words;
ifstream file;
string word;
file.open(path);
while (file >> word) {
if (word.length() == length) {
transform(word.begin(), word.end(), word.begin(), ::tolower);
words.insert(word);
}
}
return words;
}
vector<string> get_siblings(const string &from, set<string> &words)
{
vector<string> siblings;
for (int i = 0; i < from.length(); i++) {
string alter = from;
for (char c = 'a'; c <= 'z'; c++) {
alter[i] = c;
if (c != from[i] && words.erase(alter) >= 1) {
siblings.push_back(alter);
}
}
}
return siblings;
}
vector<string> bfs(const string &from, const string &to, set<string> &words)
{
map<string, string> parent;
queue<string> queue;
queue.push(from);
while (queue.size() > 0 && parent.count(to) == 0) {
string node = queue.front();
vector<string> siblings = get_siblings(node, words);
queue.pop();
for(int i = 0; i < siblings.size(); i++) {
parent[siblings[i]] = node;
queue.push(siblings[i]);
}
}
vector<string> path;
string position = to;
path.push_back(position);
while (parent.count(position) > 0) {
path.insert(path.begin(), parent[position]);
position = parent[position];
}
return path;
}
int main(int ac, char **av)
{
if (ac < 3) {
cerr << "Usage: ./words_path jina pray" << endl;
return 1;
}
string from = av[1];
string to = av[2];
set<string> words = load_dict("/usr/share/dict/words", from.length());
vector<string> path = bfs(from, to, words);
for(int i = 0; i < path.size(); i++) {
if (i > 0) {
cout << " -> ";
}
cout << path[i];
}
cout << endl;
return 0;
}