资源描述:
《罗马尼亚度假问题.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、二、详细代码测试类:/**Main类,打印各个算法的结果*@authordyl**/classMain{int result;int xiabiao[]=null;//访问的下标publicstaticvoid main(String[] args){Graph graph=newGraph();System.out.println("----------------罗马尼亚问题---------------");System.out.println("1、深度优先搜索");DFSdfs=new
2、DFS();dfs.DF_Search(graph,0,12);System.out.println("2、迭代加深的搜索");IDSids=new IDS();ids.IDS_Search(graph,0,12,15);//深度设15System.out.println("3、一致代价搜索");UCSucs=new UCS(graph,0,12);System.out.println("4、A*搜索");AXing aXing=newAXing();aXing.A_Search(graph, g
3、raph.H,0,15);//0-15即Arad到达Hirsova}/**打印*@paramg:图*@paramstack:栈*/publicvoid show(Graph g,Stack stack){if(stack.size()==0){System.out.println("路径搜索失败");return;}result=0;System.out.print("访问的下标:");for(int i =0; i < stack.size(); i++){System.out.print("-
4、->"+stack.get(i));}System.out.print("访问过程:");xiabiao=newint[stack.size()];if(stack.isEmpty()){System.out.println("搜索失败");}else{for(int i =0; i < stack.size(); i++){System.out.print("-->"+g.cities[(Integer) stack.get(i)]);}for(int i =0; i < stack.siz
5、e()-1; i++){result+=g.path[(Integer) stack.get(i)][(Integer) stack.get(i+1)];}System.out.println("总长度为:"+result+"");g.markInit();//清空访问}}}/**图类*@authordyl**/publicclassGraph{publicint path[][]=newint[][]{{0,75,10000,118,140,10000,10000,10000,10000
6、,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000},{75,0,71,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000},{10000,71,0,10000,151,10000,10000,10000,10000,10000,10000,10000,10000,1
7、0000,10000,10000,10000,10000,10000,10000},{118,10000,10000,0,10000,111,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000},{140,10000,151,10000,0,10000,80,99,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,
8、10000,10000},{10000,10000,10000,111,10000,0,10000,10000,70,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000},{10000,10000,10000,10000,80,10000,0,10000,10000,10000,146,97,10000,10000,10000,10000,10000,10000,10000,10000},{10000,