-
Notifications
You must be signed in to change notification settings - Fork 1
/
126.py
141 lines (124 loc) · 4.09 KB
/
126.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import queue
class Solution:
def findLadders(self, beginWord, endWord, wordList):
if not beginWord in wordList:
wordList.append(beginWord)
if not endWord in wordList:
return []
n = len(wordList)
A = [[0] * n for i in range(n)]
for i in range(n):
A[i][i] = 1
def _diff_one(s1, s2):
count = 0
for c1, c2 in zip(s1, s2):
if c1 != c2:
count+=1
return count == 1
# 构图
for i in range(len(wordList)):
for j in range(i, len(wordList)):
if _diff_one(wordList[i], wordList[j]):
A[i][j] = 1
A[j][i] = 1
# -2 -> -1 BFS
pre = [[-1] for i in range(n)]
way_len = [100000 for i in range(n)]
flag = [True for i in range(n)]
begin_index = wordList.index(beginWord)
way_len[begin_index] = 0
q = queue.Queue()
q.put(begin_index)
flag[begin_index] = False
while not q.empty():
i = q.get()
flag[i] = False
for j in range(n):
if A[i][j] == 1 and flag[j]:
if way_len[j] > way_len[i] + 1:
way_len[j] = way_len[i] + 1
pre[j] = [i]
elif way_len[j] == way_len[i] + 1:
if not i in pre[j]:
pre[j].append(i)
q.put(j)
res = []
print('test')
k = wordList.index(endWord)
if way_len[k] == 100000:
return res
if pre[k] == [-1]:
return []
temp_res = []
def _dfs(lst, pos):
if pos == -1:
res.append(temp_res[::-1])
return
for node in lst:
temp_res.append(wordList[pos])
_dfs(pre[node], node)
temp_res.pop()
_dfs(pre[k], k)
return res
import queue
class Solution:
def findLadders(self, beginWord, endWord, wordList):
if not beginWord in wordList:
wordList.append(beginWord)
if not endWord in wordList:
return []
n = len(wordList)
A = [[0] * n for i in range(n)]
for i in range(n):
A[i][i] = 1
def _diff_one(s1, s2):
count = 0
for c1, c2 in zip(s1, s2):
if c1 != c2:
count+=1
return count == 1
# 构图
for i in range(len(wordList)):
for j in range(i, len(wordList)):
if _diff_one(wordList[i], wordList[j]):
A[i][j] = 1
A[j][i] = 1
# -2 -> -1 BFS
pre = [[-1] for i in range(n)]
way_len = [100000 for i in range(n)]
flag = [True for i in range(n)]
begin_index = wordList.index(beginWord)
way_len[begin_index] = 0
q = queue.Queue()
q.put(begin_index)
flag[begin_index] = False
while not q.empty():
i = q.get()
flag[i] = False
for j in range(n):
if A[i][j] == 1 and flag[j]:
if way_len[j] > way_len[i] + 1:
way_len[j] = way_len[i] + 1
pre[j] = [i]
elif way_len[j] == way_len[i] + 1:
if not i in pre[j]:
pre[j].append(i)
q.put(j)
res = []
print('test')
k = wordList.index(endWord)
if way_len[k] == 100000:
return res
if pre[k] == [-1]:
return []
temp_res = []
def _dfs(lst, pos):
if pos == -1:
res.append(temp_res[::-1])
return
for node in lst:
temp_res.append(wordList[pos])
_dfs(pre[node], node)
temp_res.pop()
_dfs(pre[k], k)
return res