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

回溯法其实就是一种穷举算法

八皇后问题的代码:

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[n] <= capacity){
				array[n] = 1;
				dt(n+1,array,tempWeight + weight[n],tempValue + value[n]);
			}//在此回溯,每一个物品都有放置和不放置两种可能。先计算放置当前物品的可能下的一个解,然后再计算不放置当前物品的一个解
			array[n] = 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[n] = 1;
				limitedPermute(n + 1,array,r,mark);
				mark[n] = 0;
				limitedPermute(n + 1,array,r,mark);
			}
		}		
	}
	
	public int size(int[] array){
		int count = 0;
		for(int i: array){
			count += i;
		}
		return count;
	}
	

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值