-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP127.cpp
86 lines (80 loc) · 2.35 KB
/
P127.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
#include "header.h"
#include <array>
#include <bits/c++config.h>
#include <cstddef>
#include <cstring>
#include <vector>
#define LEN 5001
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
array<bool, LEN> end;
array<bool, LEN> visited;
visited.fill(false);
bool begin;
bool hasEnd = false;
end.fill(false);
queue<pair<int, int>> q;
for (size_t i=0; i<wordList.size(); i++) {
if (transfer(beginWord, wordList[i])) begin = true;
else begin=false;
if (transfer(endWord, wordList[i])) end[i] = true;
if (endWord == wordList[i]) hasEnd = true;
if (begin) {
q.emplace(i,2);
visited[i] = true;
}
}
if (!hasEnd) return 0;
if (transfer(beginWord, endWord)) return 2;
const size_t &n = wordList.size();
static int map[LEN][LEN];
for (size_t i=0; i<n; i++)
map[i][0] = 0;
for (size_t i=0; i<n; i++) {
for (size_t j=i+1; j<n; j++) {
if (transfer(wordList[i], wordList[j])) {
map[i][++map[i][0]] = j;
map[j][++map[j][0]] = i;
}
}
}
while (!q.empty()) {
int _last = q.front().first;
int _step = q.front().second;
if (end[_last]) {
return _step+1;
}
for (int i=1; i<=map[_last][0]; i++) {
int j = map[_last][i];
if (!visited[j]) {
q.emplace(j, _step+1);
visited[j] = true;
}
}
q.pop();
}
return 0;
}
private:
bool transfer(const string &a,const string &b) {
int differ = 0;
if (a.size()!=b.size()) return false;
for (size_t i=0; i<a.size(); i++)
if (a[i]!=b[i]) {
differ++;
if (differ>1) return false;
}
if (differ==1)
return true;
else
return false;
}
};
int main() {
string beginWord = "a";
string endWord = "c";
vector<string> wordList = {"a", "b", "c"};
cout << Solution().ladderLength(beginWord, endWord, wordList) << endl;
return 0;
}