作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在执行一项不知情的搜索任务,但我陷入困境。
我已经完成了 90% 的工作,但我正在尝试:
1) 从 .txt 文件而不是代码内加载数据(城市和英里)。2) 允许程序接受三个命令行输入参数:输入文件名、出发城市和目的地城市
示例:findroute inputFilename originCity DestinationCity
命令行示例:findroute input1.txt 慕尼黑柏林
这是我现在拥有的代码的一部分:
package graph;
import java.util.Formatter;
import java.util.List;
import bisearch.UniformCostSearch;
import search.Action;
public class findRoute {
/**
* finds the shortest path
*/
public static void main(final String[] args) {
Graph graph = findRoute.Map();
GraphStateSpaceSearchProblem sssp = new GraphStateSpaceSearchProblem(
graph, " Zerind ", " Oradea ");
bisearch.Search bisearch = new UniformCostSearch();
List<Action> actions = bisearch.search(sssp);
findRoute.printOutput(bisearch.nodesExplored(), actions);
}
/**
* prints the path found
*/
private static void printOutput(final int nodesExplored,
final List<Action> actions) {
double cost = 0;
for (final Action action : actions)
cost += action.cost();
System.out.println("Distance: " + new Formatter().format("%.2f", cost)+" m");
System.out.println ("Route: ");
for (final Action action : actions)
System.out.println(action);
}
/**
* creates a map of as a Graph
*/
private static Graph Map() {
final Graph graph = new Graph();
graph.addUndirectedEdge("Oradea", "Zerind", 71);
graph.addUndirectedEdge("Zerind", "Arad", 75);
graph.addUndirectedEdge("Arad", "Sibiu", 140);
graph.addUndirectedEdge("Sibiu", "Oradea", 151);
graph.addUndirectedEdge("Timisoara", "Arad", 118);
graph.addUndirectedEdge("Timisoara", "Lugoj", 111);
graph.addUndirectedEdge("Lugoj", "Mehadia", 70);
graph.addUndirectedEdge("Mehadia", "Dobreta", 75);
graph.addUndirectedEdge("Dobreta", "Craiova", 120);
graph.addUndirectedEdge("Sibiu", "Fagaras", 99);
graph.addUndirectedEdge("Fagaras", "Bucharest", 211);
graph.addUndirectedEdge("Sibiu", "Rimnicu Vilcea", 80);
graph.addUndirectedEdge("Pitesti", "Rimnicu Vilcea", 97);
graph.addUndirectedEdge("Craiova", "Rimnicu Vilcea", 146);
graph.addUndirectedEdge("Craiova", "Pitesti", 136);
graph.addUndirectedEdge("Pitesti", "Bucharest", 101);
graph.addUndirectedEdge("Bucharest", "Giurgiu", 90);
graph.addUndirectedEdge("Bucharest", "Urziceni", 85);
graph.addUndirectedEdge("Urziceni", "Hirsova", 98);
graph.addUndirectedEdge("Hirsova", "Eforie", 86);
graph.addUndirectedEdge("Urziceni", "Vaslui", 142);
graph.addUndirectedEdge("Vaslui", "Iasi", 92);
graph.addUndirectedEdge("Neamt", "Iasi", 87);
return graph;
}
}
我就是不知道该怎么做。任何帮助将不胜感激。提前致谢
最佳答案
您需要使用args[]。命令行参数通过 args[] 变量作为数组传递给 java 程序。
args[0] = inputFilename
args[1] = originCity
args[2] = DestinationCity
通过解析文件来填充 map 。其余代码应该按原样工作。
关于Java 最短成本路径 : Uninformed/Informed search from a (. txt) 文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9418545/
我正在执行一项不知情的搜索任务,但我陷入困境。 /image/d4n18.png 我已经完成了 90% 的工作,但我正在尝试: 1) 从 .txt 文件而不是代码内加载数据(城市和英里)。2) 允许程
我是一名优秀的程序员,十分优秀!