@@ -46,44 +46,54 @@ \subsubsection{代码}
46
46
public:
47
47
int ladderLength(const string& start, const string &end,
48
48
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
+
52
52
int level = 0; // 层次
53
53
bool found = false;
54
54
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
+
55
78
current.push(start);
56
79
while (!current.empty() && !found) {
57
80
++level;
58
81
while (!current.empty() && !found) {
59
82
const string str = current.front();
60
83
current.pop();
61
84
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;
81
91
}
82
92
}
83
93
}
84
- swap(next, current); //!!! 交换两个队列
94
+ swap(next, current);
85
95
}
86
- if (found) return level+ 1;
96
+ if (found) return level + 1;
87
97
else return 0;
88
98
}
89
99
};
@@ -150,30 +160,40 @@ \subsubsection{代码}
150
160
151
161
bool found = false;
152
162
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
+
153
185
current.insert(start);
154
186
while (!current.empty() && !found) {
155
187
// 先将本层全部置为已访问,防止同层之间互相指向
156
- for (auto word : current)
188
+ for (const auto& word : current)
157
189
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); // 移动到最上面了
177
197
}
178
198
797B
}
179
199
@@ -196,7 +216,7 @@ \subsubsection{代码}
196
216
result.push_back(path);
197
217
reverse(result.back().begin(), result.back().end());
198
218
} else {
199
- for (auto f : father[word]) {
219
+ for (const auto& f : father[word]) {
200
220
gen_path(father, path, start, f, result);
201
221
}
202
222
}
0 commit comments