您的位置:首页 > 其它

回溯法解决八皇后和0,1背包问题和排列问题

2013-05-01 11:21 363 查看
回溯法其实就是一种穷举算法
八皇后问题的代码:

public class EightQueuen {
int count = 0;
public boolean place(int[] array,int col){
boolean ret  = false;
if(col == 8){
for(int i = 0; i < array.length; i++){
System.out.println(i + "     " + array[i]);
}
System.out.println("######################");
count++;
return true;
}
int row = 0;
while(row < 8){
if(isSafe(col,row,array)){
array[col] = row;
if(!place(array,col+1)){//如果它的下一列摆放错误,则回溯,当前col放置的row的位置加1,再试
row++;
}
}else{
row++;
}
}
return ret;
}

public boolean isSafe(int col,int row,int[] array){
boolean ret = true;
for(int tempCol = 0; tempCol < col ; tempCol++){
int tempRow = array[tempCol];
if(tempRow == row){
return false;
}

if(tempCol + tempRow == row + col || tempCol - tempRow == col - row){
return false;
}

}
return ret;
}

/**
* @param args
*/
public static void main(String[] args) {
EightQueuen eq = new EightQueuen();
int[] array = new int[8];
eq.place(array, 0);
System.out.println(eq.count);
}

}


0,1背包问题:

public class Bag01 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Bag01 bag01 = new Bag01();
int[] array = new int[bag01.weight.length];
bag01.dt(0, array, 0, 0);
}

public int[] weight = {2,1,3,5,7,3,6,2,6,3};
public int[] value  = {5,2,3,9,3,6,5,7,8,2};
public int capacity = 10;
public int bestValue = 0;

public void dt(int n,int[] array,int tempWeight,int tempValue){
if(n >= weight.length ){
if(tempValue > bestValue){
bestValue = tempValue;
System.out.println("The best value is : " + bestValue);
}
for(int i = 0 ; i < array.length; i++){
System.out.print(array[i] + "  ");
}
System.out.println("################");
}else{
if(tempWeight + weight
<= capacity){
array
= 1;
dt(n+1,array,tempWeight + weight
,tempValue + value
);
}//在此回溯,每一个物品都有放置和不放置两种可能。先计算放置当前物品的可能下的一个解,然后再计算不放置当前物品的一个解
array
= 0;
dt(n+1,array,tempWeight,tempValue);
}
}
}


排列问题:

public class Permute {

/**
* @param args
*/

public void permute(int[] array,int start,int end){//指定数组,求该数组的全排列
if(start == end){
for(int i : array){
System.out.print(i + "  ");
}
System.out.println("");
}else{
for(int j = start; j <= end; j++){
swap(array,j,start);
permute(array,start+1,end);
swap(array,j,start);
}
}
}

public void swap(int[] array,int i ,int j){
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Permute p = new Permute();
int[] array = {1,2,3,4,5};
int[] mark = new int[5];
p.limitedPermute(0, array, 3, mark);
}

public void limitedPermute(int n,int[] array,int r,int[] mark){//制定数组,由该数组中数字组成的小于该数组长度的一个排列的所有集合
if(size(mark) == r){
for(int i : mark){
System.out.print(i + " ");
}
System.out.println();
return;
}else{
if(n <= array.length - 1){
mark
= 1;
limitedPermute(n + 1,array,r,mark);
mark
= 0;
limitedPermute(n + 1,array,r,mark);
}
}
}

public int size(int[] array){
int count = 0;
for(int i: array){
count += i;
}
return count;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: