Sudoku Programmers Forum Index

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log inLog in          Games  Calendar

Log in to check your private messagesLog in to check your private messages   

Box number

 
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Programming sudoku
View previous topic :: View next topic  
Author Message
ChPicard

Joined: 12 Mar 2008
Posts: 82
:
Location: Montreal, Canada

Items
PostPosted: Tue Sep 08, 2009 6:10 pm    Post subject: Box number Reply with quote

HI

What is the formula to calculate the box number (b=1; b<10;) from the given's position (x=0; x<81;)?

Thank you
Back to top
View user's profile Send private message
humble_programmer

Joined: 27 Jun 2006
Posts: 69
:
Location: Colorado Springs, USA

Items
PostPosted: Tue Sep 08, 2009 9:20 pm    Post subject: Reply with quote

Try this, where index is the square index in the range [0..80]

Code:

iBox = ((( index / 9) / 3) * 3) +  (( index % 9) / 3);


The result in iBox will be in the range [0..8], so you may have to add 1; also, don't simplify the algebra, as it relies on the truncation of integers.
_________________
Cheers!
Humble Programmer
,,,^..^,,,
www.humble-programmer.com
Back to top
View user's profile Send private message Visit poster's website
Lunatic

Joined: 11 Mar 2007
Posts: 166
:
Location: Ghent - Belgium

Items
PostPosted: Wed Sep 09, 2009 3:06 pm    Post subject: Reply with quote

humble_programmer wrote:
Try this, where index is the square index in the range [0..80]

Code:

iBox = ((( index / 9) / 3) * 3) +  (( index % 9) / 3);


The result in iBox will be in the range [0..8], so you may have to add 1; also, don't simplify the algebra, as it relies on the truncation of integers.


However, you can simplify it...
Code:

iBox = (( index / 27) * 3) +  (( index % 9) / 3);


In my program i use some global arrays, and the one for this purpose is called iCell2Box[0 to 80]

Initiation at program start:
Code:

For (i=0;i<81;i++) iCell2Box(i) = ((i / 27) * 3) + ((i % 9) / 3);


Wherever you want to know in which box a cell (index) is in, just use:
Code:

iBox = iCell2Box[index];

_________________
Marc
~~~<><~~~<><~~~<><~~~<><~~~
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Programming sudoku All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Sudoku Programmers topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group

Igloo Theme Version 1.0 :: Created By: Andrew Charron