地铁换乘系统
雙人合作項目,合作伙伴:https://www.cnblogs.com/NCLONG/
package Dao;import java.util.HashMap; import java.util.LinkedHashSet; import java.util.Map; public class Station {private String name; //地鐵站名稱,假設具備唯一性public Station prev; //本站在lineNo線上面的前一個站public Station next; //本站在lineNo線上面的后一個站//本站到某一個目標站(key)所經(jīng)過的所有站集合(value),保持前后順序private Map<Station,LinkedHashSet<Station>> orderSetMap = new HashMap<Station,LinkedHashSet<Station>>();public Station (String name){this.name = name;}public Station () {// TODO Auto-generated constructor stub }public String getName() {return name;}public void setName(String name) {this.name = name;}public LinkedHashSet<Station> getAllPassedStations(Station station) {if(orderSetMap.get(station) == null){LinkedHashSet<Station> set = new LinkedHashSet<Station>();set.add(this);orderSetMap.put(station, set);}return orderSetMap.get(station);}public Map<Station, LinkedHashSet<Station>> getOrderSetMap() {return orderSetMap;}@Overridepublic boolean equals(Object obj) {if(this == obj){return true;} else if(obj instanceof Station){Station s = (Station) obj;if(s.getName().equals(this.getName())){return true;} else {return false;}} else {return false;}}@Overridepublic int hashCode() {return this.getName().hashCode();} } package Dao;import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; public class Subway {private List<Station> outList = new ArrayList<Station>();//記錄已經(jīng)分析過的站點//計算從s1站到s2站的最短經(jīng)過路徑public void calculate(Station s1,Station s2){if(outList.size() == Subwayline.totalStaion){System.out.println("找到目標站點:"+s2.getName()+",共經(jīng)過"+(s1.getAllPassedStations(s2).size()-1)+"站");for(Station station : s1.getAllPassedStations(s2)){System.out.print(station.getName()+"->");}return;}if(!outList.contains(s1)){outList.add(s1);}//如果起點站的OrderSetMap為空,則第一次用起點站的前后站點初始化之if(s1.getOrderSetMap().isEmpty()){List<Station> Linkedstations = getAllLinkedStations(s1);for(Station s : Linkedstations){s1.getAllPassedStations(s).add(s);}}Station parent = getShortestPath(s1);//獲取距離起點站s1最近的一個站(有多個的話,隨意取一個)if(parent == s2){System.out.println("找到目標站點:"+s2+",共經(jīng)過"+(s1.getAllPassedStations(s2).size()-1)+"站");for(Station station : s1.getAllPassedStations(s2)){System.out.print(station.getName()+"->");}return;}for(Station child : getAllLinkedStations(parent)){if(outList.contains(child)){continue;}int shortestPath = (s1.getAllPassedStations(parent).size()-1) + 1;//前面這個1表示計算路徑需要去除自身站點,后面這個1表示增加了1站距離if(s1.getAllPassedStations(child).contains(child)){//如果s1已經(jīng)計算過到此child的經(jīng)過距離,那么比較出最小的距離if((s1.getAllPassedStations(child).size()-1) > shortestPath){//重置S1到周圍各站的最小路徑 s1.getAllPassedStations(child).clear();s1.getAllPassedStations(child).addAll(s1.getAllPassedStations(parent));s1.getAllPassedStations(child).add(child);}} else {//如果s1還沒有計算過到此child的經(jīng)過距離 s1.getAllPassedStations(child).addAll(s1.getAllPassedStations(parent));s1.getAllPassedStations(child).add(child);}}outList.add(parent);calculate(s1,s2);//重復計算,往外面站點擴展 }//取參數(shù)station到各個站的最短距離,相隔1站,距離為1,依次類推private Station getShortestPath(Station station){int minPatn = Integer.MAX_VALUE;Station rets = null;for(Station s :station.getOrderSetMap().keySet()){if(outList.contains(s)){continue;}LinkedHashSet<Station> set = station.getAllPassedStations(s);//參數(shù)station到s所經(jīng)過的所有站點的集合if(set.size() < minPatn){minPatn = set.size();rets = s;}}return rets;}//獲取參數(shù)station直接相連的所有站,包括交叉線上面的站private List<Station> getAllLinkedStations(Station station){List<Station> linkedStaions = new ArrayList<Station>();for(List<Station> line : Subwayline.lineSet){if(line.contains(station)){//如果某一條線包含了此站,注意由于重寫了hashcode方法,只有name相同,即認為是同一個對象Station s = line.get(line.indexOf(station));if(s.prev != null){linkedStaions.add(s.prev);}if(s.next != null){linkedStaions.add(s.next);}}}return linkedStaions;}public static void main(String[] args) {long t1 = System.currentTimeMillis();Subway sw = new Subway();sw.calculate(new Station("北海山"), new Station("北豆"));long t2 = System.currentTimeMillis();System.out.println();System.out.println("耗時:"+(t2-t1)+"ms");} } package Dao;import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import Util.JDBUtil; import java.sql.*; import java.util.ArrayList; import java.util.List;public class Subwayline {public static List<Station> line1 = new ArrayList<Station>();//1號線public static List<Station> line2 = new ArrayList<Station>();//2號線public static List<Station> line3 = new ArrayList<Station>();//3號線public static List<Station> line4 = new ArrayList<Station>();//10號線public static List<Station> line5 = new ArrayList<Station>();//s1號線public static List<Station> line6 = new ArrayList<Station>();//s8號線public static Set<List<Station>> lineSet = new HashSet<List<Station>>();//所有線集合public static int totalStaion = 0;//總的站點數(shù)量static Connection conn;static PreparedStatement ps = null;static ResultSet rs;static String sql = "select * from line1";//1號線public static List<Station> load1(){conn=JDBUtil.getConn();ps=null;ResultSet rs=null;String id;sql="select name from line1 "; // sql="select * from station where Id between ? and ? order by Id";List<Station> users=new ArrayList<Station>();Station user=null;try {ps=conn.prepareStatement(sql);rs=ps.executeQuery();while(rs.next()) {user=new Station();user.setName(rs.getString("name"));users.add(user);}}catch(SQLException e) {e.printStackTrace();}finally {try {if(ps!=null)ps.close();if(conn!=null)conn.close();}catch(Exception e2) {e2.printStackTrace();}}return users;}public static List<Station> load2(){conn=JDBUtil.getConn();ps=null;ResultSet rs=null;String id;sql="select name from line2 "; // sql="select * from station where Id between ? and ? order by Id";List<Station> users=new ArrayList<Station>();Station user=null;try {ps=conn.prepareStatement(sql);rs=ps.executeQuery();while(rs.next()) {user=new Station();user.setName(rs.getString("name"));users.add(user);}}catch(SQLException e) {e.printStackTrace();}finally {try {if(ps!=null)ps.close();if(conn!=null)conn.close();}catch(Exception e2) {e2.printStackTrace();}}return users;}public static List<Station> load3(){conn=JDBUtil.getConn();ps=null;ResultSet rs=null;String id;sql="select name from line3 "; // sql="select * from station where Id between ? and ? order by Id";List<Station> users=new ArrayList<Station>();Station user=null;try {ps=conn.prepareStatement(sql);rs=ps.executeQuery();while(rs.next()) {user=new Station();user.setName(rs.getString("name"));users.add(user);}}catch(SQLException e) {e.printStackTrace();}finally {try {if(ps!=null)ps.close();if(conn!=null)conn.close();}catch(Exception e2) {e2.printStackTrace();}}return users;}public static List<Station> load4(){conn=JDBUtil.getConn();ps=null;ResultSet rs=null;String id;sql="select name from line4 "; // sql="select * from station where Id between ? and ? order by Id";List<Station> users=new ArrayList<Station>();Station user=null;try {ps=conn.prepareStatement(sql);rs=ps.executeQuery();while(rs.next()) {user=new Station();user.setName(rs.getString("name"));users.add(user);}}catch(SQLException e) {e.printStackTrace();}finally {try {if(ps!=null)ps.close();if(conn!=null)conn.close();}catch(Exception e2) {e2.printStackTrace();}}return users;}public static List<Station> load5(){conn=JDBUtil.getConn();ps=null;ResultSet rs=null;String id;sql="select name from line5 "; // sql="select * from station where Id between ? and ? order by Id";List<Station> users=new ArrayList<Station>();Station user=null;try {ps=conn.prepareStatement(sql);rs=ps.executeQuery();while(rs.next()) {user=new Station();user.setName(rs.getString("name"));users.add(user);}}catch(SQLException e) {e.printStackTrace();}finally {try {if(ps!=null)ps.close();if(conn!=null)conn.close();}catch(Exception e2) {e2.printStackTrace();}}return users;}public static List<Station> load6(){conn=JDBUtil.getConn();ps=null;ResultSet rs=null;String id;sql="select name from line6 "; // sql="select * from station where Id between ? and ? order by Id";List<Station> users=new ArrayList<Station>();Station user=null;try {ps=conn.prepareStatement(sql);rs=ps.executeQuery();while(rs.next()) {user=new Station();user.setName(rs.getString("name"));users.add(user);}}catch(SQLException e) {e.printStackTrace();}finally {try {if(ps!=null)ps.close();if(conn!=null)conn.close();}catch(Exception e2) {e2.printStackTrace();}}return users;}static {line1=load1();line2=load2();line3=load3();line4=load4();line5=load5();line6=load6();lineSet.add(line1);lineSet.add(line2);lineSet.add(line3);lineSet.add(line4);lineSet.add(line5);lineSet.add(line6);totalStaion = line1.size() + line2.size() + line3.size() + line4.size() + line5.size() + line6.size();System.out.println("總的站點數(shù)量:"+totalStaion);//} } }?
轉載于:https://www.cnblogs.com/Excusezuo/p/10652311.html
總結
- 上一篇: 试题 E: 迷宫 第十届蓝桥杯
- 下一篇: java B2B2C Springclo