473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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.
Nov 19 '07 #1
4 1762
r035198x
13,262 8TB
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.
Nov 19 '07 #2
JosAH
11,448 Expert 8TB
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
Nov 19 '07 #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
Expand|Select|Wrap|Line Numbers
  1.  package hitori;
  2.  
  3. public class Sudoku {
  4.  
  5.  
  6.     public final static int puzzle[][] = {
  7.         {2, 0, 0, 7, 4, 0, 8, 0, 1},      
  8.         {8, 0, 0, 2, 9, 0, 5, 0, 6},
  9.         {0, 7, 5, 0, 8, 0, 0, 0, 0},
  10.         {0, 0, 0, 8, 2, 0, 6, 0, 0},
  11.         {4, 0, 2, 0, 0, 0, 9, 0, 8},
  12.         {0, 0, 3, 0, 6, 9, 0, 0, 0},
  13.         {0, 0, 0, 0, 1, 0, 4, 6, 0},
  14.         {3, 0, 1, 0, 7, 8, 0, 0, 9},
  15.         {6, 0, 9, 0, 5, 4, 0, 0, 7}
  16.     };
  17.  
  18.  
  19.     static boolean possibilities[][][] = new boolean[9][9][9];
  20.  
  21.  
  22.     public static void fillPossibilitiesMatrix() {
  23.         for (int row =0; row <puzzle.length; row++) {
  24.             for  (int column =0; column <puzzle.length; column++) {
  25.                 for (int number = 0; number<puzzle.length; number++){ 
  26.                        possibilities[row][column][number] = true;   
  27.                 }                     
  28.             }
  29.         }
  30.     }
  31.  
  32.     public static void resetPossibilitiesValue(int row, int column, int number) { 
  33.  
  34.                      resetRow( row, number);
  35.                      resetColumn( column, number);
  36.                      int rebox=getBox(row,column);
  37.                      resetBox( rebox, number);
  38.  
  39.  
  40.     }
  41.  
  42.     /**
  43.      * Looks into the possibilities matrix and if the cell can have only one
  44.      * possible value, then returns the correct number from 1 to 9,
  45.      * else returns -1
  46.      */
  47.     public static void getCellSolution(int row, int column) {   
  48.        int trues = 0,value = 0;
  49.        for (int number = 0;number<9;number++) {
  50.            if (possibilities[row][column][number]) {
  51.                     trues++;  
  52.                     value = number +1;
  53.            }
  54.        }
  55.        if (trues == 1) {
  56.                     puzzle[row][column] = value; 
  57.                 } 
  58.        }     
  59.     /**
  60.      * Prints the Sudoku as 9 rows of 9 columns each
  61.      */
  62.     public static void printSudoku() {
  63.        for( int row=0; row<puzzle.length; row++) { 
  64.            for( int column=0; column<puzzle.length; column++) {
  65.                     System.out.print(puzzle[row][column] + " ");
  66.            }
  67.             System.out.println(" ");
  68.        }
  69.     }
  70.  
  71.     public static void resetRow(int rerow, int number) {
  72.          for( int column=0; column<puzzle.length; column++) {
  73.                     possibilities[rerow][column][number-1] = false;
  74.                }
  75.     }
  76.  
  77.     public static void resetColumn(int recolumn, int number) {
  78.           for(int row=0; row<puzzle.length; row++) {
  79.                     possibilities[row][recolumn][number-1] = false;
  80.           }
  81.     }
  82.  
  83.     public static void resetBox(int box, int number) {
  84.       switch(box) {
  85.             case 1: for ( int row = 0; row<3; row++) {
  86.                       for (int column= 0; column<3; column++) {
  87.                            possibilities[row][column][number-1] = false;
  88.                       }
  89.                     } break;
  90.             case 2: for ( int row = 0; row<3; row++) {
  91.                       for (int column= 3; column<6; column++) {
  92.                           possibilities[row][column][number-1] = false;  
  93.                       }
  94.                     } break;
  95.             case 3: for ( int row = 0; row<3; row++) {
  96.                       for (int column= 6; column<9; column++) {
  97.                           possibilities[row][column][number-1] = false;  
  98.                       }
  99.                     }  break;
  100.             case 4: for ( int row = 3; row<6; row++) {
  101.                       for (int column= 0; column<3; column++) {
  102.                           possibilities[row][column][number-1] = false;  
  103.                       }
  104.                     } break;
  105.             case 5: for (int  row = 3; row<6; row++) {
  106.                       for (int column= 3; column<6; column++) {
  107.                           possibilities[row][column][number-1] = false;  
  108.                       }
  109.                     } break;
  110.             case 6: for ( int row = 3; row<6; row++) {
  111.                       for (int column= 6; column<9; column++) {
  112.                           possibilities[row][column][number-1] = false;  
  113.                       }
  114.                     } break;
  115.             case 7: for ( int row = 6; row<9; row++) {
  116.                       for (int column= 0; column<3; column++) {
  117.                           possibilities[row][column][number-1] = false;
  118.                       }
  119.                     } break;
  120.             case 8: for ( int row = 6; row<9; row++) {
  121.                       for (int column= 3; column<6; column++) {
  122.                           possibilities[row][column][number-1] = false;
  123.                       }
  124.                     } break;
  125.             case 9: for ( int row = 6; row<9; row++) {
  126.                       for (int column= 6; column<9; column++) {
  127.                           possibilities[row][column][number-1] = false; 
  128.                       }
  129.                     } break;
  130.       }
  131.     }
  132.  
  133.     public static int checkSudoku() {
  134.         int count = 0;
  135.         for(int row = 0; row< 9; row++) {
  136.             for ( int column = 0; column<9; column++) {
  137.                if (puzzle[row][column] != 0) {
  138.                   count++;
  139.                }
  140.             }  
  141.         } 
  142.         return count;
  143.     }         
  144.      static int getBox(int row, int column) {
  145.         int box = 0;
  146.         box= ( 3*row/3)+(column/3) +1;
  147.         return box; 
  148.      }
  149.  
  150.  
  151.     public static void main(String argv[]) {
  152.         int row,column,number;
  153.         fillPossibilitiesMatrix();
  154.        while (checkSudoku() < 81) {
  155.          for ( row=0; row<puzzle.length; row++) {
  156.            for ( column=0; column<puzzle.length;column++)  {
  157.              number =  puzzle[row][column];
  158.              if(number != 0) {
  159.                  resetPossibilitiesValue (row,column,number);
  160.              } 
  161.              else {
  162.                  getCellSolution( row, column);
  163.              }      
  164.            }
  165.          } 
  166.         }
  167.        printSudoku();    
  168.     }  
  169.     }
