Tag:import name no indexof execute pack function public char
Package Cn.xf.algorithm.ch03;import Java.util.linkedlist;import Org.apache.commons.lang.stringutils;import org.junit.test;/** * Features: Poor lift travel business questions * @author Xiaofeng * @date April 26, 2017 * @fileName Travelingsalesmanproblem.java * */PU Blic class Travelingsalesmanproblem {/** * recursive exhaustive all paths, path can but and not!!! , seek expert guidance???? * @param data * @param points * @param paths * @param count * @param pre * @param current */public static void Getpathandc Ount (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";//Save current PATH nodes//char oldpre = new Character (current);p re = new Character;p athcount = new String (Pathcount); for (int i = 0; i < len; ++i) {//This is a shallow copy 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];p Athcount + = Data[index1][index2] + "+";}} Getpathandcount (data, newpoints, Newpaths, Pathcount, Count, pre, current);}}} /** * In other words, this is the whole arrangement of the cities enumerated * @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 ("-a");//At this time statistical data and string countpath = ""; int count = 0; String pre = resultpath[0]; String cur;for (int j = 1; j < Resultpath.length; ++j) {if (! Stringutils.isempty (Countpath)) {//If empty, then the description does not include data Countpath + + ";} Statistics and path cur = resultpath[j];countpath + = Data[seq.indexof (pre)][seq.indexof (cur)];//sum count + = Data[seq.indexof (pre)][seq.indexof (cur)];p re = Resultpath[j];} System.out.println (Paths + "Count:" + countpath + "=" + count); return;} int len = Points.size (); for (int i = 0; i < len; ++i) {//Recursive to node all join, here can not directly add the original node, should be to a new list operation, deep copy linkedlist< Character> newpoints = (linkedlist<character>) points.clone (); String newpaths = new string (paths); Character cur = points.get (i), if (Stringutils.isnotempty (Paths)) {//If the path is not empty, the node is already newpaths + = "+ cur;} else { Newpaths + = cur;} Newpoints.remove (i);//Remove node GetPathAndCount2 (data, newpoints, newpaths) of the current join path;}} @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> ();p oints.add (' a ');p oints.add (' B ');p Oints.add ( ' C ');p Oints.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 '));}}
The second method executes the result:
"Fundamentals of Algorithmic Design and analysis" 8, the problem of poor lift travel business