forked from mrchuanxu/RegularNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathback_origin.cpp
More file actions
122 lines (113 loc) · 3.34 KB
/
back_origin.cpp
File metadata and controls
122 lines (113 loc) · 3.34 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
/***
* 深度优先
* 回溯的思想,到了某个分岔路口,不符合期望值就往回走
*
* 8皇后问题
* 同样是回溯的思想
* ***/
#include <iostream>
#include <vector>
using namespace std;
// 首先,声明一个row,用来存储皇后
int *row = new int[8]; // 未初始化
bool isOk(int row,int col); // 判断是否可以有
void printQueens(int *row);
void cal8Queens(int rows){ // 先放了一种
if(rows == 8){
printQueens(row);
return;// 查到了最后,返回
}
for(int col = 0;col<8;col++){ // 这是回溯的重点
if(isOk(rows,col)){
row[rows] = col;
cal8Queens(rows+1); // 考察下一行 每次都是新的放法
}
}// 打印了所有的可能性
return;
}
bool isOk(int rows,int col){
int leftup = col-1;
int rightup = col+1;
for(int i = rows-1;i>=0;i--){
if(row[i] == col) return false; // 行与列
if(leftup>=0){
if(row[i] == leftup) return false;// 左对角线
}
if(rightup < 8){
if(row[i] == rightup) return false; // 右对角线
}
--leftup;
++rightup;
}
// 检测完没问题,就返回这个行列可以
return true;
}
void printQueens(int *row){
// 直接打印就好了
// 打印一个二维数组
for(int rows= 0;rows<8;rows++){
for(int cols = 0;cols<8;cols++){
if(row[rows] == cols) cout << "Q";
else cout << "*";
}
cout << endl;
}
cout << endl;
}
/***
* 0-1背包问题
* 搜索剪枝的技巧
* 当发现物品的重量超过Wkg之后,就停止继续探测剩下的物品
* cw表示当前已经装进去的物品的重量和;i表示考察到哪个物品了
* w背包重量;items表示每个物品的重量;n表示物品个数
* 假设背包可承受重量100,物品个数10,物品重量存储在数组a中,那可以这样调用函数
*
* ***/
int maxW = 100;
int *result = new int[10];
void func(int i,int coutweight,int items[],int n,int w){
if(coutweight == w||i==n){ // cw == w表示装满了;i==n 表示已经考察完所有物品
if(coutweight > maxW) maxW = coutweight;
return;
}
func(i+1,coutweight,items,n,w);// 不装 不装的条件是目前countweight已经等于w了
if(coutweight + items[i]<=w){ // 没超过背包承受的重量,就装 条件是countweight还小
result[i] = items[i];
func(i+1,coutweight+items[i],items,n,w);
}
}
/***
* 全排列
* 全排列数字
* ***/
void Permutation(vector<int>& vec,int start){
// 判断递归终止条件,判断到vec最后的长度
if(start == vec.size()){
for(auto iter:vec){
cout << iter;
}
cout << endl;
}else{
for(int i = start;i<vec.size();++i){
swap(vec[start],vec[i]);// 用第一个跟后面的持续交换
Permutation(vec,start+1);
swap(vec[start],vec[i]); // 重新将最后一个交换到前面,作起始点
}
}
}
int main(){
vector<int> vec{1,2,3,4};
Permutation(vec,0);
return 0;
}
int main(){
int a[] = {1,2,3,2,1,2,3,4,3,2,1,3,4,2,1,2,3,4,5,3,1,8,9,7,10,22,33};
func(0,0,a,10,50);
// cal8Queens(0);
for(int i = 0;i<10;i++){
cout << result[i] << endl;
}
delete []row;
delete []result;
return 0;
}