Advertisement
Guest User

Untitled

a guest
May 24th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1.     public void solve() {
  2.         int currentDepth = 0;
  3.         GridSolution solution = new GridSolution();
  4.        
  5.         for (int newY = 0; newY < gridCells.length; newY++) {
  6.             for (int newX = 0; newX < gridCells[newY].length; newX++) {
  7.  
  8.                 if (gridCells[newY][newX].getType() == GridCell.Type.NONE) continue;
  9.                
  10.                 _solve(gridCells, newX, newY, Direction.INCREASE, currentDepth, solution);
  11.                 _solve(gridCells, newX, newY, Direction.DECREASE, currentDepth, solution);
  12.                
  13.             }
  14.         }
  15.        
  16.         Collections.sort(solutions);
  17.     }
  18.    
  19.     private void _solve(GridCell[][] grid, int x, int y, Direction direction, int depth, GridSolution solution) {
  20.        
  21.         //"Hash", to prevent deep copy comparing
  22.         String hash = Grid.createGridCellHash(grid);
  23.        
  24.         //Check if this state was already parsed
  25.         for (String parsedGridHash : parsedStatesHashes) {
  26.             if (parsedGridHash != "" && parsedGridHash.equals(hash))
  27.                 return;
  28.         }
  29.  
  30.         this.iterations++;
  31.         GridCell[][] copy = Grid.CopyGrid(grid);
  32.         GridSolution copySolution = new GridSolution(solution);
  33.  
  34.        
  35.         if (Grid.checkSolved(grid)) {
  36.             solutions.add(copySolution);
  37.             return;
  38.         }
  39.  
  40.         if (direction == Direction.INCREASE) {
  41.             if(!Grid.increase(copy, x, y)) {
  42.                 return;
  43.             }
  44.         } else {
  45.             if(!Grid.decrease(copy, x, y)) {
  46.                 return;
  47.             }
  48.         }
  49.        
  50.         copySolution.AddTurn(new GridTurn(x, y, direction));
  51.        
  52.        
  53.         depth += 1;
  54.        
  55.         if (depth > MAX_DEPTH) {
  56.             return;
  57.         }
  58.        
  59.         for (int newY = 0; newY < gridCells.length; newY++) {
  60.            
  61.             for (int newX = 0; newX < gridCells[newY].length; newX++) {
  62.                
  63.                 if (gridCells[newY][newX].getType() == GridCell.Type.NONE) continue;
  64.                
  65.                 if (newY != y && newX != x && direction != Direction.DECREASE) {
  66.                    
  67.                 } else {
  68.                     _solve(copy, newX, newY, Direction.INCREASE, depth, copySolution);
  69.                 }
  70.                
  71.                 if (newY != y && newX != x && direction != Direction.INCREASE)  {
  72.                    
  73.                 } else {
  74.                     _solve(copy, newX, newY, Direction.DECREASE, depth, copySolution);
  75.                 }
  76.                
  77.             }
  78.            
  79.         }
  80.        
  81.         //After completely parsing this state, add it to parsed states
  82.         parsedStatesHashes.add(hash);
  83.  
  84.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement