递归
回溯
public class WolfQueen {
int max = 8;
int[] array = new int[max];
public static void main(String[] args) {
new WolfQueen().check(0);
}
private void check(int n) {
if (n == max) {
print();
return;
}
for (int i = 0; i < max; i++) {
array[n] = i;
if (judge(n)) {
check(n + 1);
}
}
}
private boolean judge(int n) {
for (int i = 0; i < n; i++) {
if (array[i] == array[n] || Math.abs(n - i) == Math.abs(array[n] - array[i])) {
return false;
}
}
return true;
}
private void print() {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + 1 + " ");
}
System.out.println();
}
}
public class Zero_One {
private static int[] p;
private static int[] w;
private static int c;
private static int count;
private static int cw;
private static int cp;
static int bestp;
private static int r;
private static int[] cx;
private static int[] bestx;
public static int Loading(int[] ww,int[] pp, int cc) {
count = ww.length - 1;
w = ww;
p = pp;
c = cc;
cw = 0;
bestp = 0;
cx = new int[count+1];
bestx = new int [count+1];
for(int i = 1;i<=count;i++) {
r += p[i];
}
BackTrack(1);
return bestp;
}
public static void BackTrack(int t) {
if(t>count) {
if(cp>bestp) {
for(int i = 1;i<=count;i++) {
bestx[i] = cx[i];
}
bestp = cp;
}
return;
}
r -= p[t];
if(cw + w[t] <= c) {
cx[t] = 1;
cp += p[t];
cw += w[t];
BackTrack(t+1);
cp -= p[t];
cw -= w[t];
}
if(cp + r >bestp) {
cx[t] = 0;
BackTrack(t+1);
}
r += p[t];
}
public static void main(String[] args) {
int[] w1 = {0,15,25,40,20,15,24};
int[] p1 = {0,10,5,20,2,14,23};
int c1 = 30;
Loading(w1,p1,c1);
System.out.println("最优装载为:" + bestp);
for(int i =1;i<=count;i++) {
System.out.print(bestx[i] + " ");
}
}
分治
public class Main{
public long count = 0;
public void getReverseCount(int[] A) {
if(A.length > 1) {
int[] leftA = getHalfArray(A, 0);
int[] rightA = getHalfArray(A, 1);
getReverseCount(leftA);
getReverseCount(rightA);
mergeArray(A, leftA, rightA);
}
}
public int[] getHalfArray(int[] A, int judge) {
int[] result;
if(judge == 0) {
result = new int[A.length / 2];
for(int i = 0;i < A.length / 2;i++)
result[i] = A[i];
} else {
result= new int[A.length - A.length / 2];
for(int i = 0;i < A.length - A.length / 2;i++)
result[i] = A[A.length / 2 + i];
}
return result;
}
public void mergeArray(int[] A, int[] leftA, int[] rightA) {
int len = 0;
int i = 0;
int j = 0;
int lenL = leftA.length;
int lenR = rightA.length;
while(i < lenL && j < lenR) {
if(leftA[i] > rightA[j]) {
A[len++] = rightA[j++];
count += (lenL - i);
} else {
A[len++] = leftA[i++];
}
}
while(i < lenL)
A[len++] = leftA[i++];
while(j < lenR)
A[len++] = rightA[j++];
}
public int[] getRandomArray(int n) {
int[] result = new int[n];
for(int i = 0;i < n;i++) {
result[i] = (int)( Math.random() * 50);
}
return result;
}
public static void main(String[] args){
long t1 = System.currentTimeMillis();
Main test = new Main();
int[] A = test.getRandomArray(50000);
test.getReverseCount(A);
long t2 = System.currentTimeMillis();
System.out.println("分治法得到结果:"+test.count+", 耗时:"+(t2 - t1)+"毫秒");
}
}
动态规划
public class Package_01 {
public static int maxValue(int[] weight, int[] value, int capacity) {
int weightLen = weight.length;
int valueLen = capacity + 1;
int maxValue = 0;
int[][] v = new int[weightLen][valueLen];
for (int i = 0; i < weightLen; i++) {
for (int j = 0; j < valueLen; j++) {
if (i == 0) {
v[i][j] = 0;
} else if (j == 0) {
v[i][j] = 0;
} else {
if (weight[i] > j) {
v[i][j] = v[i - 1][j];
} else if (weight[i] <= j) {
v[i][j] = Math.max(v[i - 1][j], v[i - 1][j - weight[i]] + value[i]);
}
maxValue = v[i][j];
}
}
}
return maxValue;
}
}
- 最小路径和(详细可看 Minimum Path Sum)
- 编程实现莱文斯坦最短编辑距离
- 编程实现查找两个字符串的最长公共子序列
- 编程实现一个数据序列的最长递增子序列