-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackTrack.cpp
More file actions
617 lines (587 loc) · 14.8 KB
/
backTrack.cpp
File metadata and controls
617 lines (587 loc) · 14.8 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
#include "backTrack.h"
//划分k个相等的子集和
bool canPartitionKSubsets(vector<int>& nums, int k)
{
//排除一些基本情况
if(k > nums.size())
{
return false;
}
int sum = 0;
for (int i = 0; i < nums.size(); i++)
{
sum += nums[i];
}
if (sum % k != 0)
{
return false;
}
int target = sum / k;
vector<int> backet;
backet.resize(k);
return backtrack_KSubsets(nums, 0, backet, target);
}
//k个相等子集和的回溯函数
bool backtrack_KSubsets(vector<int>& nums, int index, vector<int> bucket, int target)
{
//base case
//以数字的视角,所以base case应该是nums的数字遍历完了一遍
if (index == nums.size())
{
//判断每个桶中的数字是否一样
for (int i = 0; i < bucket.size(); i++)
{
if(bucket[i] != target)
{
return false;
}
}
return true;
}
//遍历每个桶
for (int i = 0; i < bucket.size(); i++)
{
//剪枝
//当桶中的数字新加一个数之后,其和大于target,直接下一次循环
if (bucket[i] + nums[index] > target)
{
continue;
}
//做选择:把当前nums加入桶中
bucket[i] += nums[index];
//回溯
if (backtrack_KSubsets(nums, index + 1, bucket, target))
{
return true;
}
//撤销选择
bucket[i] -= nums[index];
}
// nums[index] 装入哪个桶都不行
return false;
}
//给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案
vector<vector<int>> permute(vector<int>& nums)
{
//记录排列路径
vector<int> track;
//结果
vector<vector<int>> res;
// 「路径」中的元素会被标记为 true,避免重复使用,一开始要初始化为0,全为false
bool* used = new bool[nums.size()]{};
//结果必须引用传参,不然错误
backtrack_permute(nums, track, used,res);
return res;
}
//回溯函数
// 路径:记录在 track 中
// 选择列表:nums 中不存在于 track 的那些元素(used[i] 为 false)
// 结束条件:nums 中的元素全都在 track 中出现
void backtrack_permute(vector<int> nums, vector<int> track, bool * used, vector<vector<int>>& out_res)
{
// 触发结束条件
if (track.size() == nums.size()) {
out_res.push_back(track);
return ;
}
for (int i = 0; i < nums.size(); i++) {
// 排除不合法的选择
if (used[i]) {
// nums[i] 已经在 track 中,跳过
continue;
}
// 做选择
track.push_back(nums[i]);
used[i] = true;
// 进入下一层决策树
backtrack_permute(nums, track, used, out_res);
// 取消选择
track.pop_back();
used[i] = false;
}
}
//78 子集 中等:
vector<vector<int>> subsets(vector<int>& nums)
{
vector<vector<int>> res;
vector<int> track;
backtrack_subsets(nums, 0, res, track);
return res;
}
//78 子集回溯函数
void backtrack_subsets(vector<int>& nums, int start, vector<vector<int>>& res, vector<int> track)
{
//子集是要收集所有的节点,而组合和分割只是收割叶子节点
//所以子集收集元素的时候是放在if外面,这代表不需要判断就收集,如果在里面会漏掉自己
res.push_back(track);
for (int i = start; i < nums.size(); i++)
{
track.push_back(nums[i]);
// 通过 start 参数控制树枝的遍历,避免产生重复的子集
backtrack_subsets(nums, i + 1, res, track);
track.pop_back();
}
}
//90 子集II 中等
vector<vector<int>> subsetsWithDup(vector<int>& nums)
{
vector<vector<int>> res;
vector<int> path;
vector<bool> used(nums.size(), false);
sort(nums.begin(),nums.end());
subsetsDupBackTrack(res,path,nums,0,used);
return res;
}
//子集II的回调函数
void subsetsDupBackTrack(vector<vector<int>>& res, vector<int>& path, vector<int>& nums, int startIndex, vector<bool> &used)
{
//这和子集问题区别在于集合里面有重复的元素,所以需要去重
res.push_back(path);
if (startIndex >= nums.size()) return;
for (size_t i = startIndex; i < nums.size(); i++)
{
//去重,和组合II问题的去重一样,也是在树层去重,树枝不需要去重
if (i > 0 && nums[i] == nums[i - 1] && used[i-1] == false) continue;
path.push_back(nums[i]);
used[i] = true;
subsetsDupBackTrack(res, path, nums, i + 1,used);
path.pop_back();
used[i] = false;
}
}
//77 组合 中等
vector<vector<int>> combine(int n, int k)
{
vector<vector<int>> res;
vector<int> path;
backTrackCombine(res, path, n, k, 1);
return res;
}
//组合的回溯函数
void backTrackCombine(vector<vector<int>>& res, vector<int>& path, int n, int k, int startIndex)
{
//终止条件
if (path.size() == k)
{
res.push_back(path);
return;
}
//可以剪枝优化,在for循环的终止条件做剪枝
//for (int i = startIndex; i <= n - (k - path.size()) + 1; i++)
for (int i = startIndex; i <= n; i++)//横向遍历
{
path.push_back(i);//收集路径上的值
backTrackCombine(res, path, n, k, i + 1);//递归,也就是纵向遍历
path.pop_back();//回溯操作
}
}
//216 组合总和III 中等
vector<vector<int>> combinationSum3(int k, int n)
{
vector<vector<int>> res;
vector<int> path;
backTrackCombinationSum3(res,path,k,n,1);
return res;
}
//组合总和的回调函数
void backTrackCombinationSum3(vector<vector<int>>& res, vector<int>& path, int k, int n, int startIndex)
{
if (path.size() == k)
{
//相加之和为n
if (accumulate(path.begin(), path.end(), 0) == n)
{
res.push_back(path);
return;
}
return;
}
for (int i = startIndex; i <= 9; i++)
{
path.push_back(i);
//如果索引是startIndex+1,则数组中会有重复数字
backTrackCombinationSum3(res, path, k, n, i + 1);
path.pop_back();
}
}
//组合总和
vector<vector<int>> combinationSum(vector<int>& candidates, int target)
{
vector<vector<int>> res;
vector<int> path;
backTrackcombinationSum(res, path,candidates, target, 0);
return res;
}
//组合总和回调函数
void backTrackcombinationSum(vector<vector<int>>& res, vector<int>& path, vector<int>& candidate, int target, int startIndex)
{
//如果一直没有符合条件的就是空
if (accumulate(path.begin(),path.end(),0) == target)
{
res.push_back(path);
return;
}
else if (accumulate(path.begin(), path.end(), 0) > target)//没有这个就会一直递归
{
return;
}
for (int i = startIndex; i < candidate.size(); i++)
{
path.push_back(candidate[i]);
//可以有重复元素,所以索引都从0开始
//// 关键点:不用i+1了,表示可以重复读取当前的数
backTrackcombinationSum(res, path, candidate, target, i);
path.pop_back();
}
}
//40 组合总和II 中等
//和I的区别在于它的输入里面有重复的数字,而且同一个数不能被重复使用,难点在于去重
vector<vector<int>> combinationSum2(vector<int>& candidates, int target)
{
vector<bool> used(candidates.size(), false);
vector<vector<int>> res;
vector<int> path;
// 首先把给candidates排序,让其相同的元素都挨在一起。用于去重
sort(candidates.begin(), candidates.end());
backTrackcombinationSum2(res, path, candidates, target, 0,used);
return res;
}
//组合总和II回调
void backTrackcombinationSum2(vector<vector<int>>& res, vector<int>& path, vector<int>& candidate, int target, int startIndex, vector<bool> & used)
{
if (accumulate(path.begin(), path.end(), 0) == target)
{
res.push_back(path);
return;
}
//和大于目标数,得返回,不然会一直递归
else if(accumulate(path.begin(), path.end(), 0) > target)
{
return;
}
for (int i = startIndex; i < candidate.size(); i++)
{
//还不是很理解
// used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
// used[i - 1] == false,说明同一树层candidates[i - 1]使用过
// 要对同一树层使用过的元素进行跳过
if (i > 0 && candidate[i] == candidate[i - 1] && used[i - 1] == false) {
continue;
}
path.push_back(candidate[i]);
used[i] = true;
backTrackcombinationSum2(res, path, candidate, target, i + 1,used);
used[i] = false;
path.pop_back();
}
}
//131 分割回文串 中等
vector<vector<string>> partition(string s)
{
vector<vector<string>> res;
vector<string> path;
backTrackPartition(res, path, s, 0);
return res;
}
//分割回文串的回溯函数
void backTrackPartition(vector<vector<string>>& res, vector<string>& path, string s, int startIndex)
{
// 如果起始位置已经大于s的大小,说明已经找到了一组分割方案了,再判断其是不是回文
if (vectorIsPalindrome(path) && startIndex >= s.size())
{
res.push_back(path);
return;
}
for (int i = startIndex; i < s.size(); i++)
{
//这一步关键,怎截取字符串
path.push_back(s.substr(startIndex, i - startIndex + 1));
backTrackPartition(res, path, s, i + 1);
path.pop_back();
}
}
//判断字符串是不是回文的辅助函数
bool vectorIsPalindrome(vector<string>& str)
{
for (size_t i = 0; i < str.size(); i++)
{
int right = str[i].size() - 1;
int left = 0;
while (left < right)
{
if (str[i][left] != str[i][right]) return false;
left++;
right--;
}
}
return true;
}
//93 复原ip地址 中等
vector<string> restoreIpAddresses(string s)
{
vector<string> res;
vector<string> path;
restoreIpBackTrack(res, path, s, 0);
return res;
}
//复原ip地址回调函数
void restoreIpBackTrack(vector<string>& res, vector<string>& path, string & s, int startIndex)
{
//一定不要忘了加起始位置大于字符串s的大小这个条件
//不然没把字符串s用完就输出结果了
if (path.size() == 4 && isValidIp(path) && startIndex >= s.size())
{
string r;
//转成ip形式
for (size_t i = 0; i < path.size() - 1; i++)
{
r = r + path[i] + ".";
}
r = r + path[3];//最后一个不用加.
res.push_back(r);
return;
}
else if(path.size() > 4)
{
return;
}
for (int i = startIndex; i < s.size(); i++)
{
path.push_back(s.substr(startIndex, i - startIndex + 1));
restoreIpBackTrack(res, path, s, i + 1);
path.pop_back();
}
}
//验证是否是有效ip
bool isValidIp(const vector<string>& Ip)
{
for (size_t i = 0; i < Ip.size(); i++)
{
if (Ip[i].size() > 1)
{
//第一位不能是0
if (Ip[i][0] == '0') return false;
}
//不能大于255
if (Ip[i].size() == 3)
{
//先将其转为数字
int sum = 0;
int k = 2;
for (size_t j = 0; j < 3; j++)
{
sum += (Ip[i][j] - '0') * pow(10, k--);
}
if (sum > 255) return false;
}
if (Ip[i].size() > 3) return false;
}
return true;
}
//491 递增子序列 中等
vector<vector<int>> findSubsequences(vector<int>& nums)
{
vector<vector<int>> res;
vector<int> paths;
findSubsetsBackTrack(res, paths, nums, 0);
return res;
}
//使用set进行去重,简单粗暴,耗时是剪枝的4倍
vector<vector<int>> findSubsequencesBaseSet(vector<int>& nums)
{
set<vector<int>> set_res;
vector<vector<int>> res;
vector<int> paths;
findSubsetsBackTrack(set_res, paths, nums, 0);
for (auto it : set_res)
{
res.push_back(it);
}
return res;
}
//递增子序列的回溯函数,集合去重版本
void findSubsetsBackTrack(set<vector<int>>& res, vector<int>& paths, vector<int>& nums, int startIndex)
{
//一定要大小的判断在前面,不然如果paths是空的话,先执行判断是不是递增子序列,
//将会报越界错误
if (paths.size() > 1 && isIncreasingSeq(paths))
{
//集合会自动去重,这种方法简单粗暴,不需要思考如何剪枝
//但时间复杂度会长四倍,因为集合是用一个红黑树进行去重的
res.insert(paths);
// 注意这里不要加return,因为要取树上的所有节点
}
if (startIndex >= nums.size())
{
return;
}
for (int i = startIndex; i < nums.size(); i++)
{
paths.push_back(nums[i]);
findSubsetsBackTrack(res, paths, nums, i + 1);
paths.pop_back();
}
}
//递增子序列的回溯函数
void findSubsetsBackTrack(vector<vector<int>>& res, vector<int>& paths, vector<int>& nums, int startIndex)
{
//一定要大小的判断在前面,不然如果paths是空的话,先执行判断是不是递增子序列,
//将会报越界错误
if (paths.size() > 1 && isIncreasingSeq(paths) )
{
res.push_back(paths);
// 注意这里不要加return,因为要取树上的所有节点
}
//需要去重
if (startIndex >= nums.size())
{
return;
}
int used[201] = { 0 }; // 这里使用数组来进行去重操作,题目说数值范围[-100, 100]
for (int i = startIndex; i < nums.size(); i++)
{
if (used[nums[i] + 100] == 1) continue;
used[nums[i] + 100] = 1; // 记录这个元素在本层用过了,本层后面不能再用了
paths.push_back(nums[i]);
findSubsetsBackTrack(res,paths,nums,i+1);
paths.pop_back();
//这里不需要对used回溯,
//是记录本层元素是否重复使用,新的一层uset都会重新定义(清空),所以要知道uset只负责本层!
}
}
//是不是递增序列
bool isIncreasingSeq(vector<int>& path)
{
int ori = path[0];
for (size_t i = 1; i < path.size(); i++)
{
if (ori <= path[i])
{
ori = path[i];
}
else
{
return false;
}
}
return true;
}
//全排列II 中等 给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列
vector<vector<int>> permuteUnique(vector<int>& nums)
{
vector<vector<int>> res;
vector<bool> used(nums.size(), false);
//这里不能使用set去重,因为set认为112和121是一样的,但它两实际上不是一个排列
//set<vector<int>> set_res;
vector<int> path;
sort(nums.begin(), nums.end());
permuteIIBackTrack(res, path, nums, used);
return res;
}
//全排列II的回溯函数
void permuteIIBackTrack(vector<vector<int>>& set_res, vector<int>& path, vector<int>& nums, vector<bool> & used)
{
if (path.size() == nums.size())
{
set_res.push_back(path);
return;
}
for (int i = 0; i < nums.size(); i++)
{
//去重
if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) continue;
//还得加个这个判断, 不然会重复使用一个元素
if (used[i] == false)
{
path.push_back(nums[i]);
used[i] = true;
permuteIIBackTrack(set_res, path, nums, used);
path.pop_back();
used[i] = false;
}
}
}
//51 N皇后 困难 2022830暂时未解决,解答中有很多重复的元素
vector<vector<string>> solveNQueens(int n)
{
vector<vector<string>> res;
vector<string> path;
for (size_t i = 0; i < n; i++)
{
path.push_back(string(n, '.'));
}
nQueensBacktracking(res, path, n, 0);
return res;
}
//n皇后回溯函数
void nQueensBacktracking(vector<vector<string>>& res, vector<string>& path, int n, int row)
{
//行到了最末行的下一个就认为一个组合出来了
if ( row == n)
{
res.push_back(path);
return;
}
//for循环深度是列的长度
for (int col = 0; col < n; col++)
{
{
if (isNQueenValid(path, n))
path[row][col] = 'Q';
nQueensBacktracking(res, path, n, row + 1);
//这里的回溯操作很关键,这里的置空就是把皇后取走,也就是将q变为.
path[row][col] = '.';
}
}
}
//n皇后约束条件
//还是有重复的元素2022830,暂时未解决
bool isNQueenValid(vector<string>& path, int n)
{
for (size_t i = 0; i < n; i++)
{
for (size_t j = 0; j < n; j++)
{
//就i j都大于0而且值为q的时候就判断该元素上下左右是否为q
if (i > 0 && j > 0 && i < n-1 && j < n-1 && path[i][j] == 'Q')
{
if (path[i][j] == path[i][j + 1] || path[i][j] == path[i + 1][j]
|| path[i][j] == path[i][j - 1] || path[i][j] == path[i - 1][j]
|| path[i][j] == path[i-1][j - 1] || path[i][j] == path[i+1][j + 1]
|| path[i][j] == path[i+1][j - 1] || path[i][j] == path[i-1][j + 1])
{
return false;
}
}
//就i=0 j大于0而且值为q的时候就判断该元素下左右是否为q
else if(i == 0 && j > 0 && j < n - 1 && path[i][j] == 'Q')
{
if (path[i][j] == path[i][j + 1] || path[i][j] == path[i + 1][j]
|| path[i][j] == path[i][j - 1] || path[i][j] == path[i+1][j - 1]
|| path[i][j] == path[i+1][j + 1])
{
return false;
}
}
//就i>0 j=0而且值为q的时候就判断该元素上下右是否为q
else if (j == 0 && i > 0 && i < n - 1 && path[i][j] == 'Q')
{
if (path[i][j] == path[i][j + 1] || path[i][j] == path[i + 1][j]
|| path[i][j] == path[i - 1][j] || path[i][j] == path[i+1][j + 1]
|| path[i][j] == path[i-1][j + 1])
{
return false;
}
}
//就i=0 j=0而且值为q的时候就判断该元素下右是否为q
else if (j == 0 && i == 0 && path[i][j] == 'Q')
{
if (path[i][j] == path[i][j + 1] || path[i][j] == path[i + 1][j])
{
return false;
}
}
}
}
return true;
}