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.
4 1786
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.
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
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
- 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 fillPossibilitiesMatrix() {
-
for (int row =0; row <puzzle.length; row++) {
-
for (int column =0; column <puzzle.length; column++) {
-
for (int number = 0; number<puzzle.length; number++){
-
possibilities[row][column][number] = true;
-
}
-
}
-
}
-
}
-
-
public static void resetPossibilitiesValue(int row, int column, int number) {
-
-
resetRow( row, number);
-
resetColumn( column, number);
-
int rebox=getBox(row,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;number++) {
-
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.length; row++) {
-
for( int column=0; column<puzzle.length; column++) {
-
System.out.print(puzzle[row][column] + " ");
-
}
-
System.out.println(" ");
-
}
-
}
-
-
public static void resetRow(int rerow, int number) {
-
for( int column=0; column<puzzle.length; column++) {
-
possibilities[rerow][column][number-1] = false;
-
}
-
}
-
-
public static void resetColumn(int recolumn, int number) {
-
for(int row=0; row<puzzle.length; 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,number;
-
fillPossibilitiesMatrix();
-
while (checkSudoku() < 81) {
-
for ( row=0; row<puzzle.length; row++) {
-
for ( column=0; column<puzzle.length;column++) {
-
number = puzzle[row][column];
-
if(number != 0) {
-
resetPossibilitiesValue (row,column,number);
-
}
-
else {
-
getCellSolution( row, column);
-
}
-
}
-
}
-
}
-
printSudoku();
-
}
-
}
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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 colgroups before discovering that
rowgroup is called tbody.
|
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
|
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;
---coding----------------------------------------------------------
|
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:
+-------+-------+-------+
| |
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 against the classic sudoku rules and then return true for
|
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 greatly appreciate it if anyone can suggest to me what should i do. i am very new to this subject so...
|
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 java.io.*;
public class SudokuBoard {
private int board = new int;
|
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 the following.
boolean rows= new boolean;
...
|
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 row, column, or 3x3 box.
I have been thinking of ways to do this and I've come up with 2 things....
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |