Crazy

Изминување на лавиринт

Dec 11th, 2017
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.33 KB | None | 0 0
  1.  
  2. import java.util.Hashtable;
  3. import java.util.LinkedList;
  4. import java.util.Scanner;
  5. import java.util.Stack;
  6.  
  7.  
  8. public class Lavirint {
  9.  
  10.  
  11.     Graph graph;
  12.     int start;
  13.     int end;
  14.     Hashtable<Integer, String> hash;
  15.     Hashtable<String, Integer> rehash;
  16.    
  17.     Lavirint() {
  18.         hash = new Hashtable<>();
  19.         rehash = new Hashtable<>();
  20.     }
  21.    
  22.     void createGraph(int rows, int columns, String [] input) {
  23.        
  24.         int numNodes = 0;
  25.         String key;
  26.         GraphNode nodes[] = new GraphNode[rows * columns];
  27.        
  28.         for (int i=0; i < rows; ++i) {
  29.             for (int j=0; j < columns; ++j) {
  30.                 if (input[i].charAt(j) != '#') {
  31.                     key = i + "," + j;
  32.                     hash.put(numNodes,key);
  33.                     rehash.put(key, numNodes);
  34.                     nodes[numNodes] = new GraphNode(numNodes, key);
  35.                     if (input[i].charAt(j) == 'S')
  36.                         start = numNodes;
  37.                     if (input[i].charAt(j) == 'E')
  38.                         end = numNodes;
  39.                     ++numNodes;
  40.                 }
  41.             }
  42.         }
  43.        
  44.         graph = new Graph(numNodes,nodes);
  45.         int x;
  46.         int y;
  47.         for (int i=0; i < rows; ++i) {
  48.             for (int j=0; j < columns; ++j) {
  49.                
  50.                 if (input[i].charAt(j) != '#') {
  51.                     if (input[i].charAt(j-1) != '#') {
  52.                         x = rehash.get(i + "," + j);
  53.                         y = rehash.get(i + "," + (j-1));
  54.                         graph.addEdge(x, y);
  55.                     }
  56.                    
  57.                     if (input[i].charAt(j+1) != '#') {
  58.                         x = rehash.get(i + "," + j);
  59.                         y = rehash.get(i + "," + (j+1));
  60.                         graph.addEdge(x, y);
  61.                     }
  62.                    
  63.                     if (input[i-1].charAt(j) != '#') {
  64.                         x = rehash.get(i + "," + j);
  65.                         y = rehash.get((i-1) + "," + j);
  66.                         graph.addEdge(x, y);
  67.                     }
  68.                    
  69.                     if (input[i+1].charAt(j) != '#') {
  70.                         x = rehash.get(i + "," + j);
  71.                         y = rehash.get((i+1) + "," + j);
  72.                         graph.addEdge(x, y);
  73.                     }
  74.                    
  75.                 }
  76.                
  77.             }
  78.         }
  79.        
  80.     }
  81.    
  82.     void findPath() {
  83.         boolean visited[] = new boolean[graph.num_nodes];
  84.         for (int i = 0; i < graph.num_nodes; i++)
  85.             visited[i] = false;
  86.         visited[start] = true;
  87.         Stack<Integer> s = new Stack<Integer>();
  88.         s.push(start);
  89.  
  90.         GraphNode pom;
  91.  
  92.         while (!s.isEmpty()&&s.peek() != end) {
  93.             pom = graph.adjList[s.peek()];
  94.             GraphNode tmp=null;
  95.             for (int i = 0; i < pom.getNeighbors().size(); i++) {
  96.                 tmp = pom.getNeighbors().get(i);
  97.                 if (!visited[tmp.getIndex()])
  98.                     break;
  99.             }
  100.             if(tmp!=null&&!visited[tmp.getIndex()]){
  101.                 visited[tmp.getIndex()] = true;
  102.                 s.push(tmp.getIndex());
  103.             }
  104.             else
  105.                 s.pop();
  106.         }
  107.        
  108.         if(s.isEmpty())
  109.             System.out.println("Nema reshenie");
  110.         else{
  111.             Stack<Integer> os = new Stack<Integer>();
  112.             while(!s.isEmpty())
  113.                 os.push(s.pop());
  114.             while(!os.isEmpty())
  115.                 System.out.println(hash.get(os.pop()));
  116.         }
  117.    
  118.     }
  119.    
  120.     @Override
  121.     public String toString() {
  122.         return graph.toString();
  123.     }
  124.    
  125.     public static void main(String[] args) {
  126.        
  127.         Scanner scan = new Scanner(System.in);
  128.         Lavirint lavirint = new Lavirint();
  129.         String line = scan.nextLine();
  130.         String parts[] = line.split(",");
  131.        
  132.         int rows = Integer.parseInt(parts[0]);
  133.         int columns = Integer.parseInt(parts[1]);
  134.        
  135.         String input[] = new String[rows];
  136.        
  137.         for (int i=0; i < rows; ++i) {
  138.             input[i] = scan.nextLine();
  139.         }
  140.        
  141.         lavirint.createGraph(rows, columns, input);
  142.         lavirint.findPath();
  143.     }
  144.    
  145.    
  146.    
  147. }
  148.  
  149. class GraphNode {
  150.     private int index;//index (reden broj) na temeto vo grafot
  151.     private String info;
  152.     private LinkedList<GraphNode> neighbors;
  153.    
  154.     public GraphNode(int index, String info) {
  155.         this.index = index;
  156.         this.info = info;
  157.         neighbors = new LinkedList<GraphNode>();
  158.     }
  159.    
  160.     boolean containsNeighbor(GraphNode o){
  161.         return neighbors.contains(o);
  162.     }
  163.    
  164.     void addNeighbor(GraphNode o){
  165.         neighbors.add(o);
  166.     }
  167.    
  168.     void removeNeighbor(GraphNode o){
  169.         if(neighbors.contains(o))
  170.             neighbors.remove(o);
  171.     }
  172.    
  173.     @Override
  174.     public boolean equals(Object obj) {
  175.         @SuppressWarnings("unchecked")
  176.         GraphNode pom = (GraphNode)obj;
  177.         return (pom.info.equals(this.info));
  178.     }
  179.    
  180.     @Override
  181.     public String toString() {
  182.         return index + " " + info;
  183.     }
  184.  
  185.    
  186.     public int getIndex() {
  187.         return index;
  188.     }
  189.  
  190.     public void setIndex(int index) {
  191.         this.index = index;
  192.     }
  193.  
  194.     public String getInfo() {
  195.         return info;
  196.     }
  197.  
  198.     public void setInfo(String info) {
  199.         this.info = info;
  200.     }
  201.  
  202.     public LinkedList<GraphNode> getNeighbors() {
  203.         return neighbors;
  204.     }
  205.  
  206.     public void setNeighbors(LinkedList<GraphNode> neighbors) {
  207.         this.neighbors = neighbors;
  208.     }
  209.  
  210. }
  211.  
  212. class Graph {
  213.  
  214.     int num_nodes;
  215.     GraphNode adjList[];
  216.  
  217.     @SuppressWarnings("unchecked")
  218.     public Graph(int num_nodes, GraphNode[] list) {
  219.         this.num_nodes = num_nodes;
  220.         adjList = new GraphNode[num_nodes];
  221.         for (int i=0; i < num_nodes; ++i)
  222.             adjList[i] = list[i];
  223.     }
  224.  
  225.     int adjacent(int x, int y) {
  226.         // proveruva dali ima vrska od jazelot so
  227.         // indeks x do jazelot so indeks y
  228.         return (adjList[x].containsNeighbor(adjList[y])) ? 1 : 0;
  229.     }
  230.  
  231.     void addEdge(int x, int y) {
  232.         // dodava vrska od jazelot so indeks x do jazelot so indeks y
  233.         if (!adjList[x].containsNeighbor(adjList[y])) {
  234.             adjList[x].addNeighbor(adjList[y]);
  235.         }
  236.         if (!adjList[y].containsNeighbor(adjList[x])) {
  237.             adjList[y].addNeighbor(adjList[x]);
  238.         }
  239.     }
  240.  
  241.     void deleteEdge(int x, int y) {
  242.         adjList[x].removeNeighbor(adjList[y]);
  243.         adjList[y].removeNeighbor(adjList[x]);
  244.     }
  245.    
  246.     @Override
  247.     public String toString() {
  248.         String ret = new String();
  249.         for(int i=0;i<this.num_nodes;i++)
  250.             ret+=adjList[i]+"\n";
  251.         return ret;
  252.     }
  253.    
  254. }
Advertisement
Add Comment
Please, Sign In to add comment