Advertisement
Guest User

Getting out of maze, 16/18

a guest
Nov 17th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. package weblab;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. class Node {
  7.  
  8.   List<Node> outgoingEdges;
  9.  
  10.   boolean marked;
  11.  
  12.   public Node() {
  13.     this.outgoingEdges = new ArrayList<>();
  14.     this.marked = false;
  15.   }
  16. }
  17.  
  18. class Solution {
  19.  
  20.   // Implement the solve method to return the answer to the problem posed by the inputstream.
  21.   public static String run(InputStream in) {
  22.     return new Solution().solve(in);
  23.   }
  24.  
  25.   public String solve(InputStream in) {
  26.   // TODO
  27.     Scanner sc = new Scanner(in);
  28.     String line1 = sc.nextLine();
  29.     Scanner sn = new Scanner(line1);
  30.     sn.useDelimiter("\\s+");
  31.     int n = sn.nextInt();
  32.     int m = sn.nextInt();
  33.     int s = sn.nextInt();
  34.     int t = sn.nextInt();
  35.    
  36.     sn.close();
  37.     if(m < 0) { sc.close(); return "no"; }
  38.    
  39.     System.out.println("\n" + "n is " + n);
  40.     List<Node> all = new ArrayList<>();
  41.     //make nodes 1 to n
  42.     for(int i = 0; i < n; i++) {
  43.       Node node = new Node();
  44.       all.add(node);
  45.     }
  46.     // System.out.println("all size is " + all.size());
  47.    
  48.     while(sc.hasNextLine()) {
  49.       String line = sc.nextLine();
  50.       Scanner se = new Scanner(line);
  51.       se.useDelimiter("\\s+");
  52.      
  53.       //get the numbers from se
  54.       int index = se.nextInt();
  55.       int direction = se.nextInt();
  56.       int length = se.nextInt(); //not needed/used here
  57.       if(length < 0) { }
  58.      
  59.       Node curr = all.get(index - 1);
  60.       Node dir = all.get(direction - 1);
  61.       curr.outgoingEdges.add(dir);
  62.      
  63.       //close scanner
  64.       se.close();
  65.     }
  66.     //close scanners
  67.     sc.close();
  68.    
  69.     //traverse and find exit
  70.     System.out.println("start at " + s);
  71.     Boolean result = traverse(all, s, t);
  72.     if(result == true) {
  73.       return "yes";
  74.     }
  75.     return "no";
  76.   }
  77.  
  78.   public static Boolean traverse(List<Node> all, int current, int exit) {
  79.     //check if current is exit
  80.     Node curr = all.get(current - 1);
  81.     Node endNode = all.get(exit - 1);
  82.     Boolean que = (current==exit);
  83.     Boolean resultado = false;
  84.     // System.out.println("    current == exit is " + que);
  85.     if(que || endNode.marked) {
  86.       System.out.println("we at " + current + "->" + exit);
  87.       System.out.println(" we in dis bitchh!!");
  88.       curr.marked = true;
  89.       return true;
  90.     }
  91.    
  92.     //mark the current (aka <root>)
  93.     if (curr.marked) {
  94.       return false;
  95.     }
  96.     curr.marked = true;
  97.    
  98.     // PRE-oder <root><left><right>
  99.     //now traverse and visit the rest, if one of them is marked return false
  100.     //get outgoingedges of current
  101.     List<Node> out = curr.outgoingEdges;
  102.     for(Node n : out) {
  103.       int newcurr = all.indexOf(n) + 1;
  104.       System.out.println("new curr is " + newcurr);
  105.       resultado = resultado || traverse(all, newcurr, exit);
  106.     }
  107.    
  108.     System.out.println("we come down here at current is " + current);
  109.     //check if exit node is marked
  110.     // Node endNode = all.get(exit - 1);
  111.     // if(endNode.marked) {
  112.     //   return true;
  113.     // }
  114.     System.out.println(" result is " + resultado);
  115.     return resultado;
  116.   }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement