|
| 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 |
0 commit comments