66
66
67
67
<!-- 这里可写通用的实现逻辑 -->
68
68
69
+ BFS 最小步数模型。
70
+
69
71
<!-- tabs:start -->
70
72
71
73
### ** Python3**
75
77
``` python
76
78
class Solution :
77
79
def openLock (self , deadends : List[str ], target : str ) -> int :
78
- s = set (deadends)
79
- if target in s or ' 0000' in s:
80
- return - 1
81
- if target == ' 0000' :
82
- return 0
83
-
84
- def prev (c ):
85
- return ' 9' if c == ' 0' else str (int (c) - 1 )
86
-
87
- def next (c ):
88
- return ' 0' if c == ' 9' else str (int (c) + 1 )
89
-
90
- def get (t ):
80
+ def next (s ):
91
81
res = []
92
- t = list (t )
82
+ s = list (s )
93
83
for i in range (4 ):
94
- c = t [i]
95
- t [i] = prev(c )
96
- res.append(' ' .join(t ))
97
- t [i] = next (c )
98
- res.append(' ' .join(t ))
99
- t [i] = c
84
+ c = s [i]
85
+ s [i] = ' 9 ' if c == ' 0 ' else str ( int (c) - 1 )
86
+ res.append(' ' .join(s ))
87
+ s [i] = ' 0 ' if c == ' 9 ' else str ( int (c) + 1 )
88
+ res.append(' ' .join(s ))
89
+ s [i] = c
100
90
return res
101
91
102
- visited = set ()
103
- q = deque([(' 0000' , 0 )])
92
+ if target == ' 0000' :
93
+ return 0
94
+ s = set (deadends)
95
+ if ' 0000' in s:
96
+ return - 1
97
+ q = deque([(' 0000' )])
98
+ s.add(' 0000' )
99
+ ans = 0
104
100
while q:
105
- status, step = q.popleft()
106
- for t in get(status):
107
- if t in visited or t in s:
108
- continue
109
- if t == target:
110
- return step + 1
111
- q.append((t, step + 1 ))
112
- visited.add(t)
101
+ ans += 1
102
+ for _ in range (len (q), 0 , - 1 ):
103
+ p = q.popleft()
104
+ for t in next (p):
105
+ if t == target:
106
+ return ans
107
+ if t not in s:
108
+ q.append(t)
109
+ s.add(t)
113
110
return - 1
114
111
```
115
112
@@ -120,52 +117,43 @@ class Solution:
120
117
``` java
121
118
class Solution {
122
119
public int openLock (String [] deadends , String target ) {
120
+ if (" 0000" . equals(target)) {
121
+ return 0 ;
122
+ }
123
123
Set<String > s = new HashSet<> (Arrays . asList(deadends));
124
- if (s. contains(target) || s . contains( " 0000" )) {
124
+ if (s. contains(" 0000" )) {
125
125
return - 1 ;
126
126
}
127
- if (Objects . equals(target, " 0000" )) {
128
- return 0 ;
129
- }
130
- Set<String > visited = new HashSet<> ();
131
127
Deque<String > q = new ArrayDeque<> ();
132
- q. offerLast(" 0000" );
133
- int step = 0 ;
128
+ q. offer(" 0000" );
129
+ s. add(" 0000" );
130
+ int ans = 0 ;
134
131
while (! q. isEmpty()) {
135
- ++ step ;
136
- for (int i = 0 , n = q. size(); i < n; ++ i ) {
137
- String status = q. pollFirst ();
138
- for (String t : get(status )) {
139
- if (visited . contains(t) || s . contains (t)) {
140
- continue ;
132
+ ++ ans ;
133
+ for (int n = q. size(); n > 0 ; -- n ) {
134
+ String p = q. poll ();
135
+ for (String t : next(p )) {
136
+ if (target . equals (t)) {
137
+ return ans ;
141
138
}
142
- if (Objects . equals(t, target)) {
143
- return step;
139
+ if (! s. contains(t)) {
140
+ q. offer(t);
141
+ s. add(t);
144
142
}
145
- q. offerLast(t);
146
- visited. add(t);
147
143
}
148
144
}
149
145
}
150
146
return - 1 ;
151
147
}
152
148
153
- private char prev (char c ) {
154
- return c == ' 0' ? ' 9' : (char ) (c - 1 );
155
- }
156
-
157
- private char next (char c ) {
158
- return c == ' 9' ? ' 0' : (char ) (c + 1 );
159
- }
160
-
161
- private List<String > get (String t ) {
149
+ private List<String > next (String t ) {
162
150
List res = new ArrayList<> ();
163
151
char [] chars = t. toCharArray();
164
152
for (int i = 0 ; i < 4 ; ++ i) {
165
153
char c = chars[i];
166
- chars[i] = prev(c );
154
+ chars[i] = c == ' 0 ' ? ' 9 ' : ( char ) (c - 1 );
167
155
res. add(String . valueOf(chars));
168
- chars[i] = next(c );
156
+ chars[i] = c == ' 9 ' ? ' 0 ' : ( char ) (c + 1 );
169
157
res. add(String . valueOf(chars));
170
158
chars[i] = c;
171
159
}
@@ -181,47 +169,40 @@ class Solution {
181
169
public:
182
170
int openLock(vector<string >& deadends, string target) {
183
171
unordered_set<string > s(deadends.begin(), deadends.end());
184
- if (s.count(target) || s.count( "0000")) return -1;
172
+ if (s.count("0000")) return -1;
185
173
if (target == "0000") return 0;
186
- unordered_set<string > visited;
187
- queue<string > q;
188
- q.push("0000");
189
- int step = 0;
174
+ queue<string > q{{"0000"}};
175
+ s.insert("0000");
176
+ int ans = 0;
190
177
while (!q.empty())
191
178
{
192
- ++step ;
193
- for (int i = 0, n = q.size(); i < n; ++i )
179
+ ++ans ;
180
+ for (int n = q.size(); n > 0; --n )
194
181
{
195
- string status = q.front();
182
+ string p = q.front();
196
183
q.pop();
197
- for (auto t : get(status ))
184
+ for (string t : next(p ))
198
185
{
199
- if (visited.count(t) || s.count(t)) continue;
200
- if (t == target) return step;
201
- q.push(t);
202
- visited.insert(t);
186
+ if (target == t) return ans;
187
+ if (!s.count(t))
188
+ {
189
+ q.push(t);
190
+ s.insert(t);
191
+ }
203
192
}
204
193
}
205
194
}
206
195
return -1;
207
196
}
208
197
209
- char prev(char c) {
210
- return c == '0' ? '9' : (char) (c - 1);
211
- }
212
-
213
- char next (char c) {
214
- return c == '9' ? '0' : (char) (c + 1);
215
- }
216
-
217
- vector<string> get(string& t) {
198
+ vector<string> next(string& t) {
218
199
vector<string> res;
219
200
for (int i = 0; i < 4; ++i)
220
201
{
221
202
char c = t[i];
222
- t[i] = prev(c );
203
+ t[i] = c == '0' ? '9' : (char) (c - 1 );
223
204
res.push_back(t);
224
- t[i] = next(c );
205
+ t[i] = c == '9' ? '0' : (char) (c + 1 );
225
206
res.push_back(t);
226
207
t[i] = c;
227
208
}
@@ -230,6 +211,61 @@ public:
230
211
};
231
212
```
232
213
214
+ ### ** Go**
215
+
216
+ ``` go
217
+ func openLock (deadends []string , target string ) int {
218
+ if target == " 0000" {
219
+ return 0
220
+ }
221
+ s := make (map [string ]bool )
222
+ for _ , d := range deadends {
223
+ s[d] = true
224
+ }
225
+ if s[" 0000" ] {
226
+ return -1
227
+ }
228
+ q := []string {" 0000" }
229
+ s[" 0000" ] = true
230
+ ans := 0
231
+ next := func (t string ) []string {
232
+ s := []byte (t)
233
+ var res []string
234
+ for i , b := range s {
235
+ s[i] = b - 1
236
+ if s[i] < ' 0' {
237
+ s[i] = ' 9'
238
+ }
239
+ res = append (res, string (s))
240
+ s[i] = b + 1
241
+ if s[i] > ' 9' {
242
+ s[i] = ' 0'
243
+ }
244
+ res = append (res, string (s))
245
+ s[i] = b
246
+ }
247
+ return res
248
+ }
249
+ for len (q) > 0 {
250
+ ans++
251
+ for n := len (q); n > 0 ; n-- {
252
+ p := q[0 ]
253
+ q = q[1 :]
254
+ for _ , t := range next (p) {
255
+ if target == t {
256
+ return ans
257
+ }
258
+ if !s[t] {
259
+ q = append (q, t)
260
+ s[t] = true
261
+ }
262
+ }
263
+ }
264
+ }
265
+ return -1
266
+ }
267
+ ```
268
+
233
269
### ** ...**
234
270
235
271
```
0 commit comments