• 计应193第一组个人流程——姚则谦


    package beihang;
    
    import java.io.*;
    
    import java.util.*;
    
    public class getStation {
    static class Line {
    int id;
    
    String name;
    
    List stations = new ArrayList();
    
    }
    
    public static class Station {
    String name;
    
    boolean visited;
    
    String preStation;
    
    List lineNow = new ArrayList();
    
    List nextStation = new ArrayList();
    
    }
    
    static List lines = new ArrayList();
    
    static List stations = new ArrayList();
    
    static HashMap map = new HashMap<>();
    
    public static void getStationByLine(String name, String fileOut) {
    List ans = new ArrayList();
    
    for (Line line : lines) {
    if (line.name.equals(name)) {
    ans = line.stations;
    
    break;
    
    }
    
    }
    
    try {
    FileWriter fileWriter = new FileWriter(fileOut);
    
    int index = 0;
    
    for (String station : ans) {
    if (index == 0) {
    fileWriter.write(station);
    
    index = 1;
    
    } else {
    fileWriter.write("->" + station);
    
    }
    
    }
    
    fileWriter.flush();
    
    fileWriter.close();
    
    } catch (IOException e) {
    e.printStackTrace();
    
    }
    
    System.out.println();
    
    return;
    
    }
    
    }
    
    2.因为有要求进行最短路线操作,而我采用的是BFS函数,代码如下:
    
    package beihang;
    
    import java.io.FileWriter;
    
    import java.io.IOException;
    
    import java.util.ArrayList;
    
    import java.util.Collections;
    
    import java.util.LinkedList;
    
    import java.util.List;
    
    import java.util.Map;
    
    import java.util.Queue;
    
    import beihang.getStation.Station;
    
    import beihang.getStation.*;
    
    public class BFS {
    public static void BFS(String st, String ed) {
    for (Map.Entry entry : getStation.map.entrySet()) {
    entry.getValue().visited = false;
    
    }
    
    
    Queue queue = new LinkedList<>();
    
    queue.add(st);
    
    while(!queue.isEmpty()) {
    String now = queue.poll();
    
    getStation.map.get(now).visited = true;
    
    if(now.equals(ed)) {
    break;
    
    }
    
    for(Station station : getStation.map.get(now).nextStation) {
    if( getStation.map.get(station.name).visited==false) {
    getStation.map.get(station.name).preStation = now;
    
    queue.add(station.name);
    
    }
    
    }
    
    }
    
    }
    
    public static void printPath(String st, String ed, String fileOut) {
    List list = new ArrayList<>();
    
    String now = ed;
    
    int flag = 0;
    
    String preLine = "";
    
    while(!now.equals(st)) {
    list.add(now);
    
    now = getStation.map.get(now).preStation;
    
    }
    
    Collections.reverse(list);
    
    try {
    FileWriter fileWriter = new FileWriter(fileOut);
    
    fileWriter.write(list.size() + "
    ");
    
    fileWriter.write(st);
    
    for(int i = 0; i < list.size(); i++) {
    flag = 0;
    
    if( getStation.map.get(list.get(i)).lineNow.size()==1) {
    fileWriter.write("->" + list.get(i));
    
    } else {
    int j;
    
    for(j = i+1; j < list.size(); j++) {
    if( getStation.map.get(list.get(j)).lineNow.size()==1) {
    if(! getStation.map.get(list.get(i)).lineNow.get(0).contains( getStation.map.get(list.get(j)).lineNow.get(0))) {
    if(preLine.equals( getStation.map.get(list.get(j)).lineNow.get(0))) {
    fileWriter.write("->" + list.get(i));
    
    } else {
    fileWriter.write("
    ");
    
    fileWriter.write( getStation.map.get(list.get(j)).lineNow.get(0) + "
    ");
    
    preLine = getStation.map.get(list.get(j)).lineNow.get(0);
    
    fileWriter.write(list.get(i));
    
    }
    
    }
    
    break;
    
    }
    
    }
    
    }
    
    }
    
    fileWriter.flush();
    
    fileWriter.close();
    
    } catch (IOException e) {
    e.printStackTrace();
    
    }
    
    }
    
    }
    
    3.最后就是主函数,负责读入txt文件与输出txt文件,同时做一下站点的判断:
    
    package beihang;
    
    import java.io.BufferedReader;
    
    import java.io.File;
    
    import java.io.FileInputStream;
    
    import java.io.IOException;
    
    import java.io.InputStreamReader;
    
    import java.util.ArrayList;
    
    import java.util.List;
    
    import beihang.getStation.Line;
    
    import beihang.getStation.Station;
    
    public class Subway {
    public void getSubwayMessage(String fileIn) {
    try {
    int cnt = 1;
    
    String path = fileIn;
    
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(path)),"UTF-8"));
    
    String getLine = null;
    
    while ((getLine = bufferedReader.readLine()) != null) {
    Line line = new Line();
    
    String[] list = getLine.split(" ");
    
    line.id = cnt;
    
    line.name = list[0];
    
    for(int i = 1; i < list.length-1; i++) {
    Station station1 = new Station();
    
    Station station2 = new Station();
    
    if(getStation.map.containsKey(list[i])) {
    station1 = getStation.map.get(list[i]);
    
    getStation.map.remove(list[i]);
    
    } else {
    station1.name = list[i];
    
    station1.visited = false;
    
    }
    
    if(getStation.map.containsKey(list[i+1])) {
    station2 = getStation.map.get(list[i+1]);
    
    getStation.map.remove(list[i+1]);
    
    } else {
    station2.name = list[i+1];
    
    station2.visited = false;
    
    }
    
    if(!station1.lineNow.contains(line.name)) {
    station1.lineNow.add(line.name);
    
    }
    
    if(!station2.lineNow.contains(line.name)) {
    station2.lineNow.add(line.name);
    
    }
    
    if(!station1.nextStation.contains(station2)) {
    station1.nextStation.add(station2);
    
    }
    
    if(!station2.nextStation.contains(station1)) {
    station2.nextStation.add(station1);
    
    }
    
    station1.preStation = station1.name;
    
    station2.preStation = station2.name;
    
    getStation.map.put(list[i], station1);
    
    getStation.map.put(list[i+1], station2);
    
    if (!line.stations.contains(station1.name)) {
    line.stations.add(station1.name);
    
    }
    
    if (!line.stations.contains(station2.name)) {
    line.stations.add(station2.name);
    
    }
    
    }
    
    getStation.lines.add(line);
    
    cnt++;
    
    }
    
    bufferedReader.close();
    
    } catch (Exception e) {
    System.err.println("read errors: " + e);
    
    }
    
    return ;
    
    }
    
    public static void main(String[] args) {
    Subway solve = new Subway();
    
    String fileIn = "";
    
    String fileOut = "";
    
    String line = "";
    
    String start = "";
    
    String end = "";
    
    for(String s:args) {
    System.out.print(s);
    
    }
    
    for(int i = 0; i < args.length; i++){
    if(args[i].equals("-map")) {
    i++;
    
    fileIn = args[i];
    
    }
    
    if(args[i].equals("-a")) {
    i++;
    
    line = args[i];
    
    }
    
    if(args[i].equals("-o")) {
    i++;
    
    fileOut = args[i];
    
    }
    
    if(args[i].equals("-b")) {
    i++;
    
    start = args[i];
    
    i++;
    
    end = args[i];
    
    }
    
    }
    
    if(args.length==2) {
    solve.getSubwayMessage(fileIn);
    
    System.out.println("读入的地铁信息如下:");
    
    for(Line line1 : getStation.lines) {
    System.out.print(line1.name + ":");
    
    for(String s : line1.stations) {
    System.out.print(" " + s);
    
    }
    
    System.out.println();
    
    }
    
    } else if(args.length==6) {
    solve.getSubwayMessage(fileIn);
    
    int flag = 0;
    
    for(Line line1 : getStation.lines) {
    if(line1.name.equals(line)) {
    flag = 1;
    
    }
    
    }
    
    if(flag==1) {
    getStation.getStationByLine(line, fileOut);
    
    System.out.println("Successfully output all site results on the route to " + fileOut);
    
    } else {
    System.out.println("北京地铁线路中不存在'" + line + "'线路");
    
    }
    
    } else if(args.length==7) {
    solve.getSubwayMessage(fileIn);
    
    int flag1 = 0;
    
    int flag2 = 0;
    
    for(Line line1 : getStation.lines) {
    if (line1.stations.contains(start)) {
    flag1 = 1;
    
    }
    
    }
    
    for(Line line1 : getStation.lines) {
    if (line1.stations.contains(end)) {
    flag2 = 1;
    
    }
    
    }
    
    if(flag1==1 && flag2==1) {
    BFS.BFS(start, end);
    
    BFS.printPath(start, end, fileOut);
    
    }
    
    if(flag1==0) {
    System.out.println("北京地铁线路中不存在'" + start + "'站点");
    
    }
    
    if(flag2==0) {
    System.out.println("北京地铁线路中不存在'" + end + "'站点");
    
    }
    
    }
    
    }
    
    }
  • 相关阅读:
    正则表达式获取字符串中html<input>标签指定value值
    [转]利用SqlBulkCopy快速大批量导入数据(SqlBulkCopy强大)
    ASP.NET数据库操作常用类
    response.redirect 和Server.Transfer有什么区别?
    数据驱动的依存句法分析方法简介(一)
    C#序列化问题
    MSSQL 标量函数
    事件和委托问题转
    Copy指定目录下的所有文件到新位置
    页面指定播放视频
  • 原文地址:https://www.cnblogs.com/bladepoint/p/14645169.html
Copyright © 2020-2023  润新知