Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.47 KB | None | 0 0
  1. import java.awt.Point;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.LinkedList;
  5.  
  6.  
  7. public class BruteForcePath {
  8.     static int[][] data = new int[][]{
  9.               { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
  10.               { 0, 1, 1, 0, 1, 1, 0, 1, 0, 0 },
  11.               { 0, 0, 1, 0, 0, 0, 0, 1, 1, 1 },
  12.               { 0, 0, 1, 1, 1, 1, 1, 1, 0, 0 },
  13.               { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  14.             };
  15.    
  16.     private Cell[][] cells;
  17.     private final int width;
  18.     private final int height;
  19.    
  20.     public static void main(String[] args) {
  21.         new BruteForcePath();
  22.     }
  23.    
  24.     public BruteForcePath() {
  25.         width = data[0].length;
  26.         height = data.length;
  27.        
  28.         // Fill Cells from binary data (also transpose it, to match the style it was written)
  29.         cells = new Cell[width][height];
  30.         for (int i = 0; i < width; i++) {
  31.             for (int j = 0; j < height; j++) {
  32.                 cells[i][j] = data[j][i] == 0?new Cell(i,j):null;
  33.             }
  34.         }
  35.    
  36.         // Print
  37.         printData();
  38.        
  39.         // Compute a path
  40.         ArrayList<Cell> path = generatePath( getCellData(0,0), getCellData(width-1,height-1) );
  41.    
  42.         // Color path
  43.         for (int i = 0; i < path.size(); i++) {
  44.             path.get(i).path = true;
  45.         }
  46.        
  47.         // Print
  48.         printData();
  49.     }
  50.    
  51.     private ArrayList<Cell> generatePath(Cell start, Cell end) {
  52.         ArrayList<ArrayList<Cell>> paths = new ArrayList<ArrayList<Cell>>();
  53.        
  54.         // Initial path
  55.         ArrayList<Cell> startP = new ArrayList<Cell>();
  56.         startP.add(start);
  57.        
  58.         // Compute all paths
  59.         ArrayList<Cell> adjacent = getAdjacentCells(start);
  60.         for (int i = 0; i < adjacent.size(); i++) {
  61.             ArrayList<Cell> copy = (ArrayList<Cell>) startP.clone();
  62.             paths.add( copy );
  63.             computePath( adjacent.get(i), end, copy, paths );
  64.         }
  65.        
  66.         // Remove broken paths
  67.         for (int i = 0; i < paths.size(); i++) {
  68.             ArrayList<Cell> path = paths.get(i);
  69.             if ( !path.get(path.size()-1).equals(end) )
  70.                 paths.remove(i--);
  71.         }
  72.        
  73.         // Get shortest path
  74.         int shortest = Integer.MAX_VALUE;
  75.         ArrayList<Cell> shortestPath = null;
  76.         for (int i = 0; i < paths.size(); i++) {
  77.             ArrayList<Cell> path = paths.get(i);
  78.             if ( path.size() < shortest ) {
  79.                 shortest = path.size();
  80.                 shortestPath = path;
  81.             }
  82.         }
  83.        
  84.         // Print
  85.         if ( shortestPath != null ) {
  86.             System.out.println("Shortest Path is: " + shortestPath.size() + " cell(s)");
  87.             return shortestPath;
  88.         }
  89.        
  90.         System.out.println("Cound not calculate path!");
  91.         return null;
  92.     }
  93.    
  94.     /*private ArrayList<Cell> copyPath( ArrayList<Cell> list ) {
  95.         ArrayList<Cell> list2 = new ArrayList<Cell>();
  96.         for (int i = 0; i < list.size(); i++) {
  97.             list2.add(list.get(i));
  98.         }
  99.         return list2;
  100.     }*/
  101.  
  102.     private void computePath(Cell current, Cell end, ArrayList<Cell> path, ArrayList<ArrayList<Cell>> paths) {
  103.         path.add(current);
  104.         if ( current.equals(end) )
  105.             return;
  106.        
  107.         // Get adjacent cells
  108.         ArrayList<Cell> adjacent = getAdjacentCells(current);
  109.        
  110.         // Remove all adjacent cells currently in path
  111.         for (int i = 0; i < adjacent.size(); i++) {
  112.             if ( path.contains(adjacent.get(i)) ) {
  113.                 adjacent.remove(i);
  114.                 i--;
  115.             }
  116.         }
  117.        
  118.         // End of path
  119.         if ( adjacent.size() <= 0 )
  120.             return;
  121.        
  122.         // If we're at a junction
  123.         for (int i = 1; i < adjacent.size(); i++) {
  124.             ArrayList<Cell> copy = (ArrayList<Cell>) path.clone();//copyPath(path);
  125.             paths.add( copy );
  126.             computePath( adjacent.get(i), end, copy, paths );
  127.         }
  128.        
  129.         // Continue current path
  130.         computePath(adjacent.get(0), end, path, paths);    
  131.     }
  132.  
  133.     private ArrayList<Cell> getAdjacentCells(Cell root) {
  134.         ArrayList<Cell> ret = new ArrayList<Cell>();
  135.        
  136.         int xx = root.x;
  137.         int yy = root.y;
  138.        
  139.         Cell c1 = getCellData(xx-1, yy);
  140.         Cell c2 = getCellData(xx+1, yy);
  141.         Cell c3 = getCellData(xx, yy-1);
  142.         Cell c4 = getCellData(xx, yy+1);
  143.        
  144.         if ( c1 != null )
  145.             ret.add(c1);
  146.        
  147.         if ( c2 != null )
  148.             ret.add(c2);
  149.        
  150.         if ( c3 != null )
  151.             ret.add(c3);
  152.        
  153.         if ( c4 != null )
  154.             ret.add(c4);
  155.        
  156.         return ret;
  157.     }
  158.  
  159.     private Cell getCellData(int i, int j) {
  160.         if ( i < 0 || j < 0 || i >= width || j >= height )
  161.             return null;
  162.         return cells[i][j];
  163.     }
  164.  
  165.     private void printData() {
  166.         for (int j = 0; j < height; j++) {
  167.             for (int i = 0; i < width; i++) {
  168.                 Cell c = cells[i][j];
  169.                 String cellSymbol = "'";
  170.                 if ( c == null )
  171.                     cellSymbol = "#";
  172.                 if ( c != null && c.path ) {
  173.                     cellSymbol = "*";
  174.                 }
  175.                
  176.                 System.out.print( cellSymbol );
  177.             }
  178.             System.out.println();
  179.         }
  180.     }
  181.  
  182.     class Cell {
  183.         int x;
  184.         int y;
  185.         boolean path;
  186.         Cell(int x, int y) {
  187.             this.x = x;
  188.             this.y = y;
  189.         }
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement