Advertisement
nocturnalmk

Maze.java

Dec 21st, 2012
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. package lavirint;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Stack;
  5.  
  6. public class Maze {
  7.    
  8.     int start_node;
  9.     int end_node;
  10.     Graph<Integer> graph;
  11.    
  12.     public Maze() {
  13.     }
  14.    
  15.     public void solve(String[] maze) {
  16.         generateGraph(maze.length, maze[0].length(), maze);
  17.         findPath();
  18.     }
  19.    
  20.     private void generateGraph(int rows, int columns, String[] maze) {
  21.        
  22.         String key;
  23.         int node = 0;
  24.         HashMap<String, Integer> h = new HashMap<String, Integer>();
  25.        
  26.         for (int i = 1; i < rows-1; i++) {
  27.             for (int j = 1; j < columns-1; j++) {
  28.                
  29.                 char c = maze[i].charAt(j);
  30.                 if (c != '#') {
  31.                     key = i+","+j;
  32.                     h.put(key, node);
  33.                    
  34.                     if (c == 'S') {
  35.                         start_node = node;
  36.                     }
  37.                     if (c == 'E') {
  38.                         end_node = node;
  39.                     }
  40.                    
  41.                     node++;
  42.                 }
  43.  
  44.             }
  45.         }
  46.        
  47.         graph = new Graph<Integer>(node);
  48.         int x, y;
  49.        
  50.         for (int i=1; i<rows-1; i++) {
  51.             for (int j=1; j<columns-1; j++) {
  52.                
  53.                 char c = maze[i].charAt(j);
  54.                
  55.                 if (c != '#') {
  56.                    
  57.                     if (maze[i].charAt(j+1) != '#') {
  58.                         x = h.get(i+","+j);
  59.                         y = h.get(i+","+(j+1));
  60.                         graph.addEdge(x, y);
  61.                     }
  62.                    
  63.                     if (maze[i].charAt(j-1) != '#') {
  64.                         x = h.get(i+","+j);
  65.                         y = h.get(i+","+(j-1));
  66.                         graph.addEdge(x, y);
  67.                     }
  68.                    
  69.                     if (maze[i+1].charAt(j) != '#') {
  70.                         x = h.get(i+","+j);
  71.                         y = h.get((i+1)+","+j);
  72.                         graph.addEdge(x, y);
  73.                     }
  74.                    
  75.                     if (maze[i-1].charAt(j) != '#') {
  76.                         x = h.get(i+","+j);
  77.                         y = h.get((i-1)+","+j);
  78.                         graph.addEdge(x, y);
  79.                     }
  80.                 }
  81.             }
  82.            
  83.         }
  84.        
  85.     }
  86.    
  87.  
  88.    
  89.     private void findPath() {
  90.        
  91.         Stack<Integer> s = new Stack<Integer>();
  92.         boolean checked[] = new boolean[graph.getNum_nodes()];
  93.         int tmp1, tmp2;
  94.        
  95.         for (int i = 0; i < checked.length; i++) {
  96.             checked[i] = false;
  97.         }
  98.        
  99.         s.push(start_node);
  100.         checked[start_node] = true;
  101.        
  102.         while (!s.isEmpty() && (s.peek() != end_node)) {
  103.            
  104.             tmp1 = s.peek();
  105.             tmp2 = tmp1;
  106.            
  107.             for (int i = 0; i < graph.getNum_nodes(); i++) {
  108.                 if (graph.adjacent(tmp1, i) == 1) {
  109.                     tmp2 = i;
  110.                    
  111.                     if (!checked[i]) {
  112.                         break;
  113.                     }
  114.                 }
  115.             }
  116.            
  117.             if (!checked[tmp2]) {
  118.                 s.push(tmp2);
  119.                 checked[tmp2] = true;
  120.             } else {
  121.                 s.pop();
  122.             }
  123.            
  124.         }
  125.        
  126.         Stack<Integer> inverted = new Stack<Integer>();
  127.        
  128.         while (!s.isEmpty()) {
  129.             inverted.push(s.pop());        
  130.         }
  131.        
  132.         while (!inverted.isEmpty()) {
  133.             System.out.println(inverted.pop());
  134.         }
  135.        
  136.        
  137.     }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement