標籤:import name 沒有 indexof 執行 pack 功能 public char
package cn.xf.algorithm.ch03;import java.util.LinkedList;import org.apache.commons.lang.StringUtils;import org.junit.Test;/** * * 功能:窮舉 旅行商問題 * @author xiaofeng * @date 2017年4月26日 * @fileName TravelingSalesmanProblem.java * */public class TravelingSalesmanProblem {/** * 遞迴窮舉所有路徑,路徑可以但是和不對!!!,求高手指點???? * @param data * @param points * @param paths * @param count * @param pre * @param current */public static void getPathAndCount(int data[][], LinkedList<Character> points, String paths, String pathCount,int count, char pre, char current) {if (points.size() == 0) {System.out.println(paths.toString() + " count:" + pathCount.substring(0, pathCount.length() - 2) + "=" + count);} else {int len = points.size();String seq = "abcd";// 儲存當前路徑節點// char oldpre = new Character(current);pre = new Character(current);pathCount = new String(pathCount);for (int i = 0; i < len; ++i) {// 這個是淺拷貝LinkedList<Character> newPoints = (LinkedList<Character>) points.clone();// pre = current;current = new Character(newPoints.get(i));newPoints.remove(i);int index1 = seq.indexOf(pre);int index2 = seq.indexOf(current);String newPaths = new String(paths);if (pre != current) {newPaths += "->" + current;if (index1 != -1 && index2 != -1) {count += data[index1][index2];pathCount += data[index1][index2] + " + ";}}getPathAndCount(data, newPoints, newPaths, pathCount, count, pre, current);}}}/** * 換個說法,就是吧這個幾個城市的全排列枚舉出來 * @param data * @param points * @param paths * @param pathCount * @param count * @param pre * @param current */public static void getPathAndCount2(int data[][], LinkedList<Character> points, String paths) {if(points.size() == 0){String seq = "abcd";String resultPath[] = paths.split("->");//在這個時候進行統計資料和String countPath = "";int count = 0;String pre = resultPath[0];String cur;for(int j = 1; j < resultPath.length; ++j){if(!StringUtils.isEmpty(countPath)){//如果是空,那麼說明沒有加入資料countPath += " + " ;} //統計和路徑cur = resultPath[j];countPath += data[seq.indexOf(pre)][seq.indexOf(cur)];//求和count += data[seq.indexOf(pre)][seq.indexOf(cur)];pre = resultPath[j];}System.out.println(paths + " count:" + countPath + " = " + count);return;}int len = points.size();for(int i = 0; i < len; ++i){//遞迴到節點全部加入,這裡不能直接把原本的節點加入,應該是對一個新的鏈表操作,深拷貝LinkedList<Character> newPoints = (LinkedList<Character>) points.clone();String newPaths = new String(paths);Character cur = points.get(i);if(StringUtils.isNotEmpty(paths)){//如果路徑非空,說明已經有節點newPaths += "->" + cur;} else {newPaths += cur;}newPoints.remove(i);//移除當前加入路徑的節點getPathAndCount2(data, newPoints, newPaths);}}@Testpublic void testGetPathAndCount(){int data[][] = {{0,2,5,7},{2,0,8,3},{5,8,0,1},{7,3,1,0}};LinkedList<Character> points = new LinkedList<Character>();points.add(‘a‘);points.add(‘b‘);points.add(‘c‘);points.add(‘d‘);String paths = "";String pathCount = " ";int count = 0; char pre = ‘0‘; char current = ‘0‘;TravelingSalesmanProblem.getPathAndCount2(data, points, paths);//TravelingSalesmanProblem.getPathAndCount(data, points, paths, pathCount, count, pre, current);}public static void main(String[] args) {String seq = "abcd";int data[][] = {{0,2,5,7},{2,0,8,3},{5,8,0,1},{7,3,1,0}};System.out.println(seq.indexOf(‘a‘));}}
第二個方法執行結果:
【演算法設計與分析基礎】8、窮舉 旅行商問題