所花时间:5小时
代码量:154
博客篇:1
地铁起点到终点的最短路径查询:
使用广度优先遍历,当要访问该站点时先储存在队列,最后出队形式访问
每次访问传入数据:
name:站点名;
now:之前访问的站点字符串总和;
nowline:当前站点的线路;
ed:要到达的站点名称;
i:当前经过站数以及字符串的数组下标;
s:当前经过站点数
此外全局变量:
line[]:所有遍历线路的经过站点字符串数组;
though[]:是否访问过此点;
arrive[]:此线是否到达终点;
sum[]:所有线经过站点数的数组;
核心方法:
public void loops(String name,String now,String nowline,String ed,int i,int s) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql1 = "select * from subway where name = '" + name + "' and linename = '" +nowline+ "'"; ResultSet rs1 = stmt.executeQuery(sql1); rs1.next(); Subway subway = new Subway(); subway.setName(rs1.getString("name")); subway.setLinename(rs1.getString("linename")); subway.setTransfer(rs1.getString("transfer")); subway.setPre(rs1.getString("pre")); subway.setNext(rs1.getString("next")); stmt.close(); conn.close(); if(through[getID(subway.getName())-1]){ return; } visit(subway.getName()); if(i==2) System.out.println(subway.getName()); now+="->"+subway.getName(); sum[i]=s+1; s=sum[i]; line[i]=now; System.out.println(subway.getName()); if(subway.getName().equals(ed)){ arrive[i]=true; } else if(subway.getName().equals("东直门")||subway.getName().equals("宋家庄")||subway.getName().equals("苹果园")||subway.getName().equals("金安桥")){ if(!subway.getNext().equals("空")) { queue.enter(subway.getNext(), now, nowline, ed, i,s); } if(!subway.getPre().equals("空")) { queue.enter(subway.getPre(), now, nowline, ed, i,s); } Subway sw[]=getOthers(name,nowline); nowline = sw[0].getLinename(); now+="换"+nowline; while (sum[i]!=0){ i++; } if(!sw[0].getNext().equals("空")){ queue.enter(sw[0].getNext(),now,nowline,ed,i,s); } if(!sw[0].getPre().equals("空")){ queue.enter(sw[0].getPre(),now,nowline,ed,i,s); } nowline = sw[1].getLinename(); now+="换"+nowline; while (sum[i]!=0){ i++; } if(!sw[1].getNext().equals("空")){ queue.enter(sw[1].getNext(),now,nowline,ed,i,s); } if(!sw[1].getPre().equals("空")){ queue.enter(sw[1].getPre(),now,nowline,ed,i,s); } } else { if(!subway.getNext().equals("空")){ queue.enter(subway.getNext(),now,nowline,ed,i,s); } if(!subway.getPre().equals("空")) { queue.enter(subway.getPre(), now, nowline, ed, i,s); } if(subway.getTransfer().equals("1")){ subway = getOther(nowline,name); nowline = subway.getLinename(); now+="("+"换"+nowline+")"; while (sum[i]!=0){ i++; } if(!subway.getNext().equals("空")){ queue.enter(subway.getNext(),now,nowline,ed,i,s); } if(!subway.getPre().equals("空")) { queue.enter(subway.getPre(), now, nowline, ed, i,s); } } } while (!queue.isNull()){ Qnode qnode =new Qnode(); qnode=queue.delete(); loops(qnode.getName(),qnode.getNow(),qnode.getNowline(),qnode.getEd(),qnode.getI(),qnode.getS()); } }
标签:nowline,ed,equals,queue,subway,3.31,now From: https://www.cnblogs.com/chukjbgg333/p/18106954