first init
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
typedef struct Goods{
|
||||
int weight;
|
||||
int value;
|
||||
} Goods;
|
||||
int getSurplusValue(Goods *goodArr, int n, int i) {
|
||||
int surplusValue = 0;
|
||||
for (int j = i; j < n; j++)
|
||||
surplusValue += goodArr[j].value;
|
||||
return surplusValue;
|
||||
}
|
||||
int dfs(Goods *goodArr, int n, int totalWeight, int *way, int *bestWay,
|
||||
int i, int currWeight, int currValue, int bestValue) {
|
||||
if (i >= n) {
|
||||
bestValue = currValue;
|
||||
for (int k = 0; k < n; k++)
|
||||
bestWay[k] = way[k];
|
||||
return bestValue;
|
||||
}
|
||||
// 搜索左子树
|
||||
if (currWeight + goodArr[i].weight <= totalWeight) {
|
||||
currWeight += goodArr[i].weight;
|
||||
currValue += goodArr[i].value;
|
||||
way[i] = 1;
|
||||
bestValue = dfs(goodArr, n, totalWeight, way, bestWay, i + 1,
|
||||
currWeight, currValue, bestValue);
|
||||
currWeight -= goodArr[i].weight;
|
||||
currValue -= goodArr[i].value;
|
||||
way[i] = 0;
|
||||
}
|
||||
// 搜索右子树
|
||||
if (currValue + getSurplusValue(goodArr, n, i + 1) >= bestValue) {
|
||||
bestValue = dfs(goodArr, n, totalWeight, way, bestWay, i + 1,
|
||||
currWeight, currValue, bestValue);
|
||||
}
|
||||
return bestValue;
|
||||
}
|
||||
int main() {
|
||||
Goods goodArr[] = { {1, 5}, {2, 6}, {3, 7}, {4, 8} };
|
||||
int totalWeight = 8;
|
||||
int n = sizeof(goodArr) / sizeof(goodArr[0]);
|
||||
int way[n], bestWay[n];
|
||||
int bestValue = 0;
|
||||
bestValue = dfs(goodArr, n, totalWeight, way, bestWay, 0, 0, 0, bestValue);
|
||||
printf("最大价值可为: %d\n", bestValue);
|
||||
printf("各包的取舍状态为:\n");
|
||||
for (int i = 0; i < n; i++)
|
||||
printf("重量为: %d 的: %d\n", goodArr[i].weight, bestWay[i]);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,55 @@
|
||||
#include <stdio.h>
|
||||
#define MAX_N 100 // 最大集装箱数量
|
||||
int n; // 集装箱数
|
||||
int boxArr[MAX_N]; // 集装箱重量数组
|
||||
int C1; // 第一艘轮船的载重量
|
||||
int currWeight = 0; // 当前载重量
|
||||
int bestWeight = 0; // 当前最优载重量
|
||||
int leftWeight = 0; // 剩余集装箱重量
|
||||
int x[MAX_N]; // 装入数组 当前解
|
||||
int bestx[MAX_N]; // 最佳装入数组 当前最优解
|
||||
void backtrace(int i) {// 回溯算法
|
||||
// 1. 到达叶节点
|
||||
if (i >= n) { // i此时的值=叶节点+1
|
||||
if (currWeight > bestWeight) {
|
||||
for (int j = 0; j < n; j++)
|
||||
bestx[j] = x[j];
|
||||
bestWeight = currWeight;
|
||||
}
|
||||
return;
|
||||
}
|
||||
leftWeight -= boxArr[i];
|
||||
// 2. 搜索左子树
|
||||
if (currWeight + boxArr[i] <= C1) { // x[i] = 1
|
||||
x[i] = 1;
|
||||
currWeight += boxArr[i];
|
||||
backtrace(i + 1);
|
||||
currWeight -= boxArr[i];
|
||||
}
|
||||
// 3. 搜索右子树
|
||||
if (currWeight + leftWeight > bestWeight) {
|
||||
x[i] = 0;
|
||||
backtrace(i + 1);
|
||||
}
|
||||
leftWeight += boxArr[i];
|
||||
}
|
||||
int main() {
|
||||
// 初始化集装箱信息
|
||||
int boxes[] = {4, 5, 6};
|
||||
n = sizeof(boxes) / sizeof(boxes[0]); // 货物数量
|
||||
C1 = 10; // 第一艘轮船的载重量
|
||||
// 设置集装箱重量
|
||||
for (int i = 0; i < n; i++) {
|
||||
boxArr[i] = boxes[i];
|
||||
leftWeight += boxArr[i]; // 计算剩余集装箱重量
|
||||
}
|
||||
// 调用回溯算法
|
||||
backtrace(0);
|
||||
// 输出最优方案
|
||||
printf("当前最优方案:\n");
|
||||
for (int i = 0; i < n; i++) {
|
||||
printf("%d: %d\n", boxArr[i], bestx[i]);
|
||||
}
|
||||
printf("最优解:%d\n", bestWeight);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,59 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MAX_N 100 // 最大集装箱数量
|
||||
int n; // 集装箱数
|
||||
int boxArr[MAX_N]; // 集装箱重量数组
|
||||
int C1; // 第一艘轮船的载重量
|
||||
int currWeight = 0; // 当前载重量
|
||||
int bestWeight = 0; // 当前最优载重量
|
||||
int bestx[MAX_N]; // 最佳装入数组 当前最优解
|
||||
int x[MAX_N]; // 装入数组 当前解
|
||||
|
||||
void backtrace(int i, int leftWeight) {//i集装箱索引,leftWeight剩余重量
|
||||
// 1. 到达叶节点
|
||||
if (i >= n) { // i此时的值=叶节点+1
|
||||
if (currWeight > bestWeight) {
|
||||
for (int j = 0; j < n; j++) // 记录当前最佳解
|
||||
bestx[j] = x[j];
|
||||
bestWeight = currWeight;
|
||||
}
|
||||
return;
|
||||
}
|
||||
// 剪枝1:如果当前背包重量加上剩余的重量都无法超过当前最优解,直接返回
|
||||
if (currWeight + leftWeight <= bestWeight) { return; }
|
||||
// 2. 搜索左子树:选择装入第i个集装箱
|
||||
if (currWeight + boxArr[i] <= C1) { // x[i] = 1
|
||||
x[i] = 1;
|
||||
currWeight += boxArr[i];
|
||||
backtrace(i + 1, leftWeight - boxArr[i]); // 递归时更新剩余重量
|
||||
currWeight -= boxArr[i]; // 回溯时撤销选择
|
||||
}
|
||||
// 3. 搜索右子树:不装入第i个集装箱
|
||||
// 剪枝2:如果当前背包容量加上剩余集装箱总重量仍然无法找到更好的解,返回
|
||||
if (currWeight + leftWeight > bestWeight) {
|
||||
x[i] = 0;
|
||||
backtrace(i + 1, leftWeight - boxArr[i]); // 递归时更新剩余重量
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// 初始化集装箱信息
|
||||
int boxes[] = {4, 5, 6};
|
||||
n = sizeof(boxes) / sizeof(boxes[0]); // 货物数量
|
||||
C1 = 10; // 第一艘轮船的载重量
|
||||
int totalWeight = 0;
|
||||
for (int i = 0; i < n; i++) {// 设置集装箱重量,并计算总重量
|
||||
boxArr[i] = boxes[i];
|
||||
totalWeight += boxArr[i];
|
||||
}
|
||||
// 调用回溯算法
|
||||
backtrace(0, totalWeight);
|
||||
// 输出最优方案
|
||||
printf("当前最优方案:\n");
|
||||
for (int i = 0; i < n; i++) {
|
||||
printf("%d: %d\n", boxArr[i], bestx[i]);
|
||||
}
|
||||
printf("最优解:%d\n", bestWeight);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,59 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
// 判断当前皇后放置是否合法
|
||||
int place(int x[], int j) {
|
||||
for (int i = 1; i < j; i++) {
|
||||
// 判断是否在同一列或者是否在同一对角线上
|
||||
if (x[i] == x[j] || abs(j - i) == abs(x[j] - x[i])) {
|
||||
return 0; // 不合法
|
||||
}
|
||||
}
|
||||
return 1; // 合法
|
||||
}
|
||||
// 非递归方式解 N 皇后问题
|
||||
void nQNoRec(int x[], int n) {
|
||||
int j = 1; // t 表示当前放置第 t 个皇后
|
||||
for (int i = 1; i <= n; i++)
|
||||
x[i] = 0;
|
||||
while (j >= 1) {
|
||||
while (x[j] < n) {
|
||||
x[j] = x[j] + 1;
|
||||
if (place(x, j)) {
|
||||
j = j + 1;
|
||||
if (j > n) { // 到达叶子节点
|
||||
for (int i = 1; i <= n; i++)
|
||||
printf("%d\t", x[i]);
|
||||
printf("\n");
|
||||
j = j - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
x[j] = 0; // 回溯时,恢复当前位置的状态
|
||||
j = j - 1; // 向上回溯
|
||||
}
|
||||
}
|
||||
|
||||
// 递归方式解 N 皇后问题
|
||||
void nQueenRec(int x[], int j, int n) {
|
||||
if (j > n) {// 搜索到叶子节点
|
||||
for (int i = 1; i <= n; i++)
|
||||
printf("%d\t", x[i]);
|
||||
printf("\n");
|
||||
} else {// 尝试在第 j 行放置皇后
|
||||
for (int i = 1; i <= n; i++) {
|
||||
x[j] = i;
|
||||
printf("i=%d,j=%d\n",i,j);
|
||||
if (place(x, j))
|
||||
nQueenRec(x, j + 1, n);
|
||||
}
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
int n = 6; // 皇后个数
|
||||
int x[n + 1]; // 存储每行皇后的位置
|
||||
nQueenRec(x, 1, n); // 递归方式解法
|
||||
printf("****************************\n");
|
||||
nQNoRec(x, n); // 非递归方式解法
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user