新聞中心
JAVA求10個(gè)景點(diǎn)間各個(gè)景點(diǎn)的最短路徑 圖隨便話 距離隨便 求代碼
最有效,切不復(fù)雜的方法使用Breadth First Search (BFS). 基本代碼如下(偽代碼)。因?yàn)锽FS不用遞歸,所以可能會(huì)有點(diǎn)難理解。
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比鄯善網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式鄯善網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋鄯善地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。
public Stack findPath(Vertex 起始景點(diǎn), Vertex 目標(biāo)景點(diǎn)){
Queue Vertex q = new QueueVertex();
s.enqueue(起始景點(diǎn));
Vertex 當(dāng)前位置;
while(!s.isEmpty()){
當(dāng)前位置 = s.dequeue();
if (當(dāng)前位置 == 目標(biāo)景點(diǎn)) break;
for (每一個(gè)相鄰于 當(dāng)前位置 的景點(diǎn) Vertex v){
if (!v.visited){
v.parent = 當(dāng)前位置;
// 不是規(guī)定,不過可以節(jié)省一點(diǎn)時(shí)間
if (v == 目標(biāo)景點(diǎn)){
current = v;
break;
}
s.enqueue(Vertex v);
v.visited = true;
}
}
}
Stack Vertex solution = new Stack Vertex();
Vertex parent = current;
while (parent != 起始景點(diǎn)){
solution.push(parent);
parent = current.parent;
}
for (graph中的每一個(gè)vertex) vertex.visited = false;
return solution(); // 其實(shí)這里建議用一個(gè) Path 的inner class 來裝所獲得的路線
}
然后再 main 求每?jī)蓚€(gè)景點(diǎn)之間的距離即可
public static void main(String[] argv){
PathFinder pf = new PathFinder();
Stack[][] 路徑 = new Stack[10][10];
for(int i=0; ipf.vertices.length; i++){
for(int j=i+1; jpf.vertices.length; j++){
Stack s = pf.findPath(pf.vertices[i], pf.vertices[j]);
路徑[i][j] = s; 路徑[j][i] = s; // 假設(shè)你的graph是一個(gè)undirected graph
}
}
// 這么一來就大功告成了!對(duì)于每?jī)蓚€(gè)景點(diǎn)n 與 m之間的最短路徑就是在 stack[n][m] 中
}
還有一種方法就是用Depth First Search遞歸式的尋找路徑,不過這樣比較慢,而且我的代碼可能會(huì)造成stack overflow
public Stack dfs(Vertex 當(dāng)前景點(diǎn),Vertex 目標(biāo)景點(diǎn)){
if(當(dāng)前景點(diǎn) == 目標(biāo)景點(diǎn)) return;
Stack solution = new Stack();
Stack temp;
for (相鄰于 點(diǎn)錢景點(diǎn) 的每一個(gè) Vertex v){
if (!v.visited){
v.visited = true;
temp = dfs(v, 目標(biāo)景點(diǎn));
// 抱歉,不記得是stack.size()還是stack.length()
if (solution.size() == 0) solution = temp;
else if(temp.size() solution.size()) solution = temp;
v.visited = false; 復(fù)原
}
}
return solution;
}
然后再在上述的Main中叫dfs...
參考:
java用節(jié)點(diǎn)存進(jìn)一條公交線路,輸入任意兩個(gè)站點(diǎn)輸出路線
//寫了兩個(gè)小時(shí)啊,兄弟,要采納我啊
//Site(站點(diǎn)類)
package?transit;
import?java.util.ArrayList;
import?java.util.List;
public?class?Site
{
private?Integer?id;?//?給每個(gè)站點(diǎn)分配一個(gè)ID
private?String?name;?//?站點(diǎn)的名字
private?ListRoute?list;?//?經(jīng)過該站點(diǎn)的線路
public?Site()
{
super();
}
public?Site(Integer?id,?String?name)
{
super();
this.id?=?id;
this.name?=?name;
this.list?=?new?ArrayListRoute();
}
public?Integer?getId()
{
return?id;
}
public?void?setId(Integer?id)
{
this.id?=?id;
}
public?String?getName()
{
return?name;
}
public?void?setName(String?name)
{
this.name?=?name;
}
public?ListRoute?getList()
{
return?list;
}
public?void?setList(ListRoute?list)
{
this.list?=?list;
}
//?添加線路
public?boolean?addRoute(Route?route)
{
return?this.getList().add(route);
}
//?刪除線路
public?boolean?removeRoute(Route?route)
{
return?this.getList().remove(route);
}
@Override
public?String?toString()
{
return?name;
}
@Override
public?boolean?equals(Object?obj)
{
if?(obj?instanceof?Site)
{
Site?s?=?(Site)?obj;
return?this.id?==?s.getId()??this.name.equals(s.getName());
}
return?false;
}
}
JAVA中怎么實(shí)現(xiàn)查詢 代碼
try{Connection con;
Statement stmt;
ResultSet rs;
int temp;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/java","root","");//以上是數(shù)據(jù)庫連接,不同的數(shù)據(jù)管理器有 //不同的驅(qū)動(dòng)和鏈接方式,以上是mysql的連接
stmt=con.createStatement();
rs=stmt.executeQuery("select * from student");//執(zhí)行查詢語句,結(jié)果賦值給結(jié)果集rs
//結(jié)果集是結(jié)果于字段編號(hào)的映射,每一個(gè)字
//段都有一個(gè)編號(hào),最小為1,也就是第一個(gè)字段
while(rs.next()){
String names=rs.getString("name");//查詢結(jié)果轉(zhuǎn)換成字符串。
System.out.println(names);
}rs.close();
}catch(Exception e){
e.printStackTrace();
}
網(wǎng)頁題目:java查詢路線代碼,java路徑查詢
本文地址:http://fisionsoft.com.cn/article/dsipeis.html