sudoku

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hashimtk
    New Member
    • Nov 2007
    • 2

    sudoku

    Hi,
    i done a sudoku solving program ,and i got solution for easy sudoku, Can u explain any logic to find out medium and hard sudoku.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by hashimtk
    Hi,
    i done a sudoku solving program ,and i got solution for easy sudoku, Can u explain any logic to find out medium and hard sudoku.
    I hope you went through this article.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      I wrote that article and solver and I'm curious how it behaves on 'difficult' problems.
      Please let us know the results you find.

      kind regards,

      Jos

      Comment

      • hashimtk
        New Member
        • Nov 2007
        • 2

        #4
        Originally posted by JosAH
        I wrote that article and solver and I'm curious how it behaves on 'difficult' problems.
        Please let us know the results you find.

        kind regards,

        Jos
        [CODE=java] package hitori;

        public class Sudoku {


        public final static int puzzle[][] = {
        {2, 0, 0, 7, 4, 0, 8, 0, 1},
        {8, 0, 0, 2, 9, 0, 5, 0, 6},
        {0, 7, 5, 0, 8, 0, 0, 0, 0},
        {0, 0, 0, 8, 2, 0, 6, 0, 0},
        {4, 0, 2, 0, 0, 0, 9, 0, 8},
        {0, 0, 3, 0, 6, 9, 0, 0, 0},
        {0, 0, 0, 0, 1, 0, 4, 6, 0},
        {3, 0, 1, 0, 7, 8, 0, 0, 9},
        {6, 0, 9, 0, 5, 4, 0, 0, 7}
        };


        static boolean possibilities[][][] = new boolean[9][9][9];


        public static void fillPossibiliti esMatrix() {
        for (int row =0; row <puzzle.lengt h; row++) {
        for (int column =0; column <puzzle.lengt h; column++) {
        for (int number = 0; number<puzzle.l ength; number++){
        possibilities[row][column][number] = true;
        }
        }
        }
        }

        public static void resetPossibilit iesValue(int row, int column, int number) {

        resetRow( row, number);
        resetColumn( column, number);
        int rebox=getBox(ro w,column);
        resetBox( rebox, number);


        }

        /**
        * Looks into the possibilities matrix and if the cell can have only one
        * possible value, then returns the correct number from 1 to 9,
        * else returns -1
        */
        public static void getCellSolution (int row, int column) {
        int trues = 0,value = 0;
        for (int number = 0;number<9;numb er++) {
        if (possibilities[row][column][number]) {
        trues++;
        value = number +1;
        }
        }
        if (trues == 1) {
        puzzle[row][column] = value;
        }
        }
        /**
        * Prints the Sudoku as 9 rows of 9 columns each
        */
        public static void printSudoku() {
        for( int row=0; row<puzzle.leng th; row++) {
        for( int column=0; column<puzzle.l ength; column++) {
        System.out.prin t(puzzle[row][column] + " ");
        }
        System.out.prin tln(" ");
        }
        }

        public static void resetRow(int rerow, int number) {
        for( int column=0; column<puzzle.l ength; column++) {
        possibilities[rerow][column][number-1] = false;
        }
        }

        public static void resetColumn(int recolumn, int number) {
        for(int row=0; row<puzzle.leng th; row++) {
        possibilities[row][recolumn][number-1] = false;
        }
        }

        public static void resetBox(int box, int number) {
        switch(box) {
        case 1: for ( int row = 0; row<3; row++) {
        for (int column= 0; column<3; column++) {
        possibilities[row][column][number-1] = false;
        }
        } break;
        case 2: for ( int row = 0; row<3; row++) {
        for (int column= 3; column<6; column++) {
        possibilities[row][column][number-1] = false;
        }
        } break;
        case 3: for ( int row = 0; row<3; row++) {
        for (int column= 6; column<9; column++) {
        possibilities[row][column][number-1] = false;
        }
        } break;
        case 4: for ( int row = 3; row<6; row++) {
        for (int column= 0; column<3; column++) {
        possibilities[row][column][number-1] = false;
        }
        } break;
        case 5: for (int row = 3; row<6; row++) {
        for (int column= 3; column<6; column++) {
        possibilities[row][column][number-1] = false;
        }
        } break;
        case 6: for ( int row = 3; row<6; row++) {
        for (int column= 6; column<9; column++) {
        possibilities[row][column][number-1] = false;
        }
        } break;
        case 7: for ( int row = 6; row<9; row++) {
        for (int column= 0; column<3; column++) {
        possibilities[row][column][number-1] = false;
        }
        } break;
        case 8: for ( int row = 6; row<9; row++) {
        for (int column= 3; column<6; column++) {
        possibilities[row][column][number-1] = false;
        }
        } break;
        case 9: for ( int row = 6; row<9; row++) {
        for (int column= 6; column<9; column++) {
        possibilities[row][column][number-1] = false;
        }
        } break;
        }
        }

        public static int checkSudoku() {
        int count = 0;
        for(int row = 0; row< 9; row++) {
        for ( int column = 0; column<9; column++) {
        if (puzzle[row][column] != 0) {
        count++;
        }
        }
        }
        return count;
        }
        static int getBox(int row, int column) {
        int box = 0;
        box= ( 3*row/3)+(column/3) +1;
        return box;
        }


        public static void main(String argv[]) {
        int row,column,numb er;
        fillPossibiliti esMatrix();
        while (checkSudoku() < 81) {
        for ( row=0; row<puzzle.leng th; row++) {
        for ( column=0; column<puzzle.l ength;column++) {
        number = puzzle[row][column];
        if(number != 0) {
        resetPossibilit iesValue (row,column,num ber);
        }
        else {
        getCellSolution ( row, column);
        }
        }
        }
        }
        printSudoku();
        }
        }[/CODE]
        Last edited by r035198x; Nov 20 '07, 06:19 AM. Reason: added code tags

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Hi, did your program solve that puzzle? The solver described in that article solved
          it instantaneously (I didn't time it though).

          kind regards,

          Jos

          Comment

          Working...