@@ -46,44 +46,54 @@ \subsubsection{代码}
4646public:
4747 int ladderLength(const string& start, const string &end,
4848 const unordered_set<string> &dict) {
49- queue<string> current, next; // 当前层,下一层
50- unordered_set<string> visited; // 判重
51- unordered_map<string, string > father;
49+ queue<string> current, next; // 当前层,下一层
50+ unordered_set<string> visited; // 判重
51+
5252 int level = 0; // 层次
5353 bool found = false;
5454
55+ auto state_is_target = [&](const string &s) {return s == end;};
56+ auto state_extend = [&](const string &s) {
57+ vector<string> result;
58+
59+ for (size_t i = 0; i < s.size(); ++i) {
60+ string new_word(s);
61+ for (char c = 'a' ; c <= 'z' ; c++) {
62+ if (c == new_word[i]) continue;
63+
64+ swap(c, new_word[i]);
65+
66+ if (dict.count(new_word) > 0 &&
67+ !visited.count(new_word)) {
68+ result.push_back(new_word);
69+ visited.insert(new_word);
70+ }
71+ swap(c, new_word[i]); // 恢复该单词
72+ }
73+ }
74+
75+ return result;
76+ };
77+
5578 current.push(start);
5679 while (!current.empty() && !found) {
5780 ++level;
5881 while (!current.empty() && !found) {
5982 const string str = current.front();
6083 current.pop();
6184
62- for (size_t i = 0; i < str.size(); ++i) {
63- string new_word(str);
64- for (char c = 'a' ; c <= 'z' ; c++) {
65- if (c == new_word[i]) continue;
66-
67- swap(c, new_word[i]);
68- if (new_word == end) {
69- found = true; //找到了
70- father[new_word] = str;
71- break;
72- }
73-
74- if (dict.count(new_word) > 0
75- && !visited.count(new_word)) {
76- next.push(new_word);
77- visited.insert(new_word);
78- father[new_word] = str;
79- }
80- swap(c, new_word[i]); // 恢复该单词
85+ const auto& new_states = state_extend(str);
86+ for (const auto& state : new_states) {
87+ next.push(state);
88+ if (state_is_target(state)) {
89+ found = true; //找到了
90+ break;
8191 }
8292 }
8393 }
84- swap(next, current); //!!! 交换两个队列
94+ swap(next, current);
8595 }
86- if (found) return level+ 1;
96+ if (found) return level + 1;
8797 else return 0;
8898 }
8999};
@@ -150,30 +160,40 @@ \subsubsection{代码}
150160
151161 bool found = false;
152162
163+ auto state_is_target = [&](const string &s) {return s == end;};
164+ auto state_extend = [&](const string &s) {
165+ unordered_set<string> result;
166+
167+ for (size_t i = 0; i < s.size(); ++i) {
168+ string new_word(s);
169+ for (char c = 'a' ; c <= 'z' ; c++) {
170+ if (c == new_word[i]) continue;
171+
172+ swap(c, new_word[i]);
173+
174+ if ((dict.count(new_word) > 0|| new_word == end) &&
175+ !visited.count(new_word)) {
176+ result.insert(new_word);
177+ }
178+ swap(c, new_word[i]); // 恢复该单词
179+ }
180+ }
181+
182+ return result;
183+ };
184+
153185 current.insert(start);
154186 while (!current.empty() && !found) {
155187 // 先将本层全部置为已访问,防止同层之间互相指向
156- for (auto word : current)
188+ for (const auto& word : current)
157189 visited.insert(word);
158- for (auto word : current) {
159- for (size_t i = 0; i < word.size(); ++i) {
160- string new_word = word;
161- for (char c = 'a' ; c <= 'z' ; ++c) {
162- if (c == new_word[i]) continue;
163- swap(c, new_word[i]);
164-
165- if (new_word == end) found = true; //找到了
166-
167- if (visited.count(new_word) == 0
168- && (dict.count(new_word) > 0 ||
169- new_word == end)) {
170- next.insert(new_word);
171- father[new_word].push_back(word);
172- // visited.insert(new_word); // 移动到最上面了
173- }
174-
175- swap(c, new_word[i]); // restore
176- }
190+ for (const auto& word : current) {
191+ const auto new_states = state_extend(word);
192+ for (const auto &state : new_states) {
193+ if (state_is_target(state)) found = true;
194+ next.insert(state);
195+ father[state].push_back(word);
196+ // visited.insert(state); // 移动到最上面了
177197 }
178198 }
179199
@@ -196,7 +216,7 @@ \subsubsection{代码}
196216 result.push_back(path);
197217 reverse(result.back().begin(), result.back().end());
198218 } else {
199- for (auto f : father[word]) {
219+ for (const auto& f : father[word]) {
200220 gen_path(father, path, start, f, result);
201221 }
202222 }
0 commit comments