Skip to content

Commit 5ccbaf4

Browse files
committed
126完成
1 parent 08bdc01 commit 5ccbaf4

File tree

2 files changed

+246
-0
lines changed

2 files changed

+246
-0
lines changed

python/126.py

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import queue
2+
class Solution:
3+
def findLadders(self, beginWord, endWord, wordList):
4+
if not beginWord in wordList:
5+
wordList.append(beginWord)
6+
if not endWord in wordList:
7+
return []
8+
n = len(wordList)
9+
A = [[0] * n for i in range(n)]
10+
for i in range(n):
11+
A[i][i] = 1
12+
13+
def _diff_one(s1, s2):
14+
count = 0
15+
for c1, c2 in zip(s1, s2):
16+
if c1 != c2:
17+
count+=1
18+
return count == 1
19+
20+
# 构图
21+
for i in range(len(wordList)):
22+
for j in range(i, len(wordList)):
23+
if _diff_one(wordList[i], wordList[j]):
24+
A[i][j] = 1
25+
A[j][i] = 1
26+
27+
# -2 -> -1 BFS
28+
pre = [[-1] for i in range(n)]
29+
way_len = [100000 for i in range(n)]
30+
flag = [True for i in range(n)]
31+
begin_index = wordList.index(beginWord)
32+
way_len[begin_index] = 0
33+
q = queue.Queue()
34+
q.put(begin_index)
35+
flag[begin_index] = False
36+
while not q.empty():
37+
i = q.get()
38+
flag[i] = False
39+
for j in range(n):
40+
if A[i][j] == 1 and flag[j]:
41+
if way_len[j] > way_len[i] + 1:
42+
way_len[j] = way_len[i] + 1
43+
pre[j] = [i]
44+
elif way_len[j] == way_len[i] + 1:
45+
if not i in pre[j]:
46+
pre[j].append(i)
47+
q.put(j)
48+
res = []
49+
print('test')
50+
k = wordList.index(endWord)
51+
if way_len[k] == 100000:
52+
return res
53+
54+
if pre[k] == [-1]:
55+
return []
56+
57+
temp_res = []
58+
def _dfs(lst, pos):
59+
60+
if pos == -1:
61+
res.append(temp_res[::-1])
62+
return
63+
for node in lst:
64+
temp_res.append(wordList[pos])
65+
_dfs(pre[node], node)
66+
temp_res.pop()
67+
68+
69+
_dfs(pre[k], k)
70+
return res
71+
72+
import queue
73+
class Solution:
74+
def findLadders(self, beginWord, endWord, wordList):
75+
if not beginWord in wordList:
76+
wordList.append(beginWord)
77+
if not endWord in wordList:
78+
return []
79+
n = len(wordList)
80+
A = [[0] * n for i in range(n)]
81+
for i in range(n):
82+
A[i][i] = 1
83+
84+
def _diff_one(s1, s2):
85+
count = 0
86+
for c1, c2 in zip(s1, s2):
87+
if c1 != c2:
88+
count+=1
89+
return count == 1
90+
91+
# 构图
92+
for i in range(len(wordList)):
93+
for j in range(i, len(wordList)):
94+
if _diff_one(wordList[i], wordList[j]):
95+
A[i][j] = 1
96+
A[j][i] = 1
97+
98+
# -2 -> -1 BFS
99+
pre = [[-1] for i in range(n)]
100+
way_len = [100000 for i in range(n)]
101+
flag = [True for i in range(n)]
102+
begin_index = wordList.index(beginWord)
103+
way_len[begin_index] = 0
104+
q = queue.Queue()
105+
q.put(begin_index)
106+
flag[begin_index] = False
107+
while not q.empty():
108+
i = q.get()
109+
flag[i] = False
110+
for j in range(n):
111+
if A[i][j] == 1 and flag[j]:
112+
if way_len[j] > way_len[i] + 1:
113+
way_len[j] = way_len[i] + 1
114+
pre[j] = [i]
115+
elif way_len[j] == way_len[i] + 1:
116+
if not i in pre[j]:
117+
pre[j].append(i)
118+
q.put(j)
119+
res = []
120+
print('test')
121+
k = wordList.index(endWord)
122+
if way_len[k] == 100000:
123+
return res
124+
125+
if pre[k] == [-1]:
126+
return []
127+
128+
temp_res = []
129+
def _dfs(lst, pos):
130+
131+
if pos == -1:
132+
res.append(temp_res[::-1])
133+
return
134+
for node in lst:
135+
temp_res.append(wordList[pos])
136+
_dfs(pre[node], node)
137+
temp_res.pop()
138+
139+
140+
_dfs(pre[k], k)
141+
return res

