Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.27 KB | None | 0 0
  1.     public static void main(String[] args) throws IOException
  2.     {
  3.         hp = new HashSet<String>();
  4.         Scanner in = new Scanner(System.in);
  5.        
  6.         String fileName = "input1.txt";
  7.         String split[];
  8.         try {
  9.             FileReader fileReader =
  10.                 new FileReader(fileName);
  11.             BufferedReader bufferedReader =
  12.                 new BufferedReader(fileReader);
  13.  
  14.             while((line = bufferedReader.readLine()) != null) {
  15.                 if(line.equals("END OF INPUT"))
  16.                     break;
  17.                 split = line.split(" ");
  18.                
  19.                 hp.add((split[0]));
  20.                 hp.add((split[1]));
  21.             }  
  22.             bufferedReader.close();        
  23.         }
  24.         catch(FileNotFoundException ex) {
  25.             System.out.println(
  26.                 "Unable to open file '" +
  27.                 fileName + "'");                
  28.         }
  29.         catch(IOException ex) {
  30.             System.out.println(
  31.                 "Error reading file '"
  32.                 + fileName + "'");                  
  33.         }
  34.  
  35.         copy = new ArrayList<String>(hp);
  36.         int[][] adjacencyMatrix = new int[hp.size()+1][hp.size()+1];
  37.        
  38.         try {
  39.             FileReader fileReader =
  40.                 new FileReader(fileName);
  41.             BufferedReader bufferedReader =
  42.                 new BufferedReader(fileReader);
  43.  
  44.             while((line = bufferedReader.readLine()) != null) {
  45.                 if(line.equals("END OF INPUT"))
  46.                     break;
  47.                 split = line.split(" ");
  48.                
  49.                 int i = getIndex(split[0]);
  50.                 int j = getIndex(split[1]);
  51.                 adjacencyMatrix[j][i] = adjacencyMatrix[i][j] = Integer.parseInt(split[2]);
  52.                
  53.             }  
  54.             bufferedReader.close();        
  55.         }
  56.         catch(FileNotFoundException ex) {
  57.             System.out.println(
  58.                 "Unable to open file '" +
  59.                 fileName + "'");                
  60.         }
  61.         catch(IOException ex) {
  62.             System.out.println(
  63.                 "Error reading file '"
  64.                 + fileName + "'");                  
  65.         }
  66.        
  67.         for(int i=0;i<adjacencyMatrix.length;i++){
  68.             for(int j=0;j<adjacencyMatrix.length;j++){
  69.                 if(adjacencyMatrix[i][j] == 0)
  70.                     adjacencyMatrix[i][j] = 9999;
  71. //                System.out.printf("i = %d, j = %d, %d\n",i,j,adjacencyMatrix[i][j]);
  72.             }
  73.         }
  74.        
  75.         UniformCostSearch ucs = new UniformCostSearch(copy.size());
  76.         String source = in.next();
  77.         String destination = in.next();
  78.         if(hp.contains(source) && hp.contains(destination)){
  79.         int src = getIndex(source);
  80.         int dst = getIndex(destination);
  81.         int distance = ucs.uniformCostSearch(adjacencyMatrix, src, dst);
  82.         if(distance == 99999999){
  83.             System.out.println();System.out.println();System.out.println();
  84.             System.out.println("Distance: INFINITY");
  85.             System.out.println("Route:\nNONE");
  86.             System.out.println();System.out.println();System.out.println();
  87.         }else{
  88.             ucs.printPath();
  89.             Iterator<Integer> iterator = ucs.path.descendingIterator();
  90.             System.out.println();System.out.println();System.out.println();
  91.             System.out.println("Distance: "+distance);
  92.             String[] traverse = new String[ucs.path.size()];
  93.             int pp = 0;
  94.             while(iterator.hasNext()){
  95.                 String aa = copy.get(iterator.next()-1);
  96.                 traverse[pp++] = aa;
  97.             }
  98.             System.out.println("Route:");
  99.             for(int i=0;i<traverse.length-1;i++){
  100.                 int j = i+1;
  101.                 System.out.println(traverse[i]+" -> "+traverse[j]+", "+adjacencyMatrix[getIndex(traverse[i])][getIndex(traverse[j])]);
  102.             }
  103.             System.out.println("");System.out.println("");System.out.println("");}
  104.         }else{
  105.             System.out.println();System.out.println();System.out.println();
  106.             System.out.println("Specified Location does not exist");
  107.             System.out.println();System.out.println();System.out.println();
  108.         }
  109.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement