-
Notifications
You must be signed in to change notification settings - Fork 0
/
200209-1.cpp
61 lines (58 loc) · 1.52 KB
/
200209-1.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
// https://leetcode-cn.com/problems/word-ladder/
#include <cstdio>
#include <string>
#include <vector>
#include <unordered_set>
using namespace std;
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
vector<string> w = wordList; w.push_back(beginWord);
int m = beginWord.size();
int n = w.size();
vector<vector<bool>> a(n); for (int i = 0; i < n; ++i) a[i].resize(n);
int start = -1, end = -1;
for (int i = 0; i < n; ++i) {
if (start < 0 && beginWord == w[i]) start = i;
if (end < 0 && endWord == w[i]) end = i;
for (int j = i + 1; j < n; ++j) {
int d = 0; for (int k = 0; k < m; ++k) d += (w[i][k] != w[j][k]);
a[i][j] = a[j][i] = (d == 1);
}
}
if (end < 0) return 0;
vector<int> v(n);
v[start] = 1;
unordered_set<int> curr{start};
while (!curr.empty() && curr.find(end) == curr.end()) {
unordered_set<int> next;
for (auto i : curr) {
for (int j = 0; j < n; ++j) {
if (a[i][j] && v[j] == 0) {
next.insert(j);
v[j] = v[i] + 1;
}
}
}
curr = next;
}
return v[end];
}
};
int main()
{
Solution s;
{
string beginWord = "hit";
string endWord = "cog";
vector<string> wordList = {"hot","dot","dog","lot","log","cog"};
printf("%d\n", s.ladderLength(beginWord, endWord, wordList)); // answer: 5
}
{
string beginWord = "hit";
string endWord = "cog";
vector<string> wordList = {"hot","dot","dog","lot","log"};
printf("%d\n", s.ladderLength(beginWord, endWord, wordList)); // answer: 0
}
return 0;
}