python/126_2.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import queue
2+
class Solution:
3+
def findLadders(self, beginWord, endWord, wordList):
4+
if not beginWord in wordList:
5+
wordList.append(beginWord)
6+
if not endWord in wordList:
7+
return []
8+
9+
n = len(wordList)
10+
A = [[0] * n for i in range(n)]
11+
for i in range(n):
12+
A[i][i] = 1
13+
14+
def _diff_one(s1, s2):
15+
count = 0
16+
for c1, c2 in zip(s1, s2):
17+
if c1 != c2:
18+
count+=1
19+
return count == 1
20+
21+
# 构图
22+
for i in range(len(wordList)):
23+
for j in range(i, len(wordList)):
24+
if _diff_one(wordList[i], wordList[j]):
25+
A[i][j] = 1
26+
A[j][i] = 1
27+
28+
# dfs+剪枝
29+
begin_index = wordList.index(beginWord)
30+
end_pos = wordList.index(endWord)
31+
32+
max_way_len = 1000000
33+
temp_res = []
34+
view = [False for i in range(n)]
35+
res = []
36+
37+
def _dfs(pos, way_len):
38+
nonlocal max_way_len
39+
if pos == end_pos:
40+
if way_len < max_way_len:
41+
max_way_len = way_len
42+
res.clear()
43+
res.append([beginWord] + temp_res[:])
44+
print(temp_res[:])
45+
return
46+
47+
if way_len >= max_way_len:
48+
return # 剪枝
49+
50+
for i in range(n):
51+
# 判断条件
52+
if A[pos][i] == 1 and (not view[i]):
53+
view[i] = True
54+
temp_res.append(wordList[i])
55+
_dfs(i, way_len+1)
56+
view[i] = False
57+
temp_res.pop()
58+
59+
_dfs(begin_index, 0)
60+
# -2 -> -1 BFS
61+
# pre = [[-1] for i in range(n)]
62+
# way_len = [100000 for i in range(n)]
63+
# flag = [True for i in range(n)]
64+
# begin_index = wordList.index(beginWord)
65+
# way_len[begin_index] = 0
66+
# q = queue.Queue()
67+
# q.put(begin_index)
68+
# flag[begin_index] = False
69+
# while not q.empty():
70+
# i = q.get()
71+
# flag[i] = False
72+
# for j in range(n):
73+
# if A[i][j] == 1 and flag[j]:
74+
# if way_len[j] > way_len[i] + 1:
75+
# way_len[j] = way_len[i] + 1
76+
# pre[j] = [i]
77+
# elif way_len[j] == way_len[i] + 1:
78+
# if not i in pre[j]:
79+
# pre[j].append(i)
80+
# q.put(j)
81+
82+
# res = []
83+
# print('test')
84+
# k = wordList.index(endWord)
85+
# if way_len[k] == 100000:
86+
# return res
87+
88+
# if pre[k] == [-1]:
89+
# return []
90+
91+
# temp_res = []
92+
# def _dfs(lst, pos):
93+
94+
# if pos == -1:
95+
# res.append(temp_res[::-1])
96+
# return
97+
# for node in lst:
98+
# temp_res.append(wordList[pos])
99+
# _dfs(pre[node], node)
100+
# temp_res.pop()
101+
102+
103+
# _dfs(pre[k], k)
104+
return res
105+

0 commit comments

Comments
 (0)