469,301 Members | 2,272 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,301 developers. It's quick & easy.

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' 
  4.  
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) { 
  2.  
  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; 
  7.  
  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; 
  12.  
  13.     board[i][j]= val+1; 
  14.  
  15.     return true; 
  16.  
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 1657
JosAH
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,

Jos
Dec 17 '08 #2

Post your reply

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

Similar topics

5 posts views Thread by sub1ime_uk | last post: by
5 posts views Thread by Stewart Gordon | last post: by
11 posts views Thread by ago | last post: by
12 posts views Thread by kalinga1234 | last post: by
4 posts views Thread by hashimtk | last post: by
21 posts views Thread by ningxin | last post: by
38 posts views Thread by Boon | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.