473,249 Members | 1,686 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,249 software developers and data experts.

Question on the sudoku solution on this website

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.

Expand|Select|Wrap|Line Numbers
  1. boolean[][] rows= new boolean[9][9]; 
  2. ... 
  3. if (row[i][val]) // 'val' is already present in row 'i' 
if the boolean values are just declared like that, how would that serve the purposes of checking whether the value is present in a row, as done in
the method "possible", shown below?

Expand|Select|Wrap|Line Numbers
  1. private boolean possible(int i, int j, int val) { 
  3.     // position already taken or invalid? 
  4.     if (rows[i][val] || columns[j][val] ||  
  5.         squares[3*(i/3)+j/3][val]) 
  6.         return false; 
  8.     // position i,j is taken now:         
  9.     rows[i][val]= true; 
  10.     columns[j][val]= true; 
  11.     squares[3*(i/3)+j/3][val]= true; 
  13.     board[i][j]= val+1; 
  15.     return true; 
suppose the values 1,1,7 was passed to the method 'possible.'

it would first check rows[1][7], right? and that would yield true or false.
now...rows[1][7] has not been declared true or false. How would the method
at that point know anything about the first row? in fact, how do these boolean
values know to refer to the actual sudoku board, int board[][], since these
rows[][], columns[][], and squares[][], are boolean values of their own, completely unrelated to the sudoku board int values, board[][]?

perhaps I just don't understand how this "possible" method works.
any help will be appreciated. Thank you!
Dec 17 '08 #1
1 1834
11,448 Expert 8TB
The value row[i][val] represents the status that value val is present in row i. The same reasoning applies to the other boolean arrays.

If you look at the solve() method only empty squares are checked, so for a value val to be possible at an empty square i,j there shouldn't be a value val in the same row (row[i][val]) or same column (col[j][val]) or a value val in the same sub-square.

When the arrrays are created with the new operator all values are initialized to the value false implicitly which conforms to an initially empty sudoku board.

kind regards,

Dec 17 '08 #2

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...
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...
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...
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...
by: hashimtk | last post by:
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.
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...
by: Boon | last post by:
Hello group, I've been toying with a simple sudoku solver. I meant for the code to be short and easy to understand. I figured "brute force is simple" -- who needs finesse, when you've got...
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,...
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.