Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void solve() {
- int currentDepth = 0;
- GridSolution solution = new GridSolution();
- for (int newY = 0; newY < gridCells.length; newY++) {
- for (int newX = 0; newX < gridCells[newY].length; newX++) {
- if (gridCells[newY][newX].getType() == GridCell.Type.NONE) continue;
- _solve(gridCells, newX, newY, Direction.INCREASE, currentDepth, solution);
- _solve(gridCells, newX, newY, Direction.DECREASE, currentDepth, solution);
- }
- }
- Collections.sort(solutions);
- }
- private void _solve(GridCell[][] grid, int x, int y, Direction direction, int depth, GridSolution solution) {
- //"Hash", to prevent deep copy comparing
- String hash = Grid.createGridCellHash(grid);
- //Check if this state was already parsed
- for (String parsedGridHash : parsedStatesHashes) {
- if (parsedGridHash != "" && parsedGridHash.equals(hash))
- return;
- }
- this.iterations++;
- GridCell[][] copy = Grid.CopyGrid(grid);
- GridSolution copySolution = new GridSolution(solution);
- if (Grid.checkSolved(grid)) {
- solutions.add(copySolution);
- return;
- }
- if (direction == Direction.INCREASE) {
- if(!Grid.increase(copy, x, y)) {
- return;
- }
- } else {
- if(!Grid.decrease(copy, x, y)) {
- return;
- }
- }
- copySolution.AddTurn(new GridTurn(x, y, direction));
- depth += 1;
- if (depth > MAX_DEPTH) {
- return;
- }
- for (int newY = 0; newY < gridCells.length; newY++) {
- for (int newX = 0; newX < gridCells[newY].length; newX++) {
- if (gridCells[newY][newX].getType() == GridCell.Type.NONE) continue;
- if (newY != y && newX != x && direction != Direction.DECREASE) {
- } else {
- _solve(copy, newX, newY, Direction.INCREASE, depth, copySolution);
- }
- if (newY != y && newX != x && direction != Direction.INCREASE) {
- } else {
- _solve(copy, newX, newY, Direction.DECREASE, depth, copySolution);
- }
- }
- }
- //After completely parsing this state, add it to parsed states
- parsedStatesHashes.add(hash);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement