回溯法解决八皇后和0,1背包问题和排列问题
2013-05-01 11:21
363 查看
回溯法其实就是一种穷举算法
八皇后问题的代码:
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 <= 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; } }
相关文章推荐
- 八皇后问题使用排列的方法递归解决
- 回溯法解决0-1背包问题
- 回溯法解决0-1背包问题
- C语言用回溯解决八皇后问题
- C#WPF实现回溯算法解决八皇后问题
- 用回溯的思想解决排列问题
- 回溯问题+幂集、排列、子集和问题、八皇后问题
- 定义输入回溯法解决0-1背包问题
- 动态规划与回溯法解决0-1背包问题
- C# 用回溯递归解决“八皇后”问题
- 用回溯的思想解决排列问题
- 八皇后问题(DFS加回溯)输出排列的所有情况
- 回溯法解决0-1背包问题
- 回溯法解决0-1背包问题
- Java实现回溯法解决0-1背包问题
- 初次使用结构体解决学生成绩排列问题
- 八皇后问题(用排列来做)
- 贪心算法解决部分背包问题 在O(lgn)时间内
- 利用回溯的八皇后问题
- 动态规划解决背包问题