473,547 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sudoku

2 New Member
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 1776
r035198x
13,262 MVP
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 Recognized Expert MVP
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
hashimtk
2 New Member
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 Recognized Expert MVP
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
2629
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 combinatorial explosion completely scuppers this type of program)... Basic idea:- Start with a grid initialised with: 123456789
5
9969
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 isn't aesthetically the best either. There ought to be a way of making the grids look nicer. I've played about a bit with rowgroups and...
11
4123
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 faster than the ASPN program. If anyone is interested I attached the code in http://agolb.blogspot.com/2006/01/sudoku-solver-in-python.html
12
6322
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. but thats not i want to do. I want to recurse back until until it find a correct number. i will post the function which i need the help; ...
0
6721
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 of global famine, but because of a puzzle: Sudoku. This is what Sudoku is all about: +-------+-------+-------+
6
11865
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 so far: However I have found that it does not check it correctly. I just need to check the 9x9 array, which I am passing to this function...
21
11349
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 some time and still not able to get the solution out. so i hope you guys can help me out. of course i am not expecting a full solution, but i would...
3
8590
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, so making this took me hours and hours of time... // class SudokuBoard, will be called by class Sudoku import java.util.Scanner; import...
1
1854
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 to the Sudoku howto posted on this website. The links are shown above. My question centers around the boolean variables declared in Part A, such as...
3
5960
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, by checking a few things. 1. There are enough numbers, no 'periods' which signify '0' or 'no input'. 2. That there are no duplicate numbers in any...
0
7507
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7698
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7794
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6030
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5361
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.