Nov 20 '07 #4
JosAH
11,448 Expert 8TB
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
Nov 20 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: sub1ime_uk | last post by:
Thought I'd offer a method for solving all possible 9x9 sudoku puzzles in one go. It'll takes a bit of time to run however (and 9x9 seems to be about as big as is reasonably possible before...
5
by: Stewart Gordon | last post by:
I have a few Sudoku puzzles on my site. http://www.stewartsplace.org.uk/mindbenders/ But adding extra columns and rows to separate the 3x3 blocks seems a rather kludgy approach, and the result...
11
by: ago | last post by:
Inspired by some recent readings on LinuxJournal and an ASPN recipe, I decided to revamp my old python hack... The new code is a combination of (2) reduction methods and brute force and it is quite...
12
by: kalinga1234 | last post by:
hy guys i am having a problem with my sudoku program which i coded using c++.; currently in my program if a duplicate number exist in either row/column/block i would make the particualr square...
0
by: JosAH | last post by:
Greetings, a couple of years ago a large part of the world went totally mad. Not because of global climate changes, not because of terrible wars that were started in the Middle East, nor because...
6
by: blux | last post by:
I am working on a function to check the validity of a sudoku puzzle. It must check the 9x9 matrix to make sure it follows the rules and is a valid sudoku puzzle. this is what I have come up with...
21
by: ningxin | last post by:
Hi, i am currently taking a module in c++ in the university, and was given an assignment. because i have no prior background on the subject, everything is kind of new to me. i have tried for quite...
3
by: deanchhsw | last post by:
Hello, I'm trying to build a program that solves sudokus and prints out the result on the screen. Here's the code for the class SudokuBoard. this will later be called in a class Sudoku. I'm a newbie,...
1
by: deanchhsw | last post by:
Part A (http://bytes.com/topic/java/insights/645821-sudoku) B (http://bytes.com/topic/java/insights/739704-sudoku-b) C (http://bytes.com/topic/java/insights/739703-sudoku-c) this question refers...
3
by: DannyB13 | last post by:
Hi, and thanks for possible help in advance. Here's my dilemma. I've been making a sudoku generator, and I'm now stuck on one part. I must be able to take a 'solution' and verify that it is correct,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.