- 组合问题
package Retrospective;
import java.util.*;
public class Combination {
static LinkedList<Integer> list = new LinkedList<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int[] arr = Arrays.stream(s.split(" ")).mapToInt(Integer::parseInt).toArray();
int n = sc.nextInt();
Arrays.sort(arr);
combination(n, arr);
}
static void combination(int n, int[] arr) {
backtracking(n, arr, 0, 0);
}
static void backtracking(int n, int[] arr, int startIndex, int sum) {
if (sum == n) {
for (Integer list : list) {
System.out.print(list + " ");
}
System.out.println();
return;
}
if (sum > n) return;
for (int i = startIndex; i < arr.length; i++) {
list.add(arr[i]);
sum += arr[i];
backtracking(n, arr, i + 1, sum);
list.removeLast();
sum -= arr[i];
}
}
}
- N皇后问题
package Retrospective;
import java.util.Scanner;
public class NQueen {
static int num = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int queenNumber = sc.nextInt();
nQueen(queenNumber);
}
static void nQueen(int queenNumber) {
boolean[][] flag = new boolean[queenNumber][queenNumber];
backtracking(queenNumber, 0, flag);
System.out.println(num);
}
static void backtracking(int n, int row, boolean[][] flag) {
if (row == n) {
num++;
return;
}
for (int col = 0; col < n; col++) {
if (checkValid(row, col, n, flag)) {
flag[row][col] = true;
backtracking(n, row + 1, flag);
flag[row][col] = false;
}
}
}
static boolean checkValid(int row, int col, int n, boolean[][] flag) {
for (int i = 0; i < row; i++) {
if (flag[i][col]) return false;
}
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (flag[i][j]) return false;
}
for (int i = row - 1, j = col + 1; i >= 0 && j <= n - 1; i--, j++) {
if (flag[i][j]) return false;
}
return true;
}
}
- 分支限界法解决背包问题
package BranchAndLimit;
import java.util.*;
public class pack {
private int capacity;
private int number;
public BinaryTree binaryTree;
public void initialization() {
Node head = new Node();
head.setWeight(0);
head.setValue(0);
head.setLayer(0);
head.setCurrentSolution("");
binaryTree.getItemInfoList().sort((o1, o2) -> {
Double a = (double) (o1.getValue() / o1.getWeight());
Double b = (double) (o2.getValue() / o2.getWeight());
return b.compareTo(a);
});
ItemInfo firstNode = binaryTree.getItemInfoList().get(0);
head.setUp(capacity * (firstNode.getValue() * 1.0 / firstNode.getWeight()));
binaryTree.setBackpack(this);
binaryTree.createdBinaryTree(head, 0);
binaryTree.branchBound(head);
}
public void input() {
Scanner scanner = new Scanner(System.in);
capacity = scanner.nextInt();
number = scanner.nextInt();
binaryTree.setItemInfoList(new ArrayList<>(number));
for (int i = 0; i < number; i++) {
int weight = scanner.nextInt();
int value = scanner.nextInt();
ItemInfo itemInfo = new ItemInfo(weight, value);
binaryTree.getItemInfoList().add(itemInfo);
}
}
public int getCapacity() {
return capacity;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public static void main(String[] args) {
pack backpack = new pack();
backpack.binaryTree = new BinaryTree();
backpack.input();
backpack.initialization();
System.out.println(backpack.binaryTree.getMax());
String bestSolution = backpack.binaryTree.getBestSolution();
for (int i = 0; i < bestSolution.length(); i++) {
if (bestSolution.charAt(i) != '0') {
ItemInfo itemInfo = backpack.binaryTree.getItemInfoList().get(i);
System.out.println("[" + itemInfo.getWeight() + " " + itemInfo.getValue() + "]");
}
}
}
}
class ItemInfo {
private int weight;
private int value;
public ItemInfo() {
}
public ItemInfo(int weight, int value) {
this.weight = weight;
this.value = value;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
class Node extends ItemInfo implements Comparable {
private int layer;
private String currentSolution;
private double up;
private Node left;
private Node right;
public Node() {
}
public int getLayer() {
return layer;
}
public void setLayer(int layer) {
this.layer = layer;
}
public String getCurrentSolution() {
return currentSolution;
}
public void setCurrentSolution(String currentSolution) {
this.currentSolution = currentSolution;
}
public double getUp() {
return up;
}
public void setUp(double up) {
this.up = up;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public int compareTo(Object o) {
Node node = (Node) o;
if (node.getUp() > this.getUp()) {
return 1;
} else if (node.getUp() == this.getUp()) {
return 0;
}
return -1;
}
}
class BinaryTree {
private List<ItemInfo> itemInfoList;
private String bestSolution;
private int max;
private pack backpack;
private void judgementNode(Node node, Queue<Node> upPriorityQueue) {
if (node == null) {
return;
}
if (node.getWeight() > backpack.getCapacity() || node.getUp() < max) {
return;
}
if (node.getLayer() < backpack.getNumber()) {
upPriorityQueue.add(node);
} else {
max = node.getValue();
bestSolution = node.getCurrentSolution();
}
}
public void branchBound(Node node) {
Queue<Node> upPriorityQueue = new PriorityQueue<>();
upPriorityQueue.add(node);
while (!upPriorityQueue.isEmpty()) {
node = upPriorityQueue.poll();
if (node.getUp() <= max) {
break;
}
judgementNode(node.getLeft(), upPriorityQueue);
judgementNode(node.getRight(), upPriorityQueue);
}
}
public void createdBinaryTree(Node node, int index) {
if (index >= backpack.getNumber()) {
return;
}
ItemInfo currentItem = itemInfoList.get(index);
if (node.getLeft() == null) {
Node leftNode = new Node();
leftNode.setValue(currentItem.getValue() + node.getValue());
leftNode.setWeight(currentItem.getWeight() + node.getWeight());
leftNode.setLayer(index + 1);
int surplus = backpack.getCapacity() - leftNode.getWeight();
if (surplus >= 0) {
if (index + 1 < itemInfoList.size()) {
ItemInfo nextNode = itemInfoList.get(index + 1);
int up = surplus * (nextNode.getValue() / nextNode.getWeight());
if (index == 0) {
leftNode.setUp(up + currentItem.getValue());
} else {
leftNode.setUp(up + leftNode.getValue());
}
} else {
leftNode.setUp(node.getValue());
}
leftNode.setCurrentSolution(node.getCurrentSolution() + 1);
node.setLeft(leftNode);
createdBinaryTree(node.getLeft(), index + 1);
}
}
if (node.getRight() == null) {
Node rightNode = new Node();
rightNode.setValue(node.getValue());
rightNode.setWeight(node.getWeight());
rightNode.setLayer(index + 1);
int surplus = backpack.getCapacity() - rightNode.getWeight();
if (surplus >= 0) {
if (index + 1 < itemInfoList.size()) {
ItemInfo nextNode = itemInfoList.get(index + 1);
int up = surplus * (nextNode.getValue() / nextNode.getWeight());
if (index == 0) {
rightNode.setUp(up);
} else {
rightNode.setUp(up + rightNode.getValue());
}
} else {
rightNode.setUp(node.getValue());
}
rightNode.setCurrentSolution(node.getCurrentSolution() + 0);
node.setRight(rightNode);
createdBinaryTree(node.getRight(), index + 1);
}
}
}
public void preOrderBinaryTree(Node node) {
if (node != null) {
System.out.println(node.toString());
preOrderBinaryTree(node.getLeft());
preOrderBinaryTree(node.getRight());
}
}
public List<ItemInfo> getItemInfoList() {
return itemInfoList;
}
public void setItemInfoList(List<ItemInfo> itemInfoList) {
this.itemInfoList = itemInfoList;
}
public String getBestSolution() {
return bestSolution;
}
public void setBestSolution(String bestSolution) {
this.bestSolution = bestSolution;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public pack getBackpack() {
return backpack;
}
public void setBackpack(pack backpack) {
this.backpack = backpack;
}
}
- 作业调度问题
package BranchAndLimit;
import java.util.Scanner;
public class OperationScheduling {
static int minTime = Integer.MAX_VALUE;
static int n;
static int machine;
static int[] time;
static int[] everyOperationTime;
public static void main(String[] args) {
input();
backTracking(0, n, machine, time, everyOperationTime);
System.out.println(minTime);
}
static void backTracking(int next, int n, int machine, int[] time, int[] eveOpeTime) {
if (next == n) {
int maxTime = solve(machine, eveOpeTime);
if (maxTime < minTime) minTime = maxTime;
} else {
for (int i = 0; i < machine; i++) {
eveOpeTime[i] = eveOpeTime[i] + time[next];
if (eveOpeTime[i] < minTime) backTracking(next + 1, n, machine, time, eveOpeTime);
eveOpeTime[i] = eveOpeTime[i] - time[next];
}
}
}
static int solve(int machine, int[] eveOpeTime) {
int maxTime = 0;
for (int i = 0; i < machine; i++) {
if (eveOpeTime[i] > maxTime) maxTime = eveOpeTime[i];
}
return maxTime;
}
static void input() {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
machine = sc.nextInt();
time = new int[n];
everyOperationTime = new int[n];
for (int i = 0; i < n; i++) {
time[i] = sc.nextInt();
everyOperationTime[i] = 0;
}
}
}