Advertisement
Kame3

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

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