公交系统的java实现(四)

          接上集:

        首先,一个简单的去重类:

         

package graph;

/**  
 *  DelRepeat 去重需要的类
 * @author xuejupo  [email protected] 
 * create in 2015-12-7 下午6:34:09    
 */

public class DelRepeat implements Comparable{
	public DelRepeat(String standName, int num, int standNum){
		this.standName = standName;
		this.num = num;
		this.standNum = standNum;
	}
	
	//站名
	String standName;
	//需要换乘次数
	int num;
	//总共需要的站数
	int standNum;
	/**
	 * 重写比较方法,当换乘次数和换乘站数都大的话,则认为该站点不用再次处理
	 */
	@Override
	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		DelRepeat d = (DelRepeat)o;
		if(!this.standName.equals(d.standName)){
			return -1;
		}
		if(d.num > this.num && d.standNum > this.standNum){
			return 1;
		}
		return -1;
	}
	/**
	 * 要往set里放,重写equal方法和tohash方法
	 */
	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return this.standName.hashCode();
	}
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		DelRepeat d = (DelRepeat)obj;
		return this.standName.equals(d.standName);
	}
	
}

           简单的去重逻辑是这样:  如果站点A被处理过,并且站点A上次处理的结果中,从出发地到A站点的换乘次数和经过的站数都小于这次站点A的线路,则认为站点A不用再次处理。

       分别处理不换成和换乘一次的结果:

/**  
	* dealFirsstLevel:  首先处理不换成的情况
	* void  返回类型   
	*/
	private void dealFirstLevel(List<List<Line>> result,String dest){
		for(Line l:firstLevel){
			if(l.standName.equals(dest)){
				List<Line> temp = new ArrayList<Line>();
				temp.add(l);
				result.add(temp);
				DelRepeat dealRe = new DelRepeat(dest,1,l.num);
				isNeedDeal.put(dest, dealRe);
			}
		}
	}
	
	/**  
	* dealSecondLevel:  处理换乘一次的情况
	* void  返回类型   
	*/
	private void dealSecondLevel(List<List<Line>> result,String dest){
		for(Map<String, List<Line>> m:secondLevel){
			for(Entry<String, List<Line>> e:m.entrySet()){
				String standName = e.getKey();
				List<Line> l = e.getValue();
				
				//处理二层循环中每一条线路
				for(Line line:l){
					if(line.standName.equals(dest)){
						//如果该站点已经处理过,并且换乘次数和换乘站数都小于当前线路,则不处理当前线路
						if(isNeedDeal.containsKey(line.standName)){
							if((isNeedDeal.get(line.standName)).num == 1 && isNeedDeal.get(line.standName).standNum < line.num){
								continue;
							}
						}
						List<List<Line>> firstLine = new ArrayList<List<Line>>();
						this.dealFirstLevel(firstLine, standName);
						//处理当前第二层站点获取到的第一层线路
						for(List<Line> firstLevel:firstLine){
							//最终要加到result里的线路
							List<Line> secondLine = new ArrayList<Line>();
							DelRepeat second = new DelRepeat(line.standName,2,firstLevel.get(0).num + line.num);
							if(firstLevel.get(0).lineName.equals(line.lineName)){
								continue;
							}
							if(isNeedDeal.containsKey(line.standName)){
								if(isNeedDeal.get(line.standName).compareTo(second) == 1){
									continue;
								}else{
									//处理站点
									secondLine.add(firstLevel.get(0));
									secondLine.add(line);
								}
							}else{
								//处理站点
								secondLine.add(firstLevel.get(0));
								secondLine.add(line);
								isNeedDeal.put(line.standName, second);
							}
							if(secondLine.size() != 0){
								result.add(secondLine);
							}
						}
					}
				}
				
			}
		}
	}

 测试函数:

		// TODO Auto-generated method stub
		long start = System.currentTimeMillis();
		Transit t = new Transit();
		MessageInit.init();
		t.initlevel("西城公交总站");
		List<List<Line>> result = t.getLine("西门");
		int i = 1;
		for(List<Line> l:result){
			System.out.println("第"+ i++ +"条线路,需要换乘"+l.size()+"次");
			for(Line line:l){
				System.out.println(line);
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("程序共用了:"+(end - start)+"毫秒");
	

结果:

第1条线路,需要换乘0次
乘坐常规公交19路,坐6站西门下车
第2条线路,需要换乘1次
乘坐常规公交119路,坐2站海州运管所下车
乘坐常规公交19路,坐4站西门下车
第3条线路,需要换乘1次
乘坐常规公交19路,坐5站西大岭下车
乘坐常规公交28路,坐1站西门下车
第4条线路,需要换乘1次
乘坐常规公交19路,坐5站西大岭下车
乘坐常规公交11路,坐1站西门下车
第5条线路,需要换乘1次
乘坐常规公交119路,坐3站四季农贸市场下车
乘坐常规公交11路,坐3站西门下车
第6条线路,需要换乘1次
乘坐常规公交19路,坐3站四季农贸市场下车
乘坐常规公交11路,坐3站西门下车
第7条线路,需要换乘1次
乘坐常规公交119路,坐3站四季农贸市场下车
乘坐常规公交19路,坐3站西门下车
第8条线路,需要换乘1次
乘坐常规公交119路,坐3站四季农贸市场下车
乘坐常规公交28路,坐3站西门下车
第9条线路,需要换乘1次
乘坐常规公交19路,坐3站四季农贸市场下车
乘坐常规公交28路,坐3站西门下车
第10条线路,需要换乘1次
乘坐常规公交119路,坐1站粮库宿舍下车
乘坐常规公交19路,坐5站西门下车
第11条线路,需要换乘1次
乘坐常规公交19路,坐4站洪门村下车
乘坐常规公交11路,坐2站西门下车
第12条线路,需要换乘1次
乘坐常规公交19路,坐4站洪门村下车
乘坐常规公交28路,坐2站西门下车
程序共用了:216毫秒

        可以看到,其实一次换乘就有很有重复情况,先考虑把一次换乘的情况处理清楚,再去考虑换乘2次,3次的情况。健身去了,一次换乘情况去重处理明天再弄。。

猜你喜欢

转载自709002341.iteye.com/blog/2262422