Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sudoku solve method

I've a problem with my sudoku solving method. The program works like this; the board is empty when started, the users adds a couple of numbers to the board and then by hitting a Solve-button the program tries to solve it. Everything works fine besides if I put the same number in the same row. So if the user adds 1,1,0,0 ... 0. In the puzzle it can't solve it because its two 1's next to each other and will just go on forever trying to find a sulotion even though its an unsolvable puzzle. However if they were all 0's(empty) it would solve it right away, same as if Id put 1 and 2 in the top left corner. If I'd just put some random numbers in it will detect it as unsolvable (or will solve it if it's a valid puzzle)

I'm thinking around the lines of saying, when theNumber == (row, col) equals thenNumber == (row+1, col), it should return false because it's a duplicated number.

This is the code I tried to add in the solve method, obviously without success.

if ((puzzle.getNum(row, col) == a) == (puzzle.getNum(row + 1, col) == a)) {
   return false;
}

Help is greatly appreciated

like image 567
Rob Avatar asked Mar 21 '13 16:03

Rob


People also ask

Is there any rule to solve Sudoku?

The goal of sudoku is simple: fill in the numbers 1-9 exactly once in every row, column, and 3x3 region. For example, look at the above puzzle and compare it to the solved version below. Notice that every row, column and 3x3 region contain every number from 1-9 exactly once. The same puzzle, this time solved.

What is the 45 rule in Sudoku?

The 45 rule is a basic solving-technique in Killer Sudoku. Each house (row, column, nonet) must add to 45 (the sum of the digits 1 through 9).

Is Sudoku always 9 * 9?

In classic Sudoku, the objective is to fill a 9 × 9 grid with digits so that each column, each row, and each of the nine 3 × 3 subgrids that compose the grid (also called "boxes", "blocks", or "regions") contain all of the digits from 1 to 9.


1 Answers

Validate the puzzle like this:

  1. Create a boolean array of 9 elements.
  2. Loop through every row, column and 9x9 box.
    • If you read a number, set the corresponding value in the array to true.
    • If it is already true throw an error (impossible puzzle).
    • After reading a row, column or 9x9 box reset the boolean array.
  3. Then, if the validation succeeded call the solving method.

EDIT: Source code

public boolean checkPuzzle() {
    boolean[] nums = new boolean[9];
    for (int row = 0; row < panel.puzzleSize; row++) {
        for (int cell = 0; cell < panel.puzzleSize; cell++) {
            if (nums[puzzle[row][cell]]) return false;
            nums[puzzle[row][cell]] = true;
        }
        nums = new boolean[9];
    }
    for (int col = 0; col < panel.puzzleSize; col++) {
        for (int cell = 0; cell < panel.puzzleSize; cell++) {
            if (nums[puzzle[cell][col]]) return false;
            nums[puzzle[cell][col]] = true;
        }
        nums = new boolean[9];
    }
    for (int square = 0; square < panel.puzzleSize; square++) {
        int squareCol = panel.squareSize * (square % panel.squareSize);
        int squareRow = panel.squareSize * Math.floor(square / panel.squareSize);
        for (int cell = 0; cell < panel.puzzleSize; cell++) {
            int col = cell % panel.squareSize;
            int row = Math.floor(cell / panel.squareSize);
            if (nums[puzzle[squareCol + col][squareRow + row]]) return false;
            nums[puzzle[squareCol + col][squareRow + row]] = true;
        }
        nums = new boolean[9];
    }
    return true;
}

Didn't have too much time to test out, but it might work (?). The row/col variable namings might be incorrect, because I didn't have time to find that in your code, but it shouldn't matter for it to work or not.

like image 113
PurkkaKoodari Avatar answered Sep 22 '22 04:09

PurkkaKoodari