-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG03_BFS.cpp
More file actions
651 lines (627 loc) · 20.3 KB
/
Copy pathG03_BFS.cpp
File metadata and controls
651 lines (627 loc) · 20.3 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/******************** DFS ********************/
// Binary Tree Level Order Traversal-1, BFS, use two queues
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> results;
if (root == NULL) return results;
queue<TreeNode *> curr, next;
curr.push(root);
while (!curr.empty()) {
vector<int> level;
while (!curr.empty()) {
TreeNode *node = curr.front();
curr.pop();
level.push_back(node->val);
if (node->left != NULL)
next.push(node->left);
if (node->right != NULL)
next.push(node->right);
}
results.push_back(level);
swap(curr, next);
}
return results;
}
};
// Binary Tree Level Order Traversal-2, BFS, use NULL pointer
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> results;
if (root == NULL) return results;
vector<int> level;
queue<TreeNode *> next;
next.push(root);
next.push(NULL);
while (!next.empty()) {
TreeNode *node = next.front();
next.pop();
if (node == NULL) {
results.push_back(level);
level.clear();
if (!next.empty())
next.push(NULL);
} else {
level.push_back(node->val);
if (node->left != NULL)
next.push(node->left);
if (node->right != NULL)
next.push(node->right);
}
}
return results;
}
};
// Binary Tree Zigzag Level Order Traversal-1, BFS, use two queues and list
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> results;
if (root == NULL) return results;
queue<TreeNode *> curr, next;
curr.push(root);
bool isOdd = true;
while (!curr.empty()) {
list<int> level;
while (!curr.empty()) {
TreeNode *node = curr.front();
curr.pop();
if (isOdd) level.push_back(node->val);
else level.push_front(node->val);
if (node->left != NULL)
next.push(node->left);
if (node->right != NULL)
next.push(node->right);
}
vector<int> res(level.begin(), level.end());
results.push_back(res);
swap(curr, next);
isOdd = !isOdd;
}
return results;
}
};
// Binary Tree Zigzag Level Order Traversal-2, BFS, use two stacks
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> results;
if (root == NULL) return results;
stack<TreeNode *> curr, next;
curr.push(root);
bool isOdd = false;
while (!curr.empty()) {
vector<int> level;
while (!curr.empty()) {
TreeNode *node = curr.top();
curr.pop();
level.push_back(node->val);
if (!isOdd) {
if (node->left != NULL)
next.push(node->left);
if (node->right != NULL)
next.push(node->right);
} else {
if (node->right != NULL)
next.push(node->right);
if (node->left != NULL)
next.push(node->left);
}
}
results.push_back(level);
swap(curr, next);
isOdd = !isOdd;
}
return results;
}
};
// Find Bottom Left Tree Value, BFS, use two queues
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode *> curr, next;
curr.push(root);
int leftVal = 0;
bool isFirst = false;
while (!curr.empty()) {
isFirst = true;
while (!curr.empty()) {
TreeNode *node = curr.front();
curr.pop();
if (isFirst) {
leftVal = node->val;
isFirst = false;
}
if (node->left == NULL && node->right == NULL)
continue;
if (node->left != NULL)
next.push(node->left);
if (node->right != NULL)
next.push(node->right);
}
swap(curr, next);
}
return leftVal;
}
};
// The Maze, Breadth-first Search
struct Pair {
int x;
int y;
Pair(int a, int b) : x(a), y(b) {}
};
class Solution {
public:
bool hasPath(vector<vector<int>>& maze, vector<int>& start,
vector<int>& dest) {
int m = maze.size();
int n = maze[0].size();
vector<vector<int>> distance(m, vector<int>(n, INT_MAX));
vector<int> dx{1, 0, 0, -1};
vector<int> dy{0, -1, 1, 0};
queue<Pair> grid;
grid.push(Pair(start[0], start[1]));
distance[start[0]][start[1]] = 0;
while (!grid.empty()) {
Pair curPnt = grid.front();
grid.pop();
for (int i = 0; i < 4; i++) {
int x = curPnt.x, y = curPnt.y;
int dist = distance[x][y];
while (x >= 0 && x < m && y >= 0 && y < n &&
maze[x][y] == 0) {
x += dx[i];
y += dy[i];
dist++;
}
x -= dx[i];
y -= dy[i];
dist--;
if (dist < distance[x][y]) {
distance[x][y] = dist;
if (x != dest[0] || y != dest[1])
grid.push(Pair(x, y));
}
}
}
int result = distance[dest[0]][dest[1]];
return result == INT_MAX ? false : true;
}
};
// The Maze II, Breadth-first Search
struct Pair {
int x;
int y;
Pair(int a, int b) : x(a), y(b) {}
};
class Solution {
public:
int shortestDistance(vector<vector<int>>& maze, vector<int>& start,
vector<int>& dest) {
int m = maze.size();
int n = maze[0].size();
vector<vector<int>> distance(m, vector<int>(n, INT_MAX));
vector<int> dx{1, 0, 0, -1};
vector<int> dy{0, -1, 1, 0};
queue<Pair> grid;
grid.push(Pair(start[0], start[1]));
distance[start[0]][start[1]] = 0;
while (!grid.empty()) {
Pair curPnt = grid.front();
grid.pop();
for (int i = 0; i < 4; i++) {
int x = curPnt.x, y = curPnt.y;
int dist = distance[x][y];
while (x >= 0 && x < m && y >= 0 && y < n &&
maze[x][y] == 0) {
x += dx[i];
y += dy[i];
dist++;
}
x -= dx[i];
y -= dy[i];
dist--;
if (dist < distance[x][y]) {
distance[x][y] = dist;
if (x != dest[0] || y != dest[1])
grid.push(Pair(x, y));
}
}
}
int result = distance[dest[0]][dest[1]];
return result == INT_MAX ? -1 : result;
}
};
// The Maze III, Breadth-first Search
struct Pair {
int x;
int y;
Pair(int a, int b) : x(a), y(b) {}
};
class Solution {
public:
string findShortestWay(vector<vector<int>>& maze, vector<int>& ball,
vector<int>& hole) {
int m = maze.size();
int n = maze[0].size();
vector<int> dx{1, 0, 0, -1};
vector<int> dy{0, -1, 1, 0};
vector<string> dir{"d", "l", "r", "u"};
vector<vector<int>> distance(m, vector<int>(n, INT_MAX));
vector<vector<string>> path(m, vector<string>(n, ""));
queue<Pair> grid;
grid.push(Pair(ball[0], ball[1]));
distance[ball[0]][ball[1]] = 0;
// path[ball[0]][ball[1]] = "";
while (!grid.empty()) {
Pair curPnt = grid.front();
grid.pop();
for (int i = 0; i < 4; i++) {
int x = curPnt.x, y = curPnt.y;
int dist = distance[x][y];
string curPath = path[x][y];
while (x >= 0 && x < m && y >= 0 && y < n &&
maze[x][y] == 0 &&
(x != hole[0] || y != hole[1])) {
x += dx[i];
y += dy[i];
dist++;
}
if (x != hole[0] || y != hole[1]) {
x -= dx[i];
y -= dy[i];
dist--;
}
string newPath = curPath + dir[i];
if (dist < distance[x][y] || (dist == distance[x][y] &&
newPath.compare(path[x][y]) < 0)) {
distance[x][y] = dist;
path[x][y] = newPath;
if (x != hole[0] || y != hole[1])
grid.push(Pair(x, y));
}
}
}
string result = path[hole[0]][hole[1]];
return distance[hole[0]][hole[1]] == INT_MAX ?
"impossible" : result;
}
};
// Word Ladder-1, Breadth-first Search
class Solution {
public:
int ladderLength(string beginWord, string endWord,
vector<string> &wordList) {
unordered_set<string> wordSet(wordList.begin(),
wordList.end());
// if (!wordSet.count(endWord)) return 0;
queue<string> curr, next;
curr.push(beginWord);
unordered_set<string> visited;
visited.insert(beginWord);
int distance = 1;
while (!curr.empty()) {
distance++;
while (!curr.empty()) {
string str = curr.front();
curr.pop();
vector<string> wordsInDist =
_getWordsInDist(wordSet, str);
for (string word : wordsInDist) {
if (word == endWord) return distance;
if (!visited.count(word)) {
next.push(word);
visited.insert(word);
}
}
}
swap(curr, next);
}
return distance == 2 ? 0 : distance;
}
private:
vector<string> _getWordsInDist(
unordered_set<string> &wordSet, string word) {
vector<string> results;
for (int i = 0; i < word.size(); i++) {
char cOri = word[i];
for (char c = 'a'; c <= 'z'; c++) {
if (c == cOri) continue;
word[i] = c;
if (wordSet.count(word))
results.push_back(word);
}
word[i] = cOri;
}
return results;
}
};
// Word Ladder-2, Breadth-first Search
class Solution {
public:
int ladderLength(string beginWord, string endWord,
vector<string>& wordList) {
unordered_set<string> wordSet(wordList.begin(),
wordList.end());
// if (!wordSet.count(endWord)) return 0;
queue<string> curr, next;
curr.push(beginWord);
int distance = 1;
while (!curr.empty()) {
distance++;
while (!curr.empty()) {
string str = curr.front();
curr.pop();
unordered_set<string> wordsInDist =
_getWordsInDist(wordSet, str);
if (wordsInDist.count(endWord))
return distance;
for (string word : wordsInDist)
next.push(word);
}
swap(curr, next);
}
return distance == 2 ? 0 : distance;
}
private:
unordered_set<string> _getWordsInDist(
unordered_set<string> &wordSet, string word) {
unordered_set<string> results;
for (int i = 0; i < word.size(); i++) {
char cOri = word[i];
for (char c = 'a'; c <= 'z'; c++) {
if (c == cOri) continue;
word[i] = c;
if (wordSet.count(word)) {
results.insert(word);
wordSet.erase(word);
}
}
word[i] = cOri;
}
return results;
}
};
// Word Ladder-3, BFS, 20180827
class Solution {
public:
int ladderLength(string beginWord, string endWord,
vector<string>& wordList) {
unordered_set<string> wordSet(wordList.begin(),
wordList.end());
queue<pair<string, int>> curr;
curr.push(make_pair(beginWord, 1));
while (!curr.empty()) {
string str = curr.front().first;
int dist = curr.front().second;
curr.pop();
unordered_set<string> wordsInDist =
_getWordsInDist(wordSet, str);
if (wordsInDist.count(endWord))
return dist + 1;
for (string word : wordsInDist)
curr.push(make_pair(word, dist + 1));
}
return 0;
}
private:
unordered_set<string> _getWordsInDist(
unordered_set<string> &wordSet, string word) {
unordered_set<string> results;
for (int i = 0; i < word.size(); i++) {
char cOri = word[i];
for (char c = 'a'; c <= 'z'; c++) {
if (c == cOri) continue;
word[i] = c;
if (wordSet.count(word)) {
results.insert(word);
wordSet.erase(word);
}
}
word[i] = cOri;
}
return results;
}
};
// Word Ladder II-1, BFS, 20180828
class Solution {
public:
vector<vector<string>> findLadders(string beginWord,
string endWord, vector<string>& wordList) {
unordered_set<string> wordSet(wordList.begin(),
wordList.end());
vector<vector<string>> results;
if (!wordSet.count(endWord)) return results;
unordered_map<string, vector<string>> preList;
queue<string> curr;
curr.push(beginWord);
unordered_set<string> visited;
visited.insert(beginWord);
while (!curr.empty()) {
unordered_set<string> next;
bool isFinished = false;
while (!curr.empty()) {
string str = curr.front();
curr.pop();
unordered_set<string> wordsInDist =
_getWordsInDist(wordSet, str);
for (string word : wordsInDist) {
if (word == endWord) {
isFinished = true;
}
if (!visited.count(word)) {
_updatePreList(word, str, preList);
next.insert(word);
}
}
}
visited.insert(next.begin(), next.end());
if (isFinished) {
vector<string> temp;
_getPaths(results, preList, temp, endWord);
break;
}
for (auto it = next.begin(); it != next.end(); it++)
curr.push(*it);
}
return results;
}
private:
void _updatePreList(string cur, string pre,
unordered_map<string, vector<string>> &preList) {
if (!preList.count(cur)) {
vector<string> temp;
preList[cur] = temp;
}
preList[cur].push_back(pre);
}
unordered_set<string> _getWordsInDist(
unordered_set<string> &wordSet, string word) {
unordered_set<string> results;
for (int i = 0; i < word.size(); i++) {
char cOri = word[i];
for (char c = 'a'; c <= 'z'; c++) {
if (c == cOri) continue;
word[i] = c;
if (wordSet.count(word))
results.insert(word);
}
word[i] = cOri;
}
return results;
}
void _getPaths(vector<vector<string>> &paths,
unordered_map<string, vector<string>> &preList,
vector<string> curPath, string end) {
if (!preList.count(end)) {
curPath.push_back(end);
reverse(curPath.begin(), curPath.end());
paths.push_back(curPath);
return;
}
for (string pre : preList[end]) {
curPath.push_back(end);
_getPaths(paths, preList, curPath, pre);
curPath.pop_back();
}
}
};
// Surrounded region-1, BFS
class Solution {
public:
void solve(vector<vector<char>> &board) {
if (board.size() <= 2 || board[0].size() <= 2)
return;
for (int i = 0; i < board.size(); i++) {
_search(board, i, 0);
_search(board, i, board[0].size() - 1);
}
for (int j = 0; j < board[0].size(); j++) {
_search(board, 0, j);
_search(board, board.size() - 1, j);
}
for (int i = 0; i < board.size(); i++)
for (int j = 0; j < board[0].size(); j++)
board[i][j] = board[i][j] == '+' ? 'O' : 'X';
}
private:
void _search(vector<vector<char>> &board, int x, int y) {
if (board[x][y] != 'O') return;
board[x][y] = '+';
int m = board.size(), n = board[0].size();
queue<int> q;
q.push(x * n + y);
vector<int> dx{-1, 0, 1, 0};
vector<int> dy{0, 1, 0, -1};
while (!q.empty()) {
int temp = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nx = temp / n + dx[i], ny = temp % n + dy[i];
if (nx >= 0 && nx < m && ny >= 0 && ny < n &&
board[nx][ny] == 'O') {
board[nx][ny] = '+';
q.push(nx * n + ny);
}
}
}
}
};
// Surrounded region-2, DFS
class Solution {
public:
void solve(vector<vector<char>> &board) {
if (board.size() <= 2 || board[0].size() <= 2)
return;
for (int i = 0; i < board.size(); i++) {
_search(board, i, 0);
_search(board, i, board[0].size() - 1);
}
for (int j = 0; j < board[0].size(); j++) {
_search(board, 0, j);
_search(board, board.size() - 1, j);
}
for (int i = 0; i < board.size(); i++)
for (int j = 0; j < board[0].size(); j++)
board[i][j] = board[i][j] == '+' ? 'O' : 'X';
}
private:
void _search(vector<vector<char>> &board, int x, int y) {
if (board[x][y] != 'O') return;
board[x][y] = '+';
if (x > 1)
_search(board, x - 1, y);
if (x < board.size() - 2)
_search(board, x + 1, y);
if (y > 1)
_search(board, x, y - 1);
if (y < board[0].size() - 2)
_search(board, x, y + 1);
}
};
// Remove Invalid Parentheses, BFS
class Solution {
public:
vector<string> removeInvalidParentheses(string s) {
vector<string> results;
queue<string> q;
q.push(s);
unordered_set<string> checked;
bool found = false;
int max = 0;
while (!q.empty()) {
string t = q.front();
q.pop();
if (_isValid(t)) {
found = true;
if (t.size() >= max) {
max = t.size();
results.push_back(t);
}
}
if (found) continue;
for (int i = 0; i < t.size(); i++) {
string sub = t.substr(0, i) +
t.substr(i + 1, t.size() - i - 1);
if (!checked.count(sub)) {
q.push(sub);
checked.insert(sub);
}
}
}
return results;
}
private:
bool _isValid(string s) {
int count = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(') count++;
else if (s[i] == ')') {
if (count == 0) return false;
else count--;
}
}
return count == 0;
}